# cat /etc/crontab (To read/print any file)
# cat –n /etc/crontab (To print files with line number)
# cat /etc/crontab /etc/passwd (To print multiple files together)
# tac /etc/crontab (To print exact opposite output from cat command)
# wc /etc/crontab (To print line, word, character count of file)
# wc -l /etc/crontab (To print line count of file)
# wc -w /etc/crontab (To print word count of file)
# wc -c /etc/crontab (To print character count of file)
# wc -l -w /etc/crontab (To print line and word count of file)
# who | wc –l (To print count of logged user)
# date; cal; (; used to run two commands together)
# uniq (Print the unique content of file or comment)
# grep -o root /etc/passwd (Let’s print all output having root)
root
root
root
root
# grep -o root /etc/passwd | uniq (uniq command omitted all and just prints one)
root
# grep -o root /etc/passwd | uniq –c (to print of all count for root)
4 root
# grep -E -o 'root|ftp|games' /etc/passwd | uniq –c (To print multiple things)
4 root
3 games
2 ftp
# cut (To print a section from a line)
# cut <options> <filename>
Options:
-c => cut by characters
-f => cut by fields *must be used with delimeter –d*
-d => delimiter symbol definition like *:, ;, space, “, etc.*
# head -n 4 /etc/passwd | cut -c1 (To print first character of first column)
# head -n 1 /etc/passwd | cut -c1-4 (To print first four character of first column)
# head -n 1 /etc/passwd | cut -d ":" -f1 (To print first field using delimiter “:”)
Or
# head -n 1 /etc/passwd | cut -d: -f1
# head -n 1 /etc/passwd | cut -d: -f1,3 (To print first and third field)
# head -n 1 /etc/passwd | cut -d: -f-4 (To print field from first to fourth)
# awk (Used for pattern scanning and processing language)
Note: When cut didn’t work as you can use only one delimiter so here you can use *awk*
Command.
# cat example.txt | awk –F’[:;]’ ‘{print $1}’ (In cut command you can’t use two delimiter so you can use awk command there)
# lsblk | awk '{print $1}' (To print first column)
# lsblk | awk '{print $1,4}' (To print first and fourth column)
# lsblk | awk '{print $1,$4}' | column –t (To print in proper column form)
# df -h | grep rl-root | awk '{print $5}' | column -t | awk -F"%" '{print $1}' (See what’s print it)
# lsblk | grep -E 'nvme0n1p1|nvme0n1p2' | awk '{print $4}' | cut -d"G" -f1 (See what’s print it)
Run below commands and see some difference between “cut” and “awk” command:
# tail -n 4 /etc/passwd | cut -d':' -f1,4 (cut command to find field)
# tail -n 4 /etc/passwd | awk -F':' '{print $1,$4}' (awk command to cut field but didn’t get same)
# tail -n 4 /etc/passwd | awk -F':' '{print $1,$4}' OFS=':' (Will do what above cut command did)
# ifconfig ens160 | grep -w inet | awk '{print $2}' (-w with grep will grep exact value)
# echo "varelite.com" (To print message)
# echo -e "varelite.com\n\n" (-e is used to interpretate of backlash spaces)
# echo -e " \bvar \belite \bcom" (\b will remove all spaces)
# echo -e " \nvar \nelite \ncom" (\n will add new line)
# echo -e " \tvar \telite \tcom" (\t will add tab)
# echo -e " \vvar \velite \vcom" (\v will add a vertical tab)
# echo -e " var \relite com" (\r will leave content before it)
# echo -e " var \celite com" (\c will leave written after it)
# echo -e " var \\ elite com" (\ will give you to keep \)
# echo -n "Write" (Do not print the trailing line)
# less filename (To read any file page per page, space – used for next page, b – back, q – quit)
# lscpu | less (You can see any command output)
# more filename (It is also used to see filename)
# ln –s source destination (To create soft link)
# ln source destination (To create hard link)
Soft Link => File and Folder
Hard Link => File but not any Folder
# ln -s hello.sh hello1.sh (Created a soft link for hello.sh, both will have different inode)
# ls -l hello1.sh
lrwxrwxrwx. 1 root root 8 Mar 13 10:42 hello1.sh -> hello.sh
# ls -i hello.sh
35228596 hello.sh
# ls -i hello1.sh
35218167 hello1.sh
Note: if you will create a hard link then inode will be same. Data will be replicated if one is deleted
Then you have another one. But in case of soft link, you can delete link but can’t source.
# find / -inum 35228596 (To find file with mentioned inode number)
# unlink hello1.sh (To unlink the soft link)
# sed
- Print
- Find and Replace
- Delete
- Before the line insert
- After the line insert
# sed ‘attribute ’ <filename>
# sed -n '1p' /etc/passwd (To print line number 1)
# sed -n '1,5p' /etc/passwd (To print line from 1 to 5)
# sed -n '1p;5p' /etc/passwd (To print line 1 and 5)
# sed -n '$p' /etc/passwd (To print last line)
# sed -n '1,5p;11,15p' /etc/passwd (To print 1-5 and 11-15 lines)
# cat -n /etc/passwd | sed -n '1,5p' (Check same command with cat)
# cat -n /etc/passwd | sed -n '1,5p;$p' (You can confirm the line number as well)
Note: Below command will not delete permanently in file it will delete while printing only to delete it permanent we need to user “-i” which can be seen below as well.
# cat -n info.txt | sed '5d' (It will print the content after deleting line no. 5)
# cat -n info.txt | sed '1,5d' (It will print the content after deleting lines 1-5)
# cat -n info.txt | sed '1d;5d' (It will delete line number 1 and 5)
# cat -n info.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
# sed -i '2d' info.txt
# cat -n info.txt
1 root:x:0:0:root:/root:/bin/bash
2 daemon:x:2:2:daemon:/sbin:/sbin/nologin
3 adm:x:3:4:adm:/var/adm:/sbin/nologin
4 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
5 sync:x:5:0:sync:/sbin:/bin/sync
6 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
7 halt:x:7:0:halt:/sbin:/sbin/halt
8 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
9 operator:x:11:0:operator:/root:/sbin/nologin
# sed ‘2,4!d’ info.txt (It will delete except 2-4)
# grep root data.txt | sed 's/root/varelite/' (It will change only first one in each line)
# grep root data.txt | sed 's/root/varelite/1' (It will change 1st root on each line)
# grep root data.txt | sed 's/root/varelite/2' (Similarly will do for 2nd root)
# grep root data.txt | sed 's/root/varelite/g' (It will change all root)
# grep root data.txt | sed 's/ROOT/varelite/gi' (‘gi’ will change if written in Caps or Small letter)
# grep root data.txt | sed '1s/root/varelite/g' (Will do in first line only)
# grep root data.txt | sed '1,5s/root/varelite/g' (Will do it line from 1 to 5)
# cat data.txt | grep -E 'root|daemon' | sed 's/root/varelite/g;s/daemon/cloud/g' (You can do multiple changes)
Or
# cat data.txt | grep -E 'root|daemon' | sed -e 's/root/varelite/g' -e 's/daemon/cloud/g'
# sed -i 's/root/varelite/g;s/daemon/cloud/g' data.txt (Will do it permanently)
# sed 's/:/>/g' data.txt (Will replace : with > symbol)
# grep ^[#] /etc/sudoers (Will print which has # in line first)
# grep ^[^#] /etc/sudoers (Will print which has not # in file)
# sed '9s/^/#/' info.txt (Will add # before starting the line 9)
# sed –i '9s/^/#/' info.txt (Will do it permanently in file)
# sed '9s/^#//' info.txt (Will remove # from the file)
# sed '5a varelite' info.txt (Will add after line number 5)
# sed '5i varelite' info.txt (Will add before the line number 5)
# find locations options <arguments> (Command use to data search)
Options:
-name
-iname
-inum
-size
-type
-empty
-perm
-user
-group
-exec, -atime, -amin, -ctime, -cmin, -mtime, -min, etc.
# find / -name passwd (To find passwd file in /)
# find . -name passwd (To find file at current folder)
# find /etc -name passwd (To look for file at /etc location)
# find / -name passwd 2> /dev/null (It will search and redirect error to m=null)
# find / -iname PASSWD (i will work as ignore case sensitive)
# find / -inum 34528414 (Will find file which has inode as mentioned)
# find /etc -size +5M (To print file name having size more than 5MB)
# find /etc -size +2M -size -9M (To print file more than 2M and less than 9 MB)
# find /script -type f (To print data which are files)
# find /script -type d (To print data which are directories)
# find /script -type l (To print soft links)
# find /script -type f -size +2M (The data which are files and having size more than 2 MB)
# find /script –empty (To print blank files and directory)
# find /script ! –empty (To print which are not empty)
# find /script -type f -exec cp -rf {} /script-copy \; (To find and copy data to other location)
exec – Will execute command and keep in loop until task is finished.
{} – Store your data here
\; - terminate command
# find /script -user root (To find out files owned by user root)
# find /script -group root (To find out data owned by group root)
# find /script -type f | xargs ls (xargs will list of files after find command)
# ls | xargs du –sh (To list size of files using xargs command)
# find /script -atime 50 (To find data which has been accessed 50 days back)
# find /script -mtime 10 (To find files modified 10 days back)
# find /script -mtime +5 -mtime -10 (To print files modified between 5days back and less
than 10 days)
# sort test.txt (Will short the content of files)
# sort -o file.txt test.txt (Will sort the content of test.txt and copy to file.txt)
# sort –r test.txt (Sort in reverse order)
# sort –n file.txt (Sort the numbers mentioned in file)
# sort -k 2n data.txt (Sort with column 2 values *2n* is used for the same.)
# sort -c test.txt (It will just give where is disorder of sort)
# sort –M months.txt (M will be used to sort months in the file)
# cat file.txt | tr [a-z] [A-Z] (Will change all lower case to upper in file.txt)
# tr [a-z] [A-Z] < file.txt > new-file.txt (Will move changed content to new-file.txt)
# cat data.txt | tr -d ' ' (Will delete space in that file on each line)
# cat file.txt | tr –s '.' (Will remove repeated word defined in ‘’)
One thought on “Some Commands helpful in Bash Shell Scripts”