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

Regex-1.1 Regular Expression Introduction


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

\w – matches a alphanumeric character
\d – matches a number
\s – matches a space
\W, \D, \S – opposite of \w, \d, \s
^ – beginning of line
$ – end of line character
. – match any character except for newline
* – 0 or more occurrence
+ – 1 or more occurrence
? – 0 or 1 occurrence
| – alternative match
(regex) – grouping/store match
[ ] – character set/class
{ x,y } – number of occurrence between x and y
{ x } – x occurrence
{ x, } – at least x occurrence
[^ ] – opposite of [ ]
*? – non greedy matching

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

UNIX-1.3 UNIX Commands and Options


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

du – data usage
du -sh – find how much disk space used in current directory

df – data file system
df -h – find disk space usage

cp – copies files
cp file1 file2 – copy file1 to file2
cp file1 dir/file2 – copy file1 to file2 in dir
cp file1 dir/ – copy file1 to dir with same filename
cp -rf dir1 dir2 – recursively copy dir1 to dir2
cp -i file1 file2 – prompt before overwriting file2
cp file1 file2 file3 dir1 – copy multiple files to a directory
cp -p file1 file2 – by default, permission is not preserved, use -p to do that
cp -s file1 link1 – create soft link to file1

mv – change/move files
mv file1 file2 – change filename from file1 to file2
mv -i file1 file2 – prompt before overwriting
mv file1 dir1 – move file1 to dir1
mv dir1 dir2 – move dir1 to dir2 if dir2 exists, else change name from dir1 to dir2

head – display lines in file from beginning
head file1 – display first 10 lines of file1
head -100 file1 – display first 100 lines of file1

tail – display lines in file from end
tail file1 – display last 10 lines of file1
tail -100 file1 – display last 100 lines of file1

rm – remove file or directory
rm file1 – remove file1
rm -rf dir1 – remove entire directory, never prompt
rm -i dir1 – prompt before removing

mkdir – create directory
mkdir dir1 – create dir1
mkdir -m777 dir1 – create dir1 with specified permission
mkdir -p dir1 – ignore error if dir1 already exists

tar – archive file
tar zcvf tarfilename.tar file1 file2 file3 – archive all these files into one tar ball, compressed
tar xvf tarfilename.tar – extract files from a tar ball
tar rf tarfilename.tar extra.txt – append extra.txt to existing tarfilename.tar (tarfilename.tar is not compressed)
tar tvf tarfilename.tar – list out files in tar ball

gzip – compress file
gzip file1 – compress file1, new filename will have .gz extension
gzip -d file1.gz – decompress file1.gz

chmod – change permission
chmod 777 file1 – change permission for file1 (rwxrwxrwx)
chmod +x file1 – give executable permission for all to file1 (+r, +w)
chmod u+x file1 – give executable permission for owner to file1 (g+x, o+x)
chmod -R 777 dir1 – change permission for all contents in dir1