0% found this document useful (0 votes)
66 views

Book Report: Bash Scripting

This book report summarizes Bash scripting basics including commands like date, echo, cd, cp, mv, touch, cat, tail, gzip, chmod, chown, vi editor, ls, rm, sleep, wc, ps, kill, df, du, grep, find, awk, cut and sed. It provides examples of using each command and explains some of their basic options and functions.

Uploaded by

Harish R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Book Report: Bash Scripting

This book report summarizes Bash scripting basics including commands like date, echo, cd, cp, mv, touch, cat, tail, gzip, chmod, chown, vi editor, ls, rm, sleep, wc, ps, kill, df, du, grep, find, awk, cut and sed. It provides examples of using each command and explains some of their basic options and functions.

Uploaded by

Harish R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

 

Book Report 
Bash scripting 
 

Harish Raja 
5th December, 2019 
 

   

 
 

Introduction 
1. Basics 
2. grep 
3. Find 
4. awk 
5. cut 
6. Sort 
7. Tr 
8. sed 

 
 
 
 

Reading Rainbow Tip: ​Was the title interesting? Did the cover spark your curiosity? Was it 
something else? Talk about why you chose the book to help your classmates understand more 
about you! 

   


 

Basics 
date​
- Displays date in the current server  

echo​“Hello World”​
- T
​o print message 

echo​“Hello World $1,$2”​


- To print message with variable 

cd​~​
- Change directory to the home path 

cd ../​- Goback to previous directory 

cd​/dev/data​
- Change directory to specified path 

cp​/dev/file1.txt /prd/file2.txt​
- Copy files from one place to another 

mv​/dev/file1.txt /prd/file2.txt​
- Rename files or move files from one location 
to another 

(Move command changes the pointer to the new file name. More efficient)

touch f
​ile1.txt​- Creates an empty file with given name 

cat​file1.txt​
- View contents of a file 

cat​file1.txt ​
| head​​
-​
5​
- View first 5 lines of the file 

cat​file1.txt ​
| tail​​
-​
5​
- View last 5 lines of the file 

tail -f​file1.txt​
- View changes in file 

gzip​file1.txt​
- Zips the file to .gz file and deletes the original file 

gunzip​file1.gz​
-​UnZips the file and deletes the gz file 

zcat​messages.gz​
- View contents of a zipped file without unzipping 

diff​file1.txt file2.txt​
- Compare two files and shows the difference 


 

top​
- Displays the list of processes currently running in the server. Also used 
to keep the current session ​
alive without disconnecting​

File permissions 

chmod 775 -R /
​dev/data/harish 

drwxxr-rw-r: Owner,Group,Others 

0- No permission 

1- Execute 

2- Write 

4- Read 

0 No permission ---

1 Execute permission --x

2 Write permission -w-

3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx

4 Read permission r--

5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x

6 Read and write permission: 4 (read) + 2 (write) = 6 rw-


 

7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx

Change ownership of file 

chown h
​arishapp:domain /dev/data/file.txt 

Vi editor 

vi ​
file1.txt-​To open files in vi editor 

:q​
- Quit without saving 

:wq​
- Save and quit 

:w​- Save the file but keep it open 

List Command 

ll​- list files with size 

ls​- List only names  

Ls Options 

a​- Show hidden files 

L​
- Long format 

R​
- Reverse order 

T​
- Sort by time and date 

S​
- Sort by file size 


 

Remove Command 

pwd​
-Present working directory 

mkdir -p ​
/dev/data/harish​- Create previous directory if not created 

rm ​
file*.txt 

rm -r /
​dev/data/harish​
- Recursive delete in directory 

rm -rf /
​dev/data/harish​- Force delete without asking for permission 

sleep 5
​​-Waits for 5 seconds 

wc -l f
​ile1.txt (or) c
​at ​
file1.txt​| wc -l​- Counts the number of lines in 
file 

