UNIX-1.10 Customize Command Prompt


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

set -o vi – puts your command prompt into VI mode, allows you to execute VI commands
/command – search for command history, n for next match found, N for previous match found
PS1 – variable that holds text of your prompt
PS1=”\H” – displays host name
PS1=”\s” – name of shell
PS1=”\u” – current username
PS1=”\w” – current working directory
PS1=”\@” – current time
PS1=”[\\u@\\H \\W \\@]\\$” – try this
PS1=”\e[x;ym$PS1\e[m” – \e[ starts the color mode, x;ym specifys color, \e[m ends color mode. x;y can be 0;30 to 0;37
case $(id -u) in
0) PS1=”${PS1}# “;;
*) PS1=”${PS1}$ “;;
esac
alias name=”command -options” – creating alias/shortcut commands
alias vi=’vim’
alias ls=’ls -F’
alias ll=’ls -l’
alias la=’ls -la’
alias cp=’cp -ip’
alias mv=’mv -i’

Advertisements

UNIX-1.9 .bash_profile vs .bashrc


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

sed – stands for “stream editor”, not “special editor”
umask 022 – user creation mask, 022 = rwxr-xr-x
/etc/profile – system wide profile, set by root

UNIX-1.8 ps, netstat, kill, pidof, fuser, awk commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

ps -fu username – displays all processes started by user
ps -ef – display all processes running
ps -ef | grep ‘pname’ – look for specific process
netstat -t – see all tcp connections
netstat -an – see all network connections
netstat -c – netstat will execute every second
netstat -p – display processes that use these connections
kill pid – kills process by pid
kill -9 pid – force kill process by pid
& – execute process in background
free – show memory usage, alternatively use top
pidof command – shows process id for running process/command
fuser -vu dir – displays all processes that are using dir, along with owner id of processes
echo “hello world” | awk ‘{ print length($1) }’ – prints length of field
echo “hello world” | awk ‘{ print substr($1, 3) }’ – prints llo, substring function
echo “hello world” | awk ‘{sub(regex, replacement); print}’ – replace pattern on line and print
echo “HELLO world” | awk ‘{ print tolower($1) }’ – lower case field 1
echo “hello world” | awk ‘{ print toupper($1) }’ – upper case field 1

UNIX-1.7 Find Command


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

find . -iname ‘regex’ – find filenames that match regex, case insensitive
find . -mtime +2 – find files that were modified 2 days ago or older
find . -mtime -2 – find files that were modified within past 2 days
find . -ctime +2 – find files that were created 2 days or older
find . -atime +2 – find files that were accessed 2 days or older
find . -cmin +30 – find files that were created 30 minutes ago or older
find . -mmin +30 – find files that were modified 30 minutes ago or older
find . -empty – find empty files or directory
find . -executable – find files that are executable by user
find . -readable – find files that are readable by user
find . -writable – find files that are writable by user
find . -size filesize – find files that have specified filesize (c = byte, k = kb, M = Mb, G = Gb)
find . -type filetype – find by file type (d = directory, f = file, l = link)
find . -user username – find files owned by user
find . -group groupname – find files that belong to specific group
find . -name ‘regex’ -delete – remove files that match regex
find . -name ‘regex’ -exec command – execute command on files found
find . -name ‘regex’ -ok command – same as -exec, but prompts for user’s input

UNIX-1.6 grep, useradd, usermod commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

last -10 username – get last 10 logins of specific user
passwd – change password
diff -y file1 file2 – find differences between 2 files
grep ‘regex’ file – search for pattern
grep -i ‘regex’ file – case insensitive search
grep -P ‘regex’ file – full support of Perl regex
grep -e ‘regex1’ -e ‘regex2’ file – search for multiple patterns
grep -v ‘regex’ file – inverse of search pattern (return non-matching lines)
grep -n ‘regex’ file – display line number with search result
grep -B4 ‘regex’ file – also display 4 lines before match
watch -n5 command – executes command every 5 second
adduser username – add a new user
usermod username -d /home/newhome – change home for new user
usermod username -d /home/newhome -m – change and move home content to a new home
usermod username -e 2022-05-11 – set account expiration date
usermod username -g group_name – set user group name/id
usermod username -l new_username – change username for user
usermod username -p new_password – change password for user
usermod username -s shell_name – change default shell for user
sudo -u username -i – run future commands as another user

UNIX-1.5 Awk, Cut, Last, Wc Commands Video Tutorial


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

awk -F’|’ ‘BEGIN{ initialization } { code block } END{ finalization }’ – general syntax
default delimiter is space ‘ ‘
VAR=”value” – define a variable
print VAR – print a variable
NF is number of fields
$NF is value of last field
print “‘$EXTERNAL_VAR'” – double single $EXTERNAL_VAR single double
VAR~/pattern/ – pattern patching
VAR!~/pattern/ – does not match
FS returns value of field separator, same as -F
OFS returns output field separator
print $1,$2 is separated
NR returns line number
FILENAME returns current filename
awk -v var1=value1 – pre-assign values to variables in awk
awk -F, ‘conditions { code block }’ filename – condition must be right before code block

cut – get columns from file
cut -d, -f1-2 filename – print 1st to 2nd columns
cut -c1-5 filename – print first 5 characters
cut -d, -f2- filename – print from 2nd column to last column
cut -d, -f1,4,5 filename – print 1st, 4th and 5th column
cut -d, -f-3 filename – print from 1st to 3rd column
cut -d, -f1,3-5 filename – print 1st, 3rd to 5th column

wc – word count
wc -l – line count
wc -c – print number of bytes
wc – print number of newline, word and bytes
wc -m – character count
wc -w – word count

last – check login history
last -10 – show last 10 logins
last -F – show full login information

UNIX-1.4 Sort and Sed Commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

sort – sort file
sort -t, -k1 file1 – sort file1 by column 1, delimited by comma (,)
sort -r -t, -k1 file1 – in reverse
sort -n -t, -k1 file1 – sort by numerical value
sort -f file1 – case insensitive sort
sort -t’|’ +0 -2 – sort by the first 2 fields, starting from 0+1th field and end at 2nd field.

sed – special editor
sed ‘s/pattern/replacement/gi’ – search and replacement globally and case insensitively
sed ‘s/pattern/&replacement/gi’ – & represents the pattern found
sed ‘s/\(group\)pattern/\1/gi’ – \1 contains value of 1st group
sed ‘s/hello/bye/2g’ – replacements second occurrence of hello
sed ‘/pattern/p’ – will print out occurrences of pattern
sed -e ‘command1’ -e ‘command2’ – run multiple commands
sed ‘s/'”$VAR”‘/replacement/’ – using external variables in sed
sed ‘2 s/pattern/replacement/’ – replace only for line 2. you can also specify line range, 1,10
sed ‘/pattern1/ s/pattern2/replacement/’ – find pattern1, for those lines, replace pattern2 with replacement
sed ‘/start/,/end/ s/pattern/replacement/’ – using patterns as line number range. you can also mix line number with pattern
sed ‘1,10d’ – deletes line 1 to 10. you can also mix pattern range as well
sed ‘100,$d’ – delete from line 100 to end of file
sed ‘100,$!d’ – delete from line 1 to line 100. ! means inverse operation.
sed ‘y/ABC/abc/’ – replacement each character with corresponding characters
http://www.grymoire.com/Unix/Sed.html

regular expression
[0-9a-z]* – match 0 or more alphanumeric characters
[0-9a-z]+ – match 1 or more alphanumeric characters
[^a-z] – any character does not contain a-z