Book Report: Bash Scripting
Book Report: Bash Scripting
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!
1
Basics
date
- Displays date in the current server
echo“Hello World”
- T
o print message
cd~
- Change directory to the home path
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
catfile1.txt
- View contents of a file
catfile1.txt
| head
-
5
- View first 5 lines of the file
catfile1.txt
| tail
-
5
- View last 5 lines of the file
tail -ffile1.txt
- View changes in file
gzipfile1.txt
- Zips the file to .gz file and deletes the original file
gunzipfile1.gz
-UnZips the file and deletes the gz file
zcatmessages.gz
- View contents of a zipped file without unzipping
difffile1.txt file2.txt
- Compare two files and shows the difference
2
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 ---
3
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
List Command
Ls Options
L
- Long format
R
- Reverse order
T
- Sort by time and date
S
- Sort by file size
4
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
./dev/data/initialize.sh
sh -vx /dev/data/initialize.sh
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.
5
ps -ef | grep
“harish_test.sh”
-Find process id
kill -9
process_id
- Kill the process
CTRL+C
- Come out the current action
df -h
-D
isk usage across drives in the particular server
du -ch
- File size of each file in the directory
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.txth
arish.txt
- Greps patterns from a file
6
Find command
7
AWK command
awk -F '
/pattern/
{action}
' input-file
101,John Doe,CEO
Mailnull
F- Field seperator
BEGIN,BODY,END
---header---
mailnull
---footer---
Doe,CEO
8
print "
-------------\nName\tTitle\n-------------"} \
{ print $2,"\t",$3; } \
$ vi
employee-multiple-fs.txt
- File with multiple delimiter
101,John Doe:CEO%10000
John Doe:CEO
9
$ vi
employee-change-fs-ofs.txt
101
John Doe
CEO
-
102
Jason Smith
IT Manager
-
John Doe:CEO
---
---
10
$ vifnr.awk
BEGIN { FS="," }
$ awk -f
fnr.awk employee.txt employee-multiple-fs.txt
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
Andhra
Arunachal
Assam
$ 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
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
15
SED command
sed 's/unix/linux/' file.txt
-Replacing or substituting string
16
17