Invoke a shell script file 

./dev/data/initialize.sh 

sh -vx /dev/data/initialize.sh  

V - verbose mode, Displays all the actions 

nohup sh -vx /
​dev/data/initialize.sh ​
>​audit.log &
​ 

Nohup ​is a ​POSIX​ command to ignore the ​HUP​ (hangup) signal. The ​HUP​ signal is, by convention,
the way a terminal warns dependent processes of logout.To keep the processes running even after
the session is closed.

Output that would normally go to the terminal goes to a file called ​nohup.out​ if it has not already
been redirected.


 

ps -ef | grep ​
“harish_test.sh”​
-​Find process id 

kill -9 ​
process_id​
- Kill the process 

CTRL+C​
- Come out the current action 

Disk space commands 

df -h​
-D
​isk usage across drives in the particular server 

du -ch​
- File size of each file in the directory 

h - Human readable format 

Grep command 
 
grep -i ​
"unix" harish.txt​
- Ignore case  

grep -n ​
"unix" harish.txt​
- Display along with line number 

grep -v ​
"unix" harish.txt​
- Display other than matched records 

grep –e ​
"Harish"​–e "
​test"​​
harish.txt​
- Multiple patterns in same command 

grep –f ​
pattern.txt​h
​arish.txt​
- Greps patterns from a file 


 

Find command 

find . -type f -name "


​tecmint.txt"​-exec rm -f {} \;

. Stands for current directory 

Type - type of file f or d for directory 

find /tmp -type f -empty​


-​Finds all empty files 

find /tmp -type f -name *


​.mp3​-size +
​10M​-exec rm {} \; 


 

AWK command 
awk -F '​
/pattern/​​
{action}​
' input-file 

awk '{print}' employee.txt 

101,John Doe,CEO 

awk -F: '​


/mail/​{print $1}' ​
/etc/passwd 

mail 

Mailnull 

F- Field seperator  

BEGIN,BODY,END 

awk 'BEGIN { FS="​


:​
";print "
​---header---"​} \ 

/mail/ {print $1} \ 

END { print "


​---footer---"​
}'​/etc/passwd 

---header--- 

mail 

mailnull 

---footer--- 

$ awk '{print $2}' ​


employee.txt​
- Default delimiter is space 

Doe,CEO 


 

FS-Input field separator 

awk 'BEGIN { FS=","; \ 

print "
​-------------\nName\tTitle\n-------------"​} \ 

{ print $2,"\t",$3; } \ 

END {print "-------------"}' ​


employee.txt 

$ vi ​
employee-multiple-fs.txt​
- File with multiple delimiter 

101,John Doe:CEO%10000 

102,Jason Smith:IT Manager%5000 

$ awk 'BEGIN {FS="[,:%]"} {print $2, $3}' e


​mployee-multiple-fs.txt 

John Doe CEO 

Jason Smith IT Manager 

OFS - Output Field Seperator 

$ awk -F ',' 'BEGIN { OFS=":" } \ 

{ print $2, $3 }' ​


employee.txt 

John Doe:CEO 

Jason Smith:IT Manager 


 

$ vi ​
employee-change-fs-ofs.txt 

101 

John Doe 

CEO 

102 

Jason Smith 

IT Manager 

awk 'BEGIN { FS="\n"; RS="-\n"; OFS=":" } \ 

{print $2, $3}' ​


employee-change-fs-ofs.txt 

John Doe:CEO 

Jason Smith:IT Manager 

RS- Record Seperator 

NF- number of fields 

NR- Number of records 

FNR- Number of records relative to the current physical file 

ORS - Output Record Separator 

$ awk 'BEGIN { FS=","; ORS="\n---\n" } \ 

{print $2, $3}' ​


employee.txt 

John Doe CEO 

--- 

Jason Smith IT Manager 

--- 

10 
 

$ vi​fnr.awk 

BEGIN { FS="," } 

{ printf "FILENAME=%s NR=%s FNR=%s\n", FILENAME, NR, FNR; } 

END { printf "END Block: NR=%s FNR=%s\n", NR, FNR } 

$ awk -f ​
fnr.awk employee.txt employee-multiple-fs.txt 

FILENAME=employee.txt NR=1 FNR=1 

FILENAME=employee.txt NR=2 FNR=2 

FILENAME=employee.txt NR=3 FNR=3 

FILENAME=employee.txt NR=4 FNR=4 

FILENAME=employee.txt NR=5 FNR=5 

FILENAME=employee-multiple-fs.txt NR=6 FNR=1 

FILENAME=employee-multiple-fs.txt NR=7 FNR=2 

FILENAME=employee-multiple-fs.txt NR=8 FNR=3 

FILENAME=employee-multiple-fs.txt NR=9 FNR=4 

FILENAME=employee-multiple-fs.txt NR=10 FNR=5 

END Block: NR=10 FNR=5 

11 
 

Cut command 
$ cat s
​tate.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

Character option 
$ cut -c 2,5,7 s
​tate.txt 

nr 

rah 

Sm 

$ cut -c 1-7 s
​tate.txt 

Andhra 

Arunach 

Assam 

The below command prints all the characters starting from 1st position till 
the end. 

$ cut -c 1- s
​tate.txt 

Andhra Pradesh 

Arunachal Pradesh 

Assam 

$ cut -c -5 s
​tate.txt 

Andhr 

Aruna 

Assam 

12 
 

cut -d " " -f 1 ​


state.txt

Andhra 

Arunachal 

Assam

Complement - Displays everything apart from the option 

 
$ cut --complement -d " " -f 1 ​
state.txt

Pradesh 

Pradesh 

Assam 

$ cut --complement -c 5 s
​tate.txt

Andha Pradesh 

Arunchal Pradesh 

Assa

13 
 

Sort command 
sort file.txt

abhishek 

chitransh 

divyam

 
sort -o filename.txt inputfile.txt​
- Place the o/p in given file 

sort -r inputfile.txt​
- Sort in descending 

sort -n filename.txt​
- Sort numerically 

sort -u filename.txt​
- Removes duplicates 

cat file.txt | sort | uniq -c​


- Finds the count of each line’s occurence 

uniq -d​
-Displays duplicates, one duplicate per group 

uniq -D​
-Displays duplicates, all the duplicate lines 

uniq -u​
-Displays only unique lines 

uniq -i​
-Displays unique lines while being case insensitive 

14 
 

TR command 
The tr command in UNIX is a command line utility for translating or deleting 
characters 

 
cat greekfile | tr “[a-z]” “[A-Z]”​
- Converts lowercase to uppercase

tr '{}' '()' newfile.txt​


- Converts flower to round braces

echo "Welcome To GeeksforGeeks" | tr -s [:space:] ' '​


-Convert multiple 
spaces to space 

echo "Welcome To GeeksforGeeks" | tr -d 'w'​


- Delete character

15 
 

SED command 

 
sed 's/unix/linux/' file.txt​
-Replacing or substituting string 

sed 's/unix/linux/2' geekfile.txt​


- Replace 2nd occurence line of file 

sed 's/unix/linux/2g' geekfile.txt​


- Replacing from nth occurrence to all 
occurrences in a line 

sed 's/unix/linux/g' geekfile.txt​


- Global change 

sed '3 s/unix/linux/' geekfile.txt​


- Replacing string on specific line 

sed '5d' filename.txt -


​ Remove 5th line

sed '$d' filename.txt -


​ Remove last line

sed '3,6d' filename.txt​


- Remove range of lines

sed '/abc/d' filename.txt​


- Remove pattern matching lines

sed -n '2,5p' a.txt​


- Print 2nd line to 5th line

sed '2,4d' a.txt​


- Delete range of line

16 
 

17 

You might also like