SlideShare a Scribd company logo
Darshan Institute of Engineering &
Technology, Rajkot
Unit - 9
Unix Commands
maulik.trivedi@darshan.ac.in
9998 265 805
Computer Engineering Department
Prof. Maulik D. Trivedi
Operating System (OS)
GTU # 3140702
✓ Loopin
g
Outline
• What is an Operating System (OS) ?
• Tasks of Operating System
• What is Unix?
• Unix Operating System
• Unix History
• Features of Unix
• What is shell?
• Linux Operating System
• Architecture
• Component of Linux System
• Basic Function of Kernel
• Directory Structure
• Linux Vs. Windows
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
4
cd (Change Directory) Command
🞂 The cd command is used to change the current directory (i.e., the directory in
which the user is currently working)
🞂 Syntax :
cd [-Options] [Directory]
🞂 Example :
Option Use
cd .. Change Current directory to parent directory
cd ~ Move to users home directory from anywhere
cd lab_1 Change from current working directory to lab_1
cd ../downloads
If we are currently in /home/username/documents then we
would be placed in /home/username/downloads.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
5
cd Command Example
cd .. Change Current directory to parent directory
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
6
cd Command Example
cd ~ Move to users home directory from anywhere
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
7
cd Command Example
cd
lab_1
Change from current working directory to lab_1
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
8
cd Command Example
cd
../downloads
If we are currently in
/home/student/documents then we would be
placed in /home/student/downloads
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
9
ls Command
🞂 List directory contents.
🞂 Syntax :
ls [Options] [file|dir]
🞂 Example :
Option Use
ls -l
To show long listing information about the
file/directory
ls -a List all files including hidden file starting with '.'
ls -r List in reverse order
ls -t Sort by time & date
ls -s Sort by file size
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
10
🞂 Directory & Files
🞂 A read, write, execute permissions for user(root), group and others respectively
🞂 A number of links for file / dir.
🞂 It specifies owner of the file / dir.
🞂 It specifies group of the file / dir.
🞂 It indicates the file / dir size in bytes.
🞂 It specifies the date and time of the last modification of the file.
🞂 It specify name of the file / dir.
ls Command
drwxr-
xr-x
own
er
grou
p
othe
rs
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
11
ls Command Example
ls -l
To show long listing information about the
file/directory
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
12
ls Command Example
ls -a
List all files including hidden file starting
with '.'
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
13
ls Command Example
ls -r List in reverse order
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
14
man Command
• It is the interface used to view the system's reference manuals.
🞂 Syntax :
man [command name]
🞂 Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
15
echo Command
🞂 Display a line of text/string on standard output or a file.
🞂 Syntax :
echo [option] [string]
🞂 Example :
Option Use
echo -n Do not output a trailing newline
echo -e
Enable interpretation of backslash escape
sequences
Option Use
b It removes all the spaces in between the text
n It creates new line from where it is used
t It create horizontal tab spaces
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
16
echo Command Example
echo -n Do not output a trailing newline
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
17
echo Command Example
b It removes all the spaces in between the text
n It creates new line from where it is used
t It create horizontal tab spaces
echo -e
Enable interpretation of backslash escape
sequences
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
18
cal Command
🞂 Displays a simple, formatted calendar in your terminal.
🞂 Syntax :
cal [options] [[[day] month] year]
🞂 Example :
Option Use
cal -1 Display single month output. (This is the default.)
cal -3 Display three months spanning the date.
cal -s Display Sunday as the first day of the week.
cal -m Display Monday as the first day of the week.
cal -j
Use day-of-year numbering for all calendars. These are also
called ordinal days. Ordinal days range from 1 to 366.
cal –y Display a calendar for the whole year
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
19
cal Command Example
Cal or cal -1 Display single month output. (This is the default.)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
20
cal Command Example
cal -3 Display three months spanning the date.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
21
cal Command Example
cal -s Display Sunday as the first day of the week.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
22
cal Command Example
cal -m Display Monday as the first day of the week.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
23
cal Command Example
cal -j
Use day-of-year numbering for all calendars. These are
also called ordinal days. Ordinal days range from 1 to
366.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
24
cal Command Example
cal –y Display a calendar for the whole year
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
25
date Command
🞂 Print or set the system date and time.
🞂 Syntax :
date [OPTION]... [+FORMAT]
🞂 Example :
Option Use
date +%a The abbreviated weekday name (e.g., Sun)
date +%A The full weekday name (e.g., Sunday)
date +%b The abbreviated month name (e.g., Jan)
date +%B Locale's full month name (e.g., January)
date +%C
The current century; like %Y, except omit last two
digits (e.g., 20)
date +%w day of week (0..6); 0 is Sunday
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
26
date Command
Option Use
date +%d Display the day of the month
date +%m Displays the month of year (01 to 12)
date +%y Displays last two digits of the year(00 to 99)
date +%Y Display four-digit year.
date +%T Display the time in 24 hour format as HH:MM:SS
date +%H Display the hour
date +%M Display the minute
date +%S Display the seconds
date +%V
ISO week number, with Monday as first day of
week (01..53)
date +%P locale's equivalent of either AM or PM
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
27
date Command Example
date +%a The abbreviated weekday name (e.g., Sun)
date +%A The full weekday name (e.g., Sunday)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
28
date Command Example
date +%b The abbreviated month name (e.g., Jan)
date +%B Locale's full month name (e.g., January)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
29
date Command Example
date +%c Full date with IST timing
date +%C
The current century; like %Y, except omit last
two digits (e.g., 20)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
30
date Command Example
date +%d Display the day of the month
date +%m Displays the month of year (01 to 12)
date +%y Displays last two digits of the year(00 to 99)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
31
date Command Example
date +%y Displays last two digits of the year(00 to 99)
date +%Y Display four-digit year.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
32
date Command Example
date +%T
Display the time in 24 hour format as
HH:MM:SS
date +%H Display the hour
date +%M Display the minute
date +%S Display the seconds
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
33
date Command Example
date +%V
ISO week number, with Monday as first day of
week (01..53)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
34
date Command Example
date +%P locale's equivalent of either AM or PM
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
35
clear Command
🞂 Clear the terminal screen.
🞂 If you take a detailed look after running the clear command, you'll find that it
doesn't really clear the terminal. The tool just shifts the text upwards, out of the
viewable area.
🞂 Syntax :
clear
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
36
clear Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
37
cat Command
• It is used to create, display and concatenate file contents.
🞂 Syntax :
cat [OPTION] [FILE]
🞂 Example :
Option Use
cat -b Omits line numbers for blank space in the output
cat -E Displays a $ (dollar sign) at the end of each line
cat -n Line numbers for all the output lines
cat -s Suppress repeated empty output lines
cat -T Displays the tab characters as ^I in the output
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
38
cat Command
🞂 Example :
$ cat > file1.txt
🞂 It creates file1.txt and allow us to insert content for this file.
🞂 After inserting content you can use ctrl+c to exit the file.
$cat file.txt > newfile.txt
🞂 Read the contents of file.txt and write them to newfile.txt, overwriting anything
newfile.txt previously contained. If newfile.txt does not exist, it will be created.
$cat file.txt >> newfile.txt
🞂 Read the contents of file.txt and append them to the end of newfile.txt.
If newfile.txt does not exist, it will be created.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
39
cat Command
🞂 Example :
cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in the
terminal.
cat file1.txt file2.txt > combinedfile.txt
• It will concatenate the contents of file1.txt and file2.txt and write them to a new
file combinedfile.txt using the (>) operator.
• If the combinedfile.txt file doesn’t exist the command will create it. Otherwise it
will overwrite the file.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
40
cat Command Example
🞂 $ cat > file1.txt
• It creates file1.txt and allow us to insert content for this file.
• After inserting content you can use ctrl+c to exit the file.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
41
cat Command Example
cat -b Omits line numbers for blank space in the output
cat -E Displays a $ (dollar sign) at the end of each line
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
42
cat Command Example
🞂 $cat file.txt > newfile.txt
• Read the contents of file.txt and write them to newfile.txt, overwriting anything
newfile.txt previously contained.
• If newfile.txt does not exist, it will be created.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
43
cat Command Example
🞂 $cat file.txt >> newfile.txt
• Read the contents of file.txt and append them to the end of newfile.txt.
If newfile.txt does not exist, it will be created.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
44
cat Command Example
🞂 cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in the
terminal.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
45
cat Command Example
🞂 cat file1.txt file2.txt > combinedfile.txt
• It will concatenate the contents of file1.txt and file2.txt and write them to a new
file combinedfile.txt using the (>) operator.
• If the combinedfile.txt file doesn’t exist the command will create it otherwise it
will overwrite the file.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
46
pwd (Print working directory) Command
• It prints the current working directory name with the complete path starting
from root (/).
🞂 Syntax :
pwd [-OPTION]
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
47
who Command
• It display the users that are currently logged into your Unix computer system.
🞂 Syntax :
who [-options] [filename]
🞂 Example :
Optio
n
Use
who -b Display the time of the last system boot
who -H Print a line of column headings
who -q
Displays all login names, and a count of all logged-on
users
who -a Display all details of current logged in user
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
48
who Command Example
who -b Display the time of the last system boot
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
49
who Command Example
who -H Print a line of column headings
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
50
who Command Example
who -q
Displays all login names, and a count of all logged-on
users
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
51
who Command Example
who -a Display all details of current logged in user
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
52
whoami Command
• This command prints the username associated with the current effective user
ID.
🞂 Syntax :
whoami [-OPTION]
🞂 Example :
Option Use
whoami --help Display a help message, and exit
whoami --
version
Display version information, and exit
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
53
whoami Command Example
whoami --help Display a help message, and exit
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
54
whoami Command Example
whoami --
version
Display version information, and exit
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
55
uname (unix name) Command
• Print information about the current system.
🞂 Syntax :
uname [-OPTION]
🞂 Example :
Option Use
uname -s Print the kernel name
uname -
n
Print the network node hostname
uname -v Print the kernel version
uname -
m
Print the machine hardware name
uname -
o
Print the operating system
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
56
uname Command Example
uname -
s
Print the kernel name
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
57
uname Command Example
uname -
n
Print the network node hostname
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
58
uname Command Example
uname -
v
Print the kernel version
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
59
uname Command Example
uname -
m
Print the machine hardware name
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
60
uname Command Example
uname -
o
Print the operating system
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
61
passwd Command
• The passwd command is used to change the password of a user account.
🞂 Syntax :
passwd [-options] [username]
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
62
mkdir Command
• This command is used to make Directories.
🞂 Syntax :
mkdir [-OPTION] DIRECTORY
🞂 Example :
Option Use
mkdir -v Print a message for each created directory
mkdir -p No error if existing, make parent directories as needed
mkdir -
m
To control the permissions of new directories
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
63
mkdir Command Example
mkdir -v Print a message for each created directory
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
64
mkdir Command Example
mkdir -p No error if existing, make parent directories as needed
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
65
mkdir Command Example
mkdir -m To control the permissions of new directories
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
66
rmdir Command
• This command removes empty directories from your filesystem.
🞂 Syntax :
rmdir [-OPTION] DIRECTORY
🞂 Example :
Option Use
rmdir -p
Remove directory and its ancestors… e.g., ‘rmdir -p
a/b/c’ is similar to ‘rmdir a/b/c a/b a’
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
67
rmdir Command Example
rmdir -
p
Remove directory and its ancestors…
e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
68
cp(copy) Command
• This command is used to copy files and directories.
🞂 Syntax :
cp [option] source destination/directory
🞂 Example :
Option Use
cp -i Interactive - ask before overwrite
cp -f Force copy by removing the destination file if needed
cp -n Do not overwrite an existing file
cp -u Update - copy when source is newer than destination
cp -s Make symbolic links instead of copying
cp -R Copy directories recursively
cp -v Print informative messages
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
69
cp Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
70
cp Command Example
cp -i Interactive - ask before overwrite
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
71
cp Command Example
cp -v Print informative messages
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
72
cp Command Example
cp -s Make symbolic links instead of copying
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
73
mv(move) Command
• mv command is used to move files and directories.
🞂 Syntax :
mv [-options] source dest
🞂 Example :
Optio
n
Use
mv -i Interactive prompt before overwrite
mv -f Force move by overwriting destination file without prompt
mv -n Never overwrite any existing file
mv -u Update - move when source is newer than destination
mv -v Print informative messages
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
74
mv Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
75
rm(remove) Command
• The ‘rm’ command is used to delete files and directories.
🞂 Syntax :
rm [-OPTION] Filename
🞂 Example :
Optio
n
Use
rm -i Prompt before every removal
rm -d Delete a empty directory
rm -r Remove directories and their contents recursively
rm -f To remove the file forcefully
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
76
rm Command Example
rm -i Prompt before every removal
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
77
cut Command
• The cut command extracts a given number of characters or columns from a file.
🞂 Syntax :
cut [-options] [file]
🞂 Example :
Optio
n
Use
cut -c
Select only the characters from each line as specified in
LIST
cut -b Select only the bytes from each line as specified in LIST
cut -f
Cuts the input file using list of field. The default field to be
used TAB. The default behavior can be overwritten by
use of -d option
cut -d
Specifies a delimiter to by used as a field. Default field is
TAB and this option overwrites this default behavior
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
78
cut Command Example
cut -c
Select only the characters from each line as specified
in LIST
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
79
cut Command Example
cut -b Select only the bytes from each line as specified in LIST
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
80
cut Command Example
cut -f
Cuts the input file using list of field. The default field to be used TAB. The
default behavior can be overwritten by use of -d option
cut -d
Specifies a delimiter to by used as a field. Default field is TAB and this
option overwrites this default behavior
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
81
paste Command
• The paste command displays the corresponding lines of multiple files side-by-
side.
🞂 Syntax :
paste [-options] [file]
🞂 Example :
Optio
n
Use
paste -
d
Reuse characters from LIST instead of tabs
paste -
s
Paste one file at a time instead of in parallel
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
82
paste Command Example
paste -
d
Reuse characters from LIST instead
of tabs
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
83
paste Command Example
paste
-s
Paste one file at a time instead of in
parallel
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
84
more Command
• The more command is a command line utility for viewing the contents of a file
or files once screen at a time.
🞂 Syntax :
more [-options] [file]
🞂 Example :
Option Use
more -c Clear screen before displaying
more -
number
To Specify how many lines are printed in the screen
for a given file
more -s Doesn’t display extra blank lines
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
85
more Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
86
more Command Example
more -
number
To Specify how many lines are printed in the
screen for a given file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
87
more Command Example
more -c Clear screen before displaying
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
88
cmp Command
• cmp command in Linux/UNIX is used to compare the two files byte by byte and
helps you to find out whether the two files are identical or not.
• If a difference is found, it reports the byte and line number where the first
difference is found.
• If no differences are found, by default, cmp returns no output.
🞂 Syntax :
• cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
89
cmp Command Example
Option Use
cmp -b Print differing bytes
cmp -i Skip a particular number of initial bytes from both the files
cmp -s
Do not print anything; only return an exit status indicating
whether the files differ
cmp -n Compare at most LIMIT bytes
cmp -l Print byte position and byte value for all differing bytes
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
90
cmp Command Example
cmp -b Print differing bytes
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
91
cmp Command Example
cmp -i
Skip a particular number of initial bytes from both the
files
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
92
cmp Command Example
cmp -s
Do not print anything; only return an exit status
indicating whether the files differ
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
93
cmp Command Example
cmp -n Compare at most LIMIT bytes
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
94
cmp Command Example
cmp -l
Print byte position and byte value for all differing
bytes
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
95
comm Command
• Compare two sorted files line by line.
🞂 Syntax :
comm [OPTION]... FILE1 FILE2
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
96
comm Command Example
Option Use
comm -
1
Suppress column 1 (lines unique to FILE1)
comm -
2
Suppress column 2 (lines unique to FILE2)
comm -
3
Suppress column 3 (lines that appear in both files)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
97
diff(difference) Command
• This command is used to display the differences in the files by comparing the
files line by line
• diff analyzes two files and prints the lines that are different. Essentially, it
outputs a set of instructions for how to change one file to make it identical to
the second file.
🞂 Syntax :
diff [options] File1 File2
🞂 Example :
Option Use
diff -b Ignores spacing differences
diff -i Ignores case
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
98
diff Command Example
Special symbols are:
• a : add
• c : change
• d : delete
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
99
chmod(change mode) Command
• chmod is used to change the permissions of files or directories.
🞂 Syntax :
chmod [reference][operator][mode] file...
🞂 Example :
Referenc
e
Clas
s
Description
u owner file's owner
g group users who are members of the file's group
o
other
s
users who are neither the file's owner nor
members of the file's group
a all All three of the above
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
100
chmod(change mode) Command
Operator Description
+ Adds the specified modes to the specified classes
-
Removes the specified modes from the specified
classes
=
The modes specified are to be made the exact modes
for the specified classes
Permissi
on
Description
r Permission to read the file
w Permission to write (or delete) the file
x
Permission to execute the file, or, in the case of a
directory, search it
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
101
chmod Command Example
u owner
g group
o others
a all
r (read) 4
w (write) 2
x (execute) 1
no
permissions
0
• Each write, read, and execute permissions have following number
value:
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
102
chmod Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
103
chown(change owner) Command
• The chown command changes ownership of files and directories in a Linux
filesystem.
🞂 Syntax :
chown [OPTIONS] USER[:GROUP] FILE(s)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
104
chown Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
105
chgrp(change group) Command
• The chgrp command is used to change group ownership of a file/directory.
🞂 Syntax :
chgrp [OPTION]… GROUP FILE/DIR…
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
106
chgrp Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
107
chgrp Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
108
file Command
• The file command is used to determine a file's type.
🞂 Syntax :
file [OPTIONS] file1 file2 …
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
109
file Command Example
file -i
To view the mime type of a file rather than the human
readable format
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
110
finger Command
• finger looks up and displays information about system users.
🞂 Syntax :
finger [-option] [username]
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
111
finger Command Example
finger -
m
Match arguments only on user name (not first or last
name)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
112
finger Command Example
finger -l Force long output format
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
113
sleep Command
• The sleep command is used to delay for a specified amount of time.
🞂 Syntax :
sleep NUMBER[SUFFIX]...
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
114
sleep Command Example
• s for seconds; this is a default one if you don’t specify any letter after the
integer.
• m for minutes.
• h for hours.
• d for days.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
115
ps Command
• Reports a snapshot of the status of currently running processes.
🞂 Syntax :
ps [option]
🞂 Example :
Optio
n
Use
ps -e
Display every active process on a Linux system in
generic (Unix/Linux) format
ps -x View all processes owned by you
ps -f To provide more information on processes
ps -u Filter processes by its user
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
116
ps Command Example
ps -e
Display every active process on a Linux system in
generic (Unix/Linux) format
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
117
ps Command Example
ps -x View all processes owned by you
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
118
ps Command Example
ps -f To provide more information on processes
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
119
kill Command
• It is used to terminate processes manually.
• kill command sends a signal to a process which terminates the process.
• If the user doesn’t specify any signal which is to be sent along with kill
command then default TERM signal is sent that terminates the process..
🞂 Syntax :
kill [option] PID
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
120
kill Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
121
kill Command Example
Kill -l To display all the available signals
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
122
wc Command
• It s used to find out number of newline count, word count, byte and characters
count in a files specified by the file arguments.
🞂 Syntax :
wc [options] filenames
Optio
n
Use
wc -l Prints the number of lines in a file
wc -w Prints the number of words in a file
wc -c Displays the count of bytes in a file
wc -L Prints only the length of the longest line in a file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
123
wc Command Example
wc -L Prints only the length of the longest line in a file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
124
wc Command Example
wc -l Prints the number of lines in a file
wc -w Prints the number of words in a file
wc -c Displays the count of bytes in a file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
125
ln Command
• ln creates links between files.
• ln creates hard links by default, or symbolic links if the -s (--symbolic) option is
specified. When creating hard links, each TARGET must exist.
🞂 Syntax :
ln [OPTION]… [-T] TARGET LINK_NAME
Optio
n
Use
ln -f If the destination file or files already exist, overwrite them
ln -i Prompt the user before overwriting destination files
ln -s Make symbolic links instead of hard links
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
126
ln Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
127
ln Command Example
f2.txt
(hello good morning)
f6.txt
(hello good
morning)
ln -s Make symbolic links instead of hard links
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
128
nl Command
• nl command numbers the lines in a file.
🞂 Syntax :
nl [OPTION]... [FILE]...
🞂 Example :
Optio
n
Use
nl -i Line number increment at each line
nl -s Add STRING after (possible) line number
nl -w Use NUMBER columns for line numbers
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
129
nl Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
130
nl Command Example
nl -i Line number increment at each line
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
131
nl Command Example
nl -s Add STRING after (possible) line number
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
132
nl Command Example
nl -w Use NUMBER columns for line numbers
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
133
head Command
• head makes it easy to output the first part (10 lines by default) of files.
🞂 Syntax :
head [OPTION]... [FILE]...
🞂 Example :
Optio
n
Use
head -
n
Print the first n lines instead of the first 10; with the
leading '-', print all but the last n lines of each file
head -c
Print the first n bytes of each file; with a leading '-', print
all but the last n bytes of each file
head -
q
Never print headers identifying file names
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
134
head Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
135
head Command Example
head -
n
Print the first n lines instead of the first 10; with the
leading '-', print all but the last n lines of each file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
136
head Command Example
head -
c
Print the first n bytes of each file; with a leading '-',
print all but the last n bytes of each file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
137
head Command Example
head -
q
Never print headers identifying file names
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
138
tail Command
• tail is a command which prints the last few number of lines (10 lines by default)
of a certain file, then terminates.
🞂 Syntax :
tail [OPTION]... [FILE]...
Optio
n
Use
tail -n Output the last num lines, instead of the default (10)
tail -c Output the last num bytes of each file
tail -q Never output headers
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
139
tail Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
140
tail Command Example
tail -n Output the last num lines, instead of the default (10)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
141
tail Command Example
tail -c Output the last num bytes of each file
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
142
sort Command
• sort command is used to sort a file, arranging the records in a particular order.
• By default, the sort command sorts file assuming the contents are ASCII. Using
options in sort command, it can also be used to sort numerically.
🞂 Syntax : sort [OPTION]... [FILE]...
Option Use
sort -c To check if the file given is already sorted or not
sort -r Reverse the result of comparisons
sort -n Compare according to string numerical value
sort -nr To sort a file with numeric data in reverse order
sort -k Sorting a table on the basis of any column
sort -b Ignore leading blanks
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
143
sort Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
144
sort Command Example
sort -c To check if the file given is already sorted or not
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
145
sort Command Example
sort -r Reverse the result of comparisons
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
146
sort Command Example
sort -n Compare according to string numerical value
sort -nr To sort a file with numeric data in reverse order
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
147
sort Command Example
sort -k Sorting a table on the basis of any column
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
148
find Command
• find command searches for files in a directory hierarchy.
🞂 Syntax :
find [option] [path...] [expression]
Option Use
find -name
filename
Search for files that are specified by
‘filename’
find -newer
filename
Search for files that were modified/created
after ‘filename’
find -user name
Search for files owned by user name or ID
‘name’
find -size +N/-N
Search for files of ‘N’ blocks; ‘N’ followed by
‘c’ can be used to measure size in characters
find -empty Search for empty files and directories
find -perm octal Search for the file if permission is ‘octal’
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
149
find Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
150
find Command Example
find -newer
filename
Search for files that were
modified/created after ‘filename’
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
151
find Command Example
find -user name
Search for files owned by user name or ID
‘name’
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
152
find Command Example
find -size +N/-N
Search for files of ‘N’ blocks; ‘N’ followed
by ‘c’ can be used to measure size in
characters
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
153
find Command Example
find -
empty
Search for empty files and directories
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
154
uniq Command
• uniq reports or filters out repeated lines in a file.
• It can remove duplicates, show a count of occurrences, show only repeated
lines, ignore certain characters and compare on specific fields.
🞂 Syntax :
uniq [OPTION]... [INPUT [OUTPUT]]
Optio
n
Use
uniq -u Prints only unique lines
uniq -d Only print duplicated lines
uniq -D Print all duplicate lines
uniq -c
Prefix lines with a number representing how many times
they occurred
uniq -i Ignore case when comparing
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
155
uniq Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
156
uniq Command Example
uniq -u Prints only unique lines
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
157
uniq Command Example
uniq -d Only print duplicated lines
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
158
grep Command
• The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern.
• The pattern that is searched in the file is referred to as the regular expression.
• grep stands for globally search for regular expression and print out.
🞂 Syntax :
grep [options] pattern [files]
Optio
n
Use
grep -c Prints only a count of the lines that match a pattern
grep -h
Display the matched lines, but do not display the
filenames
grep -l Displays list of a filenames only
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
159
grep Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
160
grep Command Example
grep -c
Prints only a count of the lines that match a
pattern
grep -h
Display the matched lines, but do not display the
filenames
grep -l Displays list of a filenames only
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
161
grep Command Example
grep -n Display the matched lines and their line numbers
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
162
grep Command Example
grep -v
This prints out all the lines that do not matches
the pattern
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
163
grep Command Example
grep -w Match whole word
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
164
grep Command Example
grep -o Print only the matched parts of a matching line
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
165
grep Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
166
pipe (|) Command
• It redirects the command STDOUT or standard output into the given next
command STDIN or standard input.
• In short, the output of each process directly as input to the next one like a
pipeline.
• The symbol '|' denotes a pipe.
• Pipes help you mash-up two or more commands at the same time and run them
consecutively.
🞂 Syntax :
command_1 | command_2 | command_3 | .... | command_N…
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
167
pipe Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
168
pipe Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
169
pipe Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
170
tr(translate) Command
• The tr command in UNIX is a command line utility for translating or deleting
characters.
• It supports a range of transformations including uppercase to lowercase,
squeezing repeating characters, deleting specific characters and basic find and
replace.
• It can be used with UNIX pipes to support more complex translation.
• tr stands for translate.
🞂 Syntax :
tr [OPTION] SET1 [SET2]
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
171
tr(translate) Command
🞂 POSIX Character set supported by tr command :
• [:digit:] Only the digits 0 to 9.
• [:alnum:] Any alphanumeric character.
• [:alpha:] Any alpha character A to Z or a to z.
• [:blank:] Space and TAB characters only.
• [:xdigit:] Hexadecimal notation 0-9, A-F, a-f.
• [:upper:] Any alpha character A to Z.
• [:lower:] Any alpha character a to z..
Option Use
tr -s
Replaces repeated characters listed in the set1 with single
occurrence
tr -d Delete characters in string1 from the input
tr -c
complements the set of characters in string. i.e., operations apply
to characters not in the given set
tr -cd Remove all characters except digits
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
172
tr Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
173
tr Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
174
tr Command Example
tr -s
Replaces repeated characters listed in the set1 with single
occurrence
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
175
tr Command Example
tr -d Delete characters in string1 from the input
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
176
tr Command Example
tr -c
complements the set of characters in string.
i.e., operations apply to characters not in the given set
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
177
tr Command Example
tr -cd Remove all characters except digits
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
178
history Command
• history command is used to view the previously executed command.
🞂 Syntax :
history
🞂 Example :
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
179
history Command Example
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
180
write Command
• write sends a message to another user.
🞂 Syntax :
write user [ttyname]
🞂 Example
Optio
n
Use
user The user to write to
tty
The specific terminal to write to, if the user is logged in to
more than one session
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
181
write Command Example
• Open first
terminal.
• Open Second terminal then execute command on
first terminal.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
182
wall Command
• wall send a message to everybody’s terminal.
• wall sends a message to everybody logged in with their mesg permission set to
yes.
🞂 Syntax :
wall [-n] [-t TIMEOUT] [file]
🞂 Example
Optio
n
Use
wall -n --nobanner Suppress banner
wall -t
--timeout TIMEOUT Write timeout to terminals in
seconds. TIMEOUT must be positive integer. Default
value is 300 seconds, which is a legacy from time when
people ran terminals over modem lines.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
183
wall Command Example
• Open four different terminal, execute command on first terminal, message
will display on everybody’s terminal.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
184
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
185
Shell Script
🞂 Script is a sequence of commands written as plain text and run by an
interpreter (shell).
🞂 A shell script is a computer program designed to be run by the Unix/Linux shell
which could be one of the following:
⮩ The Bourne Shell
⮩ The C Shell
⮩ The Korn Shell
⮩ The GNU Bourne-Again Shell
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
186
Shell Script - Cont.…
🞂 The prompt, $, which is called the command prompt, is issued by the shell.
While the prompt is displayed, you can type a command.
🞂 The shell interprets commands the user types and manages their execution.
🞂 A shell is a command-line interpreter and typical operations performed by shell
scripts include file manipulation, program execution, and printing text.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
187
Steps to write and execute a shell script
1. Create a file using a vi editor(or any other editor like gedit).
2. Name script file with extension .sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
188
Shell Variables
🞂 The user variable name can be any sequence of letters, digits, and the
underscore character, but the first character must be a letter.
🞂 To assign a value to a variable:
number=25
name=“abc"
🞂 There cannot be any space before or after the “=“
🞂 Internally, all values are stored as strings.
🞂 To use a variable, precede the name with a “$”.
num=3
a=$num
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
189
User input in Shell Script
🞂 The read command allows a user to provide the runtime input.
🞂 Shell allows to prompt for user input.
🞂 Syntax:
read varname [more vars]
• Value entered by user is assigned to varname
🞂 Run shell script :
bash filename.sh
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
190
Operators
Arithmetic Operators
Bash Operator Operator Description
+ + Addition
- - Subtraction
* * Multiplication
/ / Division
% % Modulus
Assignment Operators
= =
Assigns right operand
in left operand i.e. a=$b
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
191
Operators
Relational Operators – for Numeric
Bash Operator Operator Description
-eq == Equal
-ne != Not equal
-gt > Greater than
-ge >= Greater than or equal
-lt < Less than
-le <= Less than or equal
-z == null Is null
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
192
Operators
Relational Operators – for String
Bash Operator Operator Description
= or == == Equal
!= != Not equal
str1 < str2 < str1 is less than str2
str1 > str2 > str1 is greater str2
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
193
Operators
Boolean Operators
Bash Operator Operator Description
! ! This is logical negation.
-o OR This is logical OR.
-a AND This is logical AND.
String Operators
Bash Operator Operator Description
= =
Checks if the value of two
operands are equal or not
!= !=
If values are not equal then the
condition becomes true.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
194
Arithmetic Operator Example (e.g. a=10, b=20)
Operator Description Example
+ (Addition) Adds values on either side of the operator `expr $a + $b` will give 30
- (Subtraction) Subtracts right hand operand from left hand operand `expr $a - $b` will give -10
* (Multiplication) Multiplies values on either side of the operator `expr $a * $b` will give 200
/ (Division) Divides left hand operand by right hand operand `expr $b / $a` will give 2
% (Modulus) Divides left hand operand by right hand operand and
returns remainder
`expr $b % $a` will give 0
= (Assignment) Assigns right operand in left operand a = $b assign value of b into a
== (Equality) Compares two numbers, if both are same then
returns true.
[ $a == $b ] would return false.
!= (Not Equality) Compares two numbers, if both are different then
returns true.
[ $a != $b ] would return true.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
195
Relational Operator Example (e.g. a=10, b=20)
Operator Description Example
-eq Checks if the value of two operands are equal or not; if yes, then the
condition becomes true.
[ $a -eq $b ] is
not true.
-ne Checks if the value of two operands are equal or not; if values are not
equal, then the condition becomes true.
[ $a -ne $b ] is
true.
-gt Checks if the value of left operand is greater than the value of right
operand; if yes, then the condition becomes true.
[ $a -gt $b ] is
not true.
-lt Checks if the value of left operand is less than the value of right operand; if
yes, then the condition becomes true.
[ $a -lt $b ] is
true.
-ge Checks if the value of left operand is greater than or equal to the value of
right operand; if yes, then the condition becomes true. [ $a -ge $b ] is
not true.
-le Checks if the value of left operand is less than or equal to the value of right
operand; if yes, then the condition becomes true. [ $a -le $b ] is
true.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
196
Boolean Operator Example (e.g. a=10, b=20)
Operator Description Example
! This is logical negation. This inverts a true condition into
false and vice versa.
[ ! false ] is true.
-o This is logical OR. If one of the operands is true, then the
condition becomes true.
[ $a -lt 20 -o $b -gt 100 ] is
true.
-a This is logical AND. If both the operands are true, then
the condition becomes true otherwise false.
[ $a -lt 20 -a $b -gt 100 ] is
false.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
197
String Operator Example (e.g. a=“abc”, b=“efg”)
Operator Description Example
= Checks if the value of two operands are equal or not; if yes, then the
condition becomes true.
[ $a = $b ]
is not true.
!= Checks if the value of two operands are equal or not; if values are not
equal then the condition becomes true.
[ $a != $b ] is
true.
-z Checks if the given string operand size is zero; if it is zero length, then it
returns true.
[ -z $a ]
is not true.
-n Checks if the given string operand size is non-zero; if it is nonzero length,
then it returns true.
[ -n $a ]
is not false.
str Checks if str is not the empty string; if it is empty, then it returns false. [ $a ]
is not false.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
198
expr Command
• The expr command in Unix evaluates a given expression and displays its
corresponding output.
• It is used for:
• Basic operations like addition, subtraction, multiplication, division, and
modulus on integers.
• Evaluating regular expressions, string operations like substring, length of
strings etc.
• Syntax : expr EXPRESSION
$ expr 3 + 5
output:
8
$ expr 3 *
5
output:
15
num1=10
num2=20
sum=`expr $num1 +
$num2`
echo $sum
output: 30
Backtic
k
sum=`expr $num1 +
$num2`
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
199
if Statement
• If statements use the if, then, else, and fi keywords.
• The condition goes in square brackets.
🞂 Syntax :
if [ <some test> ]
then
<commands>
fi
if [ $no -gt 100 ]
then
echo "Greater than
100"
fi
The spaces
before and after
the square
brackets
[ ] are required.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
200
if Statement
if [ $a -gt $b ]
then
echo “A is Greater than
B"
fi
if [ $a -ne $b ]
then
echo “A is not eqal to
B"
fi
if [ `expr $mid1_total + $mid2_total` -gt
50 ]
then
echo “Total Marks is Greater than 50"
fi
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
201
if…else Statement
if [control
command]
then
<statements-1>
else
<statements-2>
fi
if [ $a -eq $b ]
then
echo "a is equal to b"
else
echo "a is not equal to
b"
fi
• executes statements-1 if condition is
true
• executes statements-2 if condition is
false
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
202
Nested if-else-fi Statement
if [control command]
then
if [control command]
then
<statements>
else
<statements>
fi
else
<statements>
fi
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
203
if-else-if ladder
if [control command]
then
<statements>
elif [control command]
then
<statements>
elif [control command]
then
<statements>
else
<statements>
fi
• The word elif stands for “else if”
• It is part of the if statement and cannot be used by
itself
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
204
if-else-if ladder Example
if [ $i -eq 5 ]
then
echo "Value of i is 5"
elif [ $i -eq 10 ]
then
echo "Value of i is 10"
elif [ $i -eq 20 ]
then
echo "Value of i is 20"
elif [ $i -eq 30 ]
then
echo "Value of i is 30"
else
echo "Value of i is not equal to 5,10,20 or
30"
fi
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
205
case Structure
case $word in
pattern1) c
ommand-list1
;;
pattern2)
command-list2
;;
patternN)
command-listN
;;
*)
;;
esac
echo "Enter number:"
read num
case $num in
1)
echo "It's one!"
;;
2)
echo "It's two!"
;;
3)
echo "It's three!"
;;
*)
echo "It's something else!"
;;
esac
• Use the case statement for a decision that is based on
multiple choices.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
206
Repetition Constructs
206
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
207
while loop
Syntax:
while [ condition
]
do
command1
command2
…..
.....
done
i=1
while [ $i -le 10 ]
do
echo “$i”
i=`expr $i + 1`
done
The while construct tests for a
condition, and if true, executes
commands.
It keeps looping as long as the
condition is true.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
208
until loop
Syntax:
until [ condition ]
do
command1
command2
…..
.....
done
COUNT=1
until [ $COUNT -gt 5 ];
do
echo "Value of count is:
$COUNT"
COUNT=$(($COUNT + 1))
done
The until construct tests for a
condition, and if false, executes
commands.
It keeps looping as long as the
condition is false (opposite of while
construct)
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
209
for loop using “in” and list of values
for i in 1 2 3 4 5
do
echo "Looping ...
number $i"
done
for arg in list
do
command(s)
...
done
• Where the value of the variable arg is set to the values
provided in the list one at a time and the block of statements
executed.
• This is repeated until the list is exhausted.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
210
for loop using C like Syntax
for((i=1; i<=5; i++))
do
echo $i
done
for ((initialization; condition test; increment or
decrement))
do
//statements to be executed repeatedly
done
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
211
select loop
select variableName in choice1 choice2 ...
choiceN
do
-- Block of Commands –
done
• Select command use PS3 variable to print its prompt.
• Each word in list is printed on screen preceded by a number.
• If the line consists of the number corresponding to one of the
displayed words (from the list), then varName is set to that
word. You can use if..else.fi or case..in..esac to make a decision.
• If the line is empty, WORDS and the prompt are redisplayed.
• If EOF (end of file) is read, the command completes.
• The loop continues until a break (CTRL+C) is encountered.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
212
select loop Example
echo "Enter space shuttle to get more information : "
# set shuttle list
select shuttle in Columbia Endeavour Challenger Discovery
do
echo "$shuttle selected"
done
Output:
Enter the space shuttle to get more information :
1) Columbia
2) Endeavor
3) Challenger
4) Discovery
#? 1
Columbia selected
#?
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
213
Break and Continue Statement
• All statement inside the loop executed as long as some condition are true.
• If break placed inside the loop, when loop reach the break statement it will
terminated out from the loop.
• If continue placed inside the loop, when loop reach the continue statement it
will not execute next lines of the loop and it will go to the next iteration.
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
214
Break and Continue Statement
while [ condition ]
do
cmd-1
break
cmd-n
done
echo "done"
break statement continue statement
This iteration is over and
there are no more
iterations
This iteration is over, do
the next iteration
while [ condition ]
do
cmd-1
continue
cmd-n
done
echo "done"
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
215
echo and printf Command
echo Command printf Command
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1234
5
1234
5
Decimal:
100
Octal: 144
Hex: 64
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
216
Average of 4 numbers using bc Command
a=10
b=2
c=12
d=5
sum=$(( a + b + c + d))
avg=`echo $sum / 4 | bc –l`
printf %0.3f “$avg”
OUTPUT :
7.250
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
217
Division using bc Command
# get the first number
printf "Enter first integer number: "
read a
# get the second number
printf "Enter second integer number: "
read b
# if b is 0
if [ $b == 0 ]
then
printf "Division by 0 not allowed.n"
exit # exit the script
fi
# perform division
result=`expr "$a / $b" | bc -l`
printf "%d / %d = %.5fn" "$a" "$b"
"$result"
Prof. Maulik D Trivedi
#3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating
218
Darshan Institute of Engineering &
Technology, Rajkot
Than
k
You
Operating System (OS)
GTU # 3140702
maulik.trivedi@darshan.ac.in
9998 265 805
Computer Engineering Department
Prof. Maulik D Trivedi

More Related Content

PDF
Bozorgmeh os lab
PPTX
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
PPT
R_Language_study_forstudents_R_Material.ppt
PPT
Brief introduction to R Lecturenotes1_R .ppt
PPT
Lecture1_R.ppt
PPT
Lecture1 r
PPT
Modeling in R Programming Language for Beginers.ppt
PPT
Lecture1_R.ppt
Bozorgmeh os lab
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
R_Language_study_forstudents_R_Material.ppt
Brief introduction to R Lecturenotes1_R .ppt
Lecture1_R.ppt
Lecture1 r
Modeling in R Programming Language for Beginers.ppt
Lecture1_R.ppt

Similar to Unix Commands and Shells Scripts in OS.pptx (20)

PPT
Lecture1_R Programming Introduction1.ppt
DOCX
40 basic linux command
DOCX
40 basic linux command
PDF
Lecture1_R.pdf
PDF
Linux Commands for linux learner & engineering students
PPTX
Unix - Filters/Editors
PDF
Linux Basic commands for beginners-@OneMento
PDF
Basic linux commands
PPT
workshop_1.ppt
PPTX
PPTX
Operating System Laboratory presentation .ppt
PDF
1) List currently running jobsANS) see currently runningcommand.pdf
DOC
NOTES ON "FOXPRO"
PPTX
465330485-Unix-basic-commands-opera.pptx
PPTX
Unix-basic-commands-operatingSystem.pptx
PPTX
Linux Basics.pptx
PDF
2018-Summer-Tutorial-Intro-to-Linux.pdf
PPTX
Linux System commands Essentialsand Basics.pptx
PDF
Lecture1_R Programming Introduction1.ppt
40 basic linux command
40 basic linux command
Lecture1_R.pdf
Linux Commands for linux learner & engineering students
Unix - Filters/Editors
Linux Basic commands for beginners-@OneMento
Basic linux commands
workshop_1.ppt
Operating System Laboratory presentation .ppt
1) List currently running jobsANS) see currently runningcommand.pdf
NOTES ON "FOXPRO"
465330485-Unix-basic-commands-opera.pptx
Unix-basic-commands-operatingSystem.pptx
Linux Basics.pptx
2018-Summer-Tutorial-Intro-to-Linux.pdf
Linux System commands Essentialsand Basics.pptx
Ad

More from Vivekananda Gn (20)

PPTX
Stream Ciphers and Block Ciphers in Security.pptx
PPTX
Introduction to Network and Information Security.pptx
PPT
Network and Information Security unit2.ppt.ppt
PPT
Network and Information Security unit 1.ppt
PPT
Sorting and Searching in Data Structures.ppt
PPT
Trees Traversals Expressions in Data structures.ppt
PPT
STACKS AND QUEUES in Data Structures.ppt
PPT
Introduction to Data structures and Trees.ppt
PPTX
Virtualization concepts in Operating systems.pptx
PPTX
Security and Protection in operating systems.pptx
PPTX
Concurrency and Inter-Process communication.pptx
PPTX
Unix-Linux Operating Systems and its concepts
PPTX
Network-Administration-ITA3011 BCA VIT Vellore
PPTX
project Idea on Online food ordering system
PPT
Software Architecture and Design Details Module
PPT
memory allocation techniques in operating systems
PPT
virtual memory concepts in operating systems
PPTX
user authentication in cryptography and network security.pptx
PPTX
vector processing, pipelining - computer organization.pptx
PPTX
OS Building and Booting in Fundamentals of OS
Stream Ciphers and Block Ciphers in Security.pptx
Introduction to Network and Information Security.pptx
Network and Information Security unit2.ppt.ppt
Network and Information Security unit 1.ppt
Sorting and Searching in Data Structures.ppt
Trees Traversals Expressions in Data structures.ppt
STACKS AND QUEUES in Data Structures.ppt
Introduction to Data structures and Trees.ppt
Virtualization concepts in Operating systems.pptx
Security and Protection in operating systems.pptx
Concurrency and Inter-Process communication.pptx
Unix-Linux Operating Systems and its concepts
Network-Administration-ITA3011 BCA VIT Vellore
project Idea on Online food ordering system
Software Architecture and Design Details Module
memory allocation techniques in operating systems
virtual memory concepts in operating systems
user authentication in cryptography and network security.pptx
vector processing, pipelining - computer organization.pptx
OS Building and Booting in Fundamentals of OS
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Lecture Notes Electrical Wiring System Components
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Artificial Intelligence
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
additive manufacturing of ss316l using mig welding
Well-logging-methods_new................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Automation-in-Manufacturing-Chapter-Introduction.pdf
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Internet of Things (IOT) - A guide to understanding
CH1 Production IntroductoryConcepts.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Lecture Notes Electrical Wiring System Components
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
R24 SURVEYING LAB MANUAL for civil enggi
Model Code of Practice - Construction Work - 21102022 .pdf
Artificial Intelligence
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
additive manufacturing of ss316l using mig welding

Unix Commands and Shells Scripts in OS.pptx

  • 1. Darshan Institute of Engineering & Technology, Rajkot Unit - 9 Unix Commands maulik.trivedi@darshan.ac.in 9998 265 805 Computer Engineering Department Prof. Maulik D. Trivedi Operating System (OS) GTU # 3140702
  • 2. ✓ Loopin g Outline • What is an Operating System (OS) ? • Tasks of Operating System • What is Unix? • Unix Operating System • Unix History • Features of Unix • What is shell? • Linux Operating System • Architecture • Component of Linux System • Basic Function of Kernel • Directory Structure • Linux Vs. Windows
  • 3. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 4 cd (Change Directory) Command 🞂 The cd command is used to change the current directory (i.e., the directory in which the user is currently working) 🞂 Syntax : cd [-Options] [Directory] 🞂 Example : Option Use cd .. Change Current directory to parent directory cd ~ Move to users home directory from anywhere cd lab_1 Change from current working directory to lab_1 cd ../downloads If we are currently in /home/username/documents then we would be placed in /home/username/downloads.
  • 4. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 5 cd Command Example cd .. Change Current directory to parent directory
  • 5. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 6 cd Command Example cd ~ Move to users home directory from anywhere
  • 6. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 7 cd Command Example cd lab_1 Change from current working directory to lab_1
  • 7. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 8 cd Command Example cd ../downloads If we are currently in /home/student/documents then we would be placed in /home/student/downloads
  • 8. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 9 ls Command 🞂 List directory contents. 🞂 Syntax : ls [Options] [file|dir] 🞂 Example : Option Use ls -l To show long listing information about the file/directory ls -a List all files including hidden file starting with '.' ls -r List in reverse order ls -t Sort by time & date ls -s Sort by file size
  • 9. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 10 🞂 Directory & Files 🞂 A read, write, execute permissions for user(root), group and others respectively 🞂 A number of links for file / dir. 🞂 It specifies owner of the file / dir. 🞂 It specifies group of the file / dir. 🞂 It indicates the file / dir size in bytes. 🞂 It specifies the date and time of the last modification of the file. 🞂 It specify name of the file / dir. ls Command drwxr- xr-x own er grou p othe rs
  • 10. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 11 ls Command Example ls -l To show long listing information about the file/directory
  • 11. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 12 ls Command Example ls -a List all files including hidden file starting with '.'
  • 12. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 13 ls Command Example ls -r List in reverse order
  • 13. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 14 man Command • It is the interface used to view the system's reference manuals. 🞂 Syntax : man [command name] 🞂 Example
  • 14. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 15 echo Command 🞂 Display a line of text/string on standard output or a file. 🞂 Syntax : echo [option] [string] 🞂 Example : Option Use echo -n Do not output a trailing newline echo -e Enable interpretation of backslash escape sequences Option Use b It removes all the spaces in between the text n It creates new line from where it is used t It create horizontal tab spaces
  • 15. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 16 echo Command Example echo -n Do not output a trailing newline
  • 16. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 17 echo Command Example b It removes all the spaces in between the text n It creates new line from where it is used t It create horizontal tab spaces echo -e Enable interpretation of backslash escape sequences
  • 17. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 18 cal Command 🞂 Displays a simple, formatted calendar in your terminal. 🞂 Syntax : cal [options] [[[day] month] year] 🞂 Example : Option Use cal -1 Display single month output. (This is the default.) cal -3 Display three months spanning the date. cal -s Display Sunday as the first day of the week. cal -m Display Monday as the first day of the week. cal -j Use day-of-year numbering for all calendars. These are also called ordinal days. Ordinal days range from 1 to 366. cal –y Display a calendar for the whole year
  • 18. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 19 cal Command Example Cal or cal -1 Display single month output. (This is the default.)
  • 19. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 20 cal Command Example cal -3 Display three months spanning the date.
  • 20. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 21 cal Command Example cal -s Display Sunday as the first day of the week.
  • 21. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 22 cal Command Example cal -m Display Monday as the first day of the week.
  • 22. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 23 cal Command Example cal -j Use day-of-year numbering for all calendars. These are also called ordinal days. Ordinal days range from 1 to 366.
  • 23. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 24 cal Command Example cal –y Display a calendar for the whole year
  • 24. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 25 date Command 🞂 Print or set the system date and time. 🞂 Syntax : date [OPTION]... [+FORMAT] 🞂 Example : Option Use date +%a The abbreviated weekday name (e.g., Sun) date +%A The full weekday name (e.g., Sunday) date +%b The abbreviated month name (e.g., Jan) date +%B Locale's full month name (e.g., January) date +%C The current century; like %Y, except omit last two digits (e.g., 20) date +%w day of week (0..6); 0 is Sunday
  • 25. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 26 date Command Option Use date +%d Display the day of the month date +%m Displays the month of year (01 to 12) date +%y Displays last two digits of the year(00 to 99) date +%Y Display four-digit year. date +%T Display the time in 24 hour format as HH:MM:SS date +%H Display the hour date +%M Display the minute date +%S Display the seconds date +%V ISO week number, with Monday as first day of week (01..53) date +%P locale's equivalent of either AM or PM
  • 26. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 27 date Command Example date +%a The abbreviated weekday name (e.g., Sun) date +%A The full weekday name (e.g., Sunday)
  • 27. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 28 date Command Example date +%b The abbreviated month name (e.g., Jan) date +%B Locale's full month name (e.g., January)
  • 28. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 29 date Command Example date +%c Full date with IST timing date +%C The current century; like %Y, except omit last two digits (e.g., 20)
  • 29. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 30 date Command Example date +%d Display the day of the month date +%m Displays the month of year (01 to 12) date +%y Displays last two digits of the year(00 to 99)
  • 30. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 31 date Command Example date +%y Displays last two digits of the year(00 to 99) date +%Y Display four-digit year.
  • 31. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 32 date Command Example date +%T Display the time in 24 hour format as HH:MM:SS date +%H Display the hour date +%M Display the minute date +%S Display the seconds
  • 32. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 33 date Command Example date +%V ISO week number, with Monday as first day of week (01..53)
  • 33. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 34 date Command Example date +%P locale's equivalent of either AM or PM
  • 34. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 35 clear Command 🞂 Clear the terminal screen. 🞂 If you take a detailed look after running the clear command, you'll find that it doesn't really clear the terminal. The tool just shifts the text upwards, out of the viewable area. 🞂 Syntax : clear
  • 35. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 36 clear Command Example
  • 36. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 37 cat Command • It is used to create, display and concatenate file contents. 🞂 Syntax : cat [OPTION] [FILE] 🞂 Example : Option Use cat -b Omits line numbers for blank space in the output cat -E Displays a $ (dollar sign) at the end of each line cat -n Line numbers for all the output lines cat -s Suppress repeated empty output lines cat -T Displays the tab characters as ^I in the output
  • 37. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 38 cat Command 🞂 Example : $ cat > file1.txt 🞂 It creates file1.txt and allow us to insert content for this file. 🞂 After inserting content you can use ctrl+c to exit the file. $cat file.txt > newfile.txt 🞂 Read the contents of file.txt and write them to newfile.txt, overwriting anything newfile.txt previously contained. If newfile.txt does not exist, it will be created. $cat file.txt >> newfile.txt 🞂 Read the contents of file.txt and append them to the end of newfile.txt. If newfile.txt does not exist, it will be created.
  • 38. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 39 cat Command 🞂 Example : cat file1.txt file2.txt • It will read the contents of file1.txt and file2.txt and display the result in the terminal. cat file1.txt file2.txt > combinedfile.txt • It will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the (>) operator. • If the combinedfile.txt file doesn’t exist the command will create it. Otherwise it will overwrite the file.
  • 39. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 40 cat Command Example 🞂 $ cat > file1.txt • It creates file1.txt and allow us to insert content for this file. • After inserting content you can use ctrl+c to exit the file.
  • 40. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 41 cat Command Example cat -b Omits line numbers for blank space in the output cat -E Displays a $ (dollar sign) at the end of each line
  • 41. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 42 cat Command Example 🞂 $cat file.txt > newfile.txt • Read the contents of file.txt and write them to newfile.txt, overwriting anything newfile.txt previously contained. • If newfile.txt does not exist, it will be created.
  • 42. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 43 cat Command Example 🞂 $cat file.txt >> newfile.txt • Read the contents of file.txt and append them to the end of newfile.txt. If newfile.txt does not exist, it will be created.
  • 43. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 44 cat Command Example 🞂 cat file1.txt file2.txt • It will read the contents of file1.txt and file2.txt and display the result in the terminal.
  • 44. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 45 cat Command Example 🞂 cat file1.txt file2.txt > combinedfile.txt • It will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the (>) operator. • If the combinedfile.txt file doesn’t exist the command will create it otherwise it will overwrite the file.
  • 45. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 46 pwd (Print working directory) Command • It prints the current working directory name with the complete path starting from root (/). 🞂 Syntax : pwd [-OPTION] 🞂 Example :
  • 46. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 47 who Command • It display the users that are currently logged into your Unix computer system. 🞂 Syntax : who [-options] [filename] 🞂 Example : Optio n Use who -b Display the time of the last system boot who -H Print a line of column headings who -q Displays all login names, and a count of all logged-on users who -a Display all details of current logged in user
  • 47. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 48 who Command Example who -b Display the time of the last system boot
  • 48. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 49 who Command Example who -H Print a line of column headings
  • 49. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 50 who Command Example who -q Displays all login names, and a count of all logged-on users
  • 50. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 51 who Command Example who -a Display all details of current logged in user
  • 51. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 52 whoami Command • This command prints the username associated with the current effective user ID. 🞂 Syntax : whoami [-OPTION] 🞂 Example : Option Use whoami --help Display a help message, and exit whoami -- version Display version information, and exit
  • 52. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 53 whoami Command Example whoami --help Display a help message, and exit
  • 53. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 54 whoami Command Example whoami -- version Display version information, and exit
  • 54. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 55 uname (unix name) Command • Print information about the current system. 🞂 Syntax : uname [-OPTION] 🞂 Example : Option Use uname -s Print the kernel name uname - n Print the network node hostname uname -v Print the kernel version uname - m Print the machine hardware name uname - o Print the operating system
  • 55. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 56 uname Command Example uname - s Print the kernel name
  • 56. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 57 uname Command Example uname - n Print the network node hostname
  • 57. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 58 uname Command Example uname - v Print the kernel version
  • 58. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 59 uname Command Example uname - m Print the machine hardware name
  • 59. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 60 uname Command Example uname - o Print the operating system
  • 60. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 61 passwd Command • The passwd command is used to change the password of a user account. 🞂 Syntax : passwd [-options] [username] 🞂 Example :
  • 61. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 62 mkdir Command • This command is used to make Directories. 🞂 Syntax : mkdir [-OPTION] DIRECTORY 🞂 Example : Option Use mkdir -v Print a message for each created directory mkdir -p No error if existing, make parent directories as needed mkdir - m To control the permissions of new directories
  • 62. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 63 mkdir Command Example mkdir -v Print a message for each created directory
  • 63. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 64 mkdir Command Example mkdir -p No error if existing, make parent directories as needed
  • 64. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 65 mkdir Command Example mkdir -m To control the permissions of new directories
  • 65. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 66 rmdir Command • This command removes empty directories from your filesystem. 🞂 Syntax : rmdir [-OPTION] DIRECTORY 🞂 Example : Option Use rmdir -p Remove directory and its ancestors… e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’
  • 66. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 67 rmdir Command Example rmdir - p Remove directory and its ancestors… e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’
  • 67. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 68 cp(copy) Command • This command is used to copy files and directories. 🞂 Syntax : cp [option] source destination/directory 🞂 Example : Option Use cp -i Interactive - ask before overwrite cp -f Force copy by removing the destination file if needed cp -n Do not overwrite an existing file cp -u Update - copy when source is newer than destination cp -s Make symbolic links instead of copying cp -R Copy directories recursively cp -v Print informative messages
  • 68. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 69 cp Command Example
  • 69. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 70 cp Command Example cp -i Interactive - ask before overwrite
  • 70. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 71 cp Command Example cp -v Print informative messages
  • 71. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 72 cp Command Example cp -s Make symbolic links instead of copying
  • 72. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 73 mv(move) Command • mv command is used to move files and directories. 🞂 Syntax : mv [-options] source dest 🞂 Example : Optio n Use mv -i Interactive prompt before overwrite mv -f Force move by overwriting destination file without prompt mv -n Never overwrite any existing file mv -u Update - move when source is newer than destination mv -v Print informative messages
  • 73. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 74 mv Command Example
  • 74. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 75 rm(remove) Command • The ‘rm’ command is used to delete files and directories. 🞂 Syntax : rm [-OPTION] Filename 🞂 Example : Optio n Use rm -i Prompt before every removal rm -d Delete a empty directory rm -r Remove directories and their contents recursively rm -f To remove the file forcefully
  • 75. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 76 rm Command Example rm -i Prompt before every removal
  • 76. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 77 cut Command • The cut command extracts a given number of characters or columns from a file. 🞂 Syntax : cut [-options] [file] 🞂 Example : Optio n Use cut -c Select only the characters from each line as specified in LIST cut -b Select only the bytes from each line as specified in LIST cut -f Cuts the input file using list of field. The default field to be used TAB. The default behavior can be overwritten by use of -d option cut -d Specifies a delimiter to by used as a field. Default field is TAB and this option overwrites this default behavior
  • 77. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 78 cut Command Example cut -c Select only the characters from each line as specified in LIST
  • 78. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 79 cut Command Example cut -b Select only the bytes from each line as specified in LIST
  • 79. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 80 cut Command Example cut -f Cuts the input file using list of field. The default field to be used TAB. The default behavior can be overwritten by use of -d option cut -d Specifies a delimiter to by used as a field. Default field is TAB and this option overwrites this default behavior
  • 80. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 81 paste Command • The paste command displays the corresponding lines of multiple files side-by- side. 🞂 Syntax : paste [-options] [file] 🞂 Example : Optio n Use paste - d Reuse characters from LIST instead of tabs paste - s Paste one file at a time instead of in parallel
  • 81. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 82 paste Command Example paste - d Reuse characters from LIST instead of tabs
  • 82. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 83 paste Command Example paste -s Paste one file at a time instead of in parallel
  • 83. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 84 more Command • The more command is a command line utility for viewing the contents of a file or files once screen at a time. 🞂 Syntax : more [-options] [file] 🞂 Example : Option Use more -c Clear screen before displaying more - number To Specify how many lines are printed in the screen for a given file more -s Doesn’t display extra blank lines
  • 84. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 85 more Command Example
  • 85. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 86 more Command Example more - number To Specify how many lines are printed in the screen for a given file
  • 86. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 87 more Command Example more -c Clear screen before displaying
  • 87. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 88 cmp Command • cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. • If a difference is found, it reports the byte and line number where the first difference is found. • If no differences are found, by default, cmp returns no output. 🞂 Syntax : • cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
  • 88. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 89 cmp Command Example Option Use cmp -b Print differing bytes cmp -i Skip a particular number of initial bytes from both the files cmp -s Do not print anything; only return an exit status indicating whether the files differ cmp -n Compare at most LIMIT bytes cmp -l Print byte position and byte value for all differing bytes
  • 89. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 90 cmp Command Example cmp -b Print differing bytes
  • 90. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 91 cmp Command Example cmp -i Skip a particular number of initial bytes from both the files
  • 91. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 92 cmp Command Example cmp -s Do not print anything; only return an exit status indicating whether the files differ
  • 92. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 93 cmp Command Example cmp -n Compare at most LIMIT bytes
  • 93. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 94 cmp Command Example cmp -l Print byte position and byte value for all differing bytes
  • 94. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 95 comm Command • Compare two sorted files line by line. 🞂 Syntax : comm [OPTION]... FILE1 FILE2 🞂 Example :
  • 95. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 96 comm Command Example Option Use comm - 1 Suppress column 1 (lines unique to FILE1) comm - 2 Suppress column 2 (lines unique to FILE2) comm - 3 Suppress column 3 (lines that appear in both files)
  • 96. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 97 diff(difference) Command • This command is used to display the differences in the files by comparing the files line by line • diff analyzes two files and prints the lines that are different. Essentially, it outputs a set of instructions for how to change one file to make it identical to the second file. 🞂 Syntax : diff [options] File1 File2 🞂 Example : Option Use diff -b Ignores spacing differences diff -i Ignores case
  • 97. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 98 diff Command Example Special symbols are: • a : add • c : change • d : delete
  • 98. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 99 chmod(change mode) Command • chmod is used to change the permissions of files or directories. 🞂 Syntax : chmod [reference][operator][mode] file... 🞂 Example : Referenc e Clas s Description u owner file's owner g group users who are members of the file's group o other s users who are neither the file's owner nor members of the file's group a all All three of the above
  • 99. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 100 chmod(change mode) Command Operator Description + Adds the specified modes to the specified classes - Removes the specified modes from the specified classes = The modes specified are to be made the exact modes for the specified classes Permissi on Description r Permission to read the file w Permission to write (or delete) the file x Permission to execute the file, or, in the case of a directory, search it
  • 100. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 101 chmod Command Example u owner g group o others a all r (read) 4 w (write) 2 x (execute) 1 no permissions 0 • Each write, read, and execute permissions have following number value:
  • 101. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 102 chmod Command Example
  • 102. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 103 chown(change owner) Command • The chown command changes ownership of files and directories in a Linux filesystem. 🞂 Syntax : chown [OPTIONS] USER[:GROUP] FILE(s)
  • 103. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 104 chown Command Example
  • 104. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 105 chgrp(change group) Command • The chgrp command is used to change group ownership of a file/directory. 🞂 Syntax : chgrp [OPTION]… GROUP FILE/DIR…
  • 105. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 106 chgrp Command Example
  • 106. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 107 chgrp Command Example
  • 107. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 108 file Command • The file command is used to determine a file's type. 🞂 Syntax : file [OPTIONS] file1 file2 … 🞂 Example :
  • 108. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 109 file Command Example file -i To view the mime type of a file rather than the human readable format
  • 109. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 110 finger Command • finger looks up and displays information about system users. 🞂 Syntax : finger [-option] [username] 🞂 Example :
  • 110. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 111 finger Command Example finger - m Match arguments only on user name (not first or last name)
  • 111. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 112 finger Command Example finger -l Force long output format
  • 112. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 113 sleep Command • The sleep command is used to delay for a specified amount of time. 🞂 Syntax : sleep NUMBER[SUFFIX]... 🞂 Example :
  • 113. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 114 sleep Command Example • s for seconds; this is a default one if you don’t specify any letter after the integer. • m for minutes. • h for hours. • d for days.
  • 114. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 115 ps Command • Reports a snapshot of the status of currently running processes. 🞂 Syntax : ps [option] 🞂 Example : Optio n Use ps -e Display every active process on a Linux system in generic (Unix/Linux) format ps -x View all processes owned by you ps -f To provide more information on processes ps -u Filter processes by its user
  • 115. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 116 ps Command Example ps -e Display every active process on a Linux system in generic (Unix/Linux) format
  • 116. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 117 ps Command Example ps -x View all processes owned by you
  • 117. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 118 ps Command Example ps -f To provide more information on processes
  • 118. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 119 kill Command • It is used to terminate processes manually. • kill command sends a signal to a process which terminates the process. • If the user doesn’t specify any signal which is to be sent along with kill command then default TERM signal is sent that terminates the process.. 🞂 Syntax : kill [option] PID
  • 119. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 120 kill Command Example
  • 120. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 121 kill Command Example Kill -l To display all the available signals
  • 121. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 122 wc Command • It s used to find out number of newline count, word count, byte and characters count in a files specified by the file arguments. 🞂 Syntax : wc [options] filenames Optio n Use wc -l Prints the number of lines in a file wc -w Prints the number of words in a file wc -c Displays the count of bytes in a file wc -L Prints only the length of the longest line in a file
  • 122. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 123 wc Command Example wc -L Prints only the length of the longest line in a file
  • 123. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 124 wc Command Example wc -l Prints the number of lines in a file wc -w Prints the number of words in a file wc -c Displays the count of bytes in a file
  • 124. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 125 ln Command • ln creates links between files. • ln creates hard links by default, or symbolic links if the -s (--symbolic) option is specified. When creating hard links, each TARGET must exist. 🞂 Syntax : ln [OPTION]… [-T] TARGET LINK_NAME Optio n Use ln -f If the destination file or files already exist, overwrite them ln -i Prompt the user before overwriting destination files ln -s Make symbolic links instead of hard links
  • 125. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 126 ln Command Example
  • 126. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 127 ln Command Example f2.txt (hello good morning) f6.txt (hello good morning) ln -s Make symbolic links instead of hard links
  • 127. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 128 nl Command • nl command numbers the lines in a file. 🞂 Syntax : nl [OPTION]... [FILE]... 🞂 Example : Optio n Use nl -i Line number increment at each line nl -s Add STRING after (possible) line number nl -w Use NUMBER columns for line numbers
  • 128. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 129 nl Command Example
  • 129. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 130 nl Command Example nl -i Line number increment at each line
  • 130. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 131 nl Command Example nl -s Add STRING after (possible) line number
  • 131. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 132 nl Command Example nl -w Use NUMBER columns for line numbers
  • 132. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 133 head Command • head makes it easy to output the first part (10 lines by default) of files. 🞂 Syntax : head [OPTION]... [FILE]... 🞂 Example : Optio n Use head - n Print the first n lines instead of the first 10; with the leading '-', print all but the last n lines of each file head -c Print the first n bytes of each file; with a leading '-', print all but the last n bytes of each file head - q Never print headers identifying file names
  • 133. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 134 head Command Example
  • 134. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 135 head Command Example head - n Print the first n lines instead of the first 10; with the leading '-', print all but the last n lines of each file
  • 135. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 136 head Command Example head - c Print the first n bytes of each file; with a leading '-', print all but the last n bytes of each file
  • 136. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 137 head Command Example head - q Never print headers identifying file names
  • 137. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 138 tail Command • tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. 🞂 Syntax : tail [OPTION]... [FILE]... Optio n Use tail -n Output the last num lines, instead of the default (10) tail -c Output the last num bytes of each file tail -q Never output headers
  • 138. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 139 tail Command Example
  • 139. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 140 tail Command Example tail -n Output the last num lines, instead of the default (10)
  • 140. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 141 tail Command Example tail -c Output the last num bytes of each file
  • 141. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 142 sort Command • sort command is used to sort a file, arranging the records in a particular order. • By default, the sort command sorts file assuming the contents are ASCII. Using options in sort command, it can also be used to sort numerically. 🞂 Syntax : sort [OPTION]... [FILE]... Option Use sort -c To check if the file given is already sorted or not sort -r Reverse the result of comparisons sort -n Compare according to string numerical value sort -nr To sort a file with numeric data in reverse order sort -k Sorting a table on the basis of any column sort -b Ignore leading blanks
  • 142. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 143 sort Command Example
  • 143. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 144 sort Command Example sort -c To check if the file given is already sorted or not
  • 144. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 145 sort Command Example sort -r Reverse the result of comparisons
  • 145. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 146 sort Command Example sort -n Compare according to string numerical value sort -nr To sort a file with numeric data in reverse order
  • 146. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 147 sort Command Example sort -k Sorting a table on the basis of any column
  • 147. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 148 find Command • find command searches for files in a directory hierarchy. 🞂 Syntax : find [option] [path...] [expression] Option Use find -name filename Search for files that are specified by ‘filename’ find -newer filename Search for files that were modified/created after ‘filename’ find -user name Search for files owned by user name or ID ‘name’ find -size +N/-N Search for files of ‘N’ blocks; ‘N’ followed by ‘c’ can be used to measure size in characters find -empty Search for empty files and directories find -perm octal Search for the file if permission is ‘octal’
  • 148. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 149 find Command Example
  • 149. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 150 find Command Example find -newer filename Search for files that were modified/created after ‘filename’
  • 150. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 151 find Command Example find -user name Search for files owned by user name or ID ‘name’
  • 151. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 152 find Command Example find -size +N/-N Search for files of ‘N’ blocks; ‘N’ followed by ‘c’ can be used to measure size in characters
  • 152. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 153 find Command Example find - empty Search for empty files and directories
  • 153. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 154 uniq Command • uniq reports or filters out repeated lines in a file. • It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields. 🞂 Syntax : uniq [OPTION]... [INPUT [OUTPUT]] Optio n Use uniq -u Prints only unique lines uniq -d Only print duplicated lines uniq -D Print all duplicate lines uniq -c Prefix lines with a number representing how many times they occurred uniq -i Ignore case when comparing
  • 154. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 155 uniq Command Example
  • 155. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 156 uniq Command Example uniq -u Prints only unique lines
  • 156. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 157 uniq Command Example uniq -d Only print duplicated lines
  • 157. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 158 grep Command • The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. • The pattern that is searched in the file is referred to as the regular expression. • grep stands for globally search for regular expression and print out. 🞂 Syntax : grep [options] pattern [files] Optio n Use grep -c Prints only a count of the lines that match a pattern grep -h Display the matched lines, but do not display the filenames grep -l Displays list of a filenames only
  • 158. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 159 grep Command Example
  • 159. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 160 grep Command Example grep -c Prints only a count of the lines that match a pattern grep -h Display the matched lines, but do not display the filenames grep -l Displays list of a filenames only
  • 160. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 161 grep Command Example grep -n Display the matched lines and their line numbers
  • 161. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 162 grep Command Example grep -v This prints out all the lines that do not matches the pattern
  • 162. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 163 grep Command Example grep -w Match whole word
  • 163. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 164 grep Command Example grep -o Print only the matched parts of a matching line
  • 164. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 165 grep Command Example
  • 165. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 166 pipe (|) Command • It redirects the command STDOUT or standard output into the given next command STDIN or standard input. • In short, the output of each process directly as input to the next one like a pipeline. • The symbol '|' denotes a pipe. • Pipes help you mash-up two or more commands at the same time and run them consecutively. 🞂 Syntax : command_1 | command_2 | command_3 | .... | command_N…
  • 166. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 167 pipe Command Example
  • 167. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 168 pipe Command Example
  • 168. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 169 pipe Command Example
  • 169. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 170 tr(translate) Command • The tr command in UNIX is a command line utility for translating or deleting characters. • It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. • It can be used with UNIX pipes to support more complex translation. • tr stands for translate. 🞂 Syntax : tr [OPTION] SET1 [SET2]
  • 170. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 171 tr(translate) Command 🞂 POSIX Character set supported by tr command : • [:digit:] Only the digits 0 to 9. • [:alnum:] Any alphanumeric character. • [:alpha:] Any alpha character A to Z or a to z. • [:blank:] Space and TAB characters only. • [:xdigit:] Hexadecimal notation 0-9, A-F, a-f. • [:upper:] Any alpha character A to Z. • [:lower:] Any alpha character a to z.. Option Use tr -s Replaces repeated characters listed in the set1 with single occurrence tr -d Delete characters in string1 from the input tr -c complements the set of characters in string. i.e., operations apply to characters not in the given set tr -cd Remove all characters except digits
  • 171. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 172 tr Command Example
  • 172. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 173 tr Command Example
  • 173. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 174 tr Command Example tr -s Replaces repeated characters listed in the set1 with single occurrence
  • 174. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 175 tr Command Example tr -d Delete characters in string1 from the input
  • 175. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 176 tr Command Example tr -c complements the set of characters in string. i.e., operations apply to characters not in the given set
  • 176. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 177 tr Command Example tr -cd Remove all characters except digits
  • 177. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 178 history Command • history command is used to view the previously executed command. 🞂 Syntax : history 🞂 Example :
  • 178. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 179 history Command Example
  • 179. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 180 write Command • write sends a message to another user. 🞂 Syntax : write user [ttyname] 🞂 Example Optio n Use user The user to write to tty The specific terminal to write to, if the user is logged in to more than one session
  • 180. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 181 write Command Example • Open first terminal. • Open Second terminal then execute command on first terminal.
  • 181. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 182 wall Command • wall send a message to everybody’s terminal. • wall sends a message to everybody logged in with their mesg permission set to yes. 🞂 Syntax : wall [-n] [-t TIMEOUT] [file] 🞂 Example Optio n Use wall -n --nobanner Suppress banner wall -t --timeout TIMEOUT Write timeout to terminals in seconds. TIMEOUT must be positive integer. Default value is 300 seconds, which is a legacy from time when people ran terminals over modem lines.
  • 182. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 183 wall Command Example • Open four different terminal, execute command on first terminal, message will display on everybody’s terminal.
  • 183. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 184
  • 184. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 185 Shell Script 🞂 Script is a sequence of commands written as plain text and run by an interpreter (shell). 🞂 A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following: ⮩ The Bourne Shell ⮩ The C Shell ⮩ The Korn Shell ⮩ The GNU Bourne-Again Shell
  • 185. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 186 Shell Script - Cont.… 🞂 The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command. 🞂 The shell interprets commands the user types and manages their execution. 🞂 A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.
  • 186. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 187 Steps to write and execute a shell script 1. Create a file using a vi editor(or any other editor like gedit). 2. Name script file with extension .sh 3. Write some code. 4. Save the script file as filename.sh 5. For executing the script type bash filename.sh
  • 187. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 188 Shell Variables 🞂 The user variable name can be any sequence of letters, digits, and the underscore character, but the first character must be a letter. 🞂 To assign a value to a variable: number=25 name=“abc" 🞂 There cannot be any space before or after the “=“ 🞂 Internally, all values are stored as strings. 🞂 To use a variable, precede the name with a “$”. num=3 a=$num
  • 188. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 189 User input in Shell Script 🞂 The read command allows a user to provide the runtime input. 🞂 Shell allows to prompt for user input. 🞂 Syntax: read varname [more vars] • Value entered by user is assigned to varname 🞂 Run shell script : bash filename.sh
  • 189. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 190 Operators Arithmetic Operators Bash Operator Operator Description + + Addition - - Subtraction * * Multiplication / / Division % % Modulus Assignment Operators = = Assigns right operand in left operand i.e. a=$b
  • 190. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 191 Operators Relational Operators – for Numeric Bash Operator Operator Description -eq == Equal -ne != Not equal -gt > Greater than -ge >= Greater than or equal -lt < Less than -le <= Less than or equal -z == null Is null
  • 191. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 192 Operators Relational Operators – for String Bash Operator Operator Description = or == == Equal != != Not equal str1 < str2 < str1 is less than str2 str1 > str2 > str1 is greater str2
  • 192. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 193 Operators Boolean Operators Bash Operator Operator Description ! ! This is logical negation. -o OR This is logical OR. -a AND This is logical AND. String Operators Bash Operator Operator Description = = Checks if the value of two operands are equal or not != != If values are not equal then the condition becomes true.
  • 193. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 194 Arithmetic Operator Example (e.g. a=10, b=20) Operator Description Example + (Addition) Adds values on either side of the operator `expr $a + $b` will give 30 - (Subtraction) Subtracts right hand operand from left hand operand `expr $a - $b` will give -10 * (Multiplication) Multiplies values on either side of the operator `expr $a * $b` will give 200 / (Division) Divides left hand operand by right hand operand `expr $b / $a` will give 2 % (Modulus) Divides left hand operand by right hand operand and returns remainder `expr $b % $a` will give 0 = (Assignment) Assigns right operand in left operand a = $b assign value of b into a == (Equality) Compares two numbers, if both are same then returns true. [ $a == $b ] would return false. != (Not Equality) Compares two numbers, if both are different then returns true. [ $a != $b ] would return true.
  • 194. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 195 Relational Operator Example (e.g. a=10, b=20) Operator Description Example -eq Checks if the value of two operands are equal or not; if yes, then the condition becomes true. [ $a -eq $b ] is not true. -ne Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true. [ $a -ne $b ] is true. -gt Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. [ $a -gt $b ] is not true. -lt Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. [ $a -lt $b ] is true. -ge Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. [ $a -ge $b ] is not true. -le Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. [ $a -le $b ] is true.
  • 195. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 196 Boolean Operator Example (e.g. a=10, b=20) Operator Description Example ! This is logical negation. This inverts a true condition into false and vice versa. [ ! false ] is true. -o This is logical OR. If one of the operands is true, then the condition becomes true. [ $a -lt 20 -o $b -gt 100 ] is true. -a This is logical AND. If both the operands are true, then the condition becomes true otherwise false. [ $a -lt 20 -a $b -gt 100 ] is false.
  • 196. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 197 String Operator Example (e.g. a=“abc”, b=“efg”) Operator Description Example = Checks if the value of two operands are equal or not; if yes, then the condition becomes true. [ $a = $b ] is not true. != Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true. [ $a != $b ] is true. -z Checks if the given string operand size is zero; if it is zero length, then it returns true. [ -z $a ] is not true. -n Checks if the given string operand size is non-zero; if it is nonzero length, then it returns true. [ -n $a ] is not false. str Checks if str is not the empty string; if it is empty, then it returns false. [ $a ] is not false.
  • 197. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 198 expr Command • The expr command in Unix evaluates a given expression and displays its corresponding output. • It is used for: • Basic operations like addition, subtraction, multiplication, division, and modulus on integers. • Evaluating regular expressions, string operations like substring, length of strings etc. • Syntax : expr EXPRESSION $ expr 3 + 5 output: 8 $ expr 3 * 5 output: 15 num1=10 num2=20 sum=`expr $num1 + $num2` echo $sum output: 30 Backtic k sum=`expr $num1 + $num2`
  • 198. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 199 if Statement • If statements use the if, then, else, and fi keywords. • The condition goes in square brackets. 🞂 Syntax : if [ <some test> ] then <commands> fi if [ $no -gt 100 ] then echo "Greater than 100" fi The spaces before and after the square brackets [ ] are required.
  • 199. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 200 if Statement if [ $a -gt $b ] then echo “A is Greater than B" fi if [ $a -ne $b ] then echo “A is not eqal to B" fi if [ `expr $mid1_total + $mid2_total` -gt 50 ] then echo “Total Marks is Greater than 50" fi
  • 200. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 201 if…else Statement if [control command] then <statements-1> else <statements-2> fi if [ $a -eq $b ] then echo "a is equal to b" else echo "a is not equal to b" fi • executes statements-1 if condition is true • executes statements-2 if condition is false
  • 201. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 202 Nested if-else-fi Statement if [control command] then if [control command] then <statements> else <statements> fi else <statements> fi
  • 202. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 203 if-else-if ladder if [control command] then <statements> elif [control command] then <statements> elif [control command] then <statements> else <statements> fi • The word elif stands for “else if” • It is part of the if statement and cannot be used by itself
  • 203. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 204 if-else-if ladder Example if [ $i -eq 5 ] then echo "Value of i is 5" elif [ $i -eq 10 ] then echo "Value of i is 10" elif [ $i -eq 20 ] then echo "Value of i is 20" elif [ $i -eq 30 ] then echo "Value of i is 30" else echo "Value of i is not equal to 5,10,20 or 30" fi
  • 204. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 205 case Structure case $word in pattern1) c ommand-list1 ;; pattern2) command-list2 ;; patternN) command-listN ;; *) ;; esac echo "Enter number:" read num case $num in 1) echo "It's one!" ;; 2) echo "It's two!" ;; 3) echo "It's three!" ;; *) echo "It's something else!" ;; esac • Use the case statement for a decision that is based on multiple choices.
  • 205. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 206 Repetition Constructs 206
  • 206. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 207 while loop Syntax: while [ condition ] do command1 command2 ….. ..... done i=1 while [ $i -le 10 ] do echo “$i” i=`expr $i + 1` done The while construct tests for a condition, and if true, executes commands. It keeps looping as long as the condition is true.
  • 207. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 208 until loop Syntax: until [ condition ] do command1 command2 ….. ..... done COUNT=1 until [ $COUNT -gt 5 ]; do echo "Value of count is: $COUNT" COUNT=$(($COUNT + 1)) done The until construct tests for a condition, and if false, executes commands. It keeps looping as long as the condition is false (opposite of while construct)
  • 208. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 209 for loop using “in” and list of values for i in 1 2 3 4 5 do echo "Looping ... number $i" done for arg in list do command(s) ... done • Where the value of the variable arg is set to the values provided in the list one at a time and the block of statements executed. • This is repeated until the list is exhausted.
  • 209. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 210 for loop using C like Syntax for((i=1; i<=5; i++)) do echo $i done for ((initialization; condition test; increment or decrement)) do //statements to be executed repeatedly done
  • 210. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 211 select loop select variableName in choice1 choice2 ... choiceN do -- Block of Commands – done • Select command use PS3 variable to print its prompt. • Each word in list is printed on screen preceded by a number. • If the line consists of the number corresponding to one of the displayed words (from the list), then varName is set to that word. You can use if..else.fi or case..in..esac to make a decision. • If the line is empty, WORDS and the prompt are redisplayed. • If EOF (end of file) is read, the command completes. • The loop continues until a break (CTRL+C) is encountered.
  • 211. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 212 select loop Example echo "Enter space shuttle to get more information : " # set shuttle list select shuttle in Columbia Endeavour Challenger Discovery do echo "$shuttle selected" done Output: Enter the space shuttle to get more information : 1) Columbia 2) Endeavor 3) Challenger 4) Discovery #? 1 Columbia selected #?
  • 212. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 213 Break and Continue Statement • All statement inside the loop executed as long as some condition are true. • If break placed inside the loop, when loop reach the break statement it will terminated out from the loop. • If continue placed inside the loop, when loop reach the continue statement it will not execute next lines of the loop and it will go to the next iteration.
  • 213. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 214 Break and Continue Statement while [ condition ] do cmd-1 break cmd-n done echo "done" break statement continue statement This iteration is over and there are no more iterations This iteration is over, do the next iteration while [ condition ] do cmd-1 continue cmd-n done echo "done"
  • 214. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 215 echo and printf Command echo Command printf Command 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1234 5 1234 5 Decimal: 100 Octal: 144 Hex: 64
  • 215. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 216 Average of 4 numbers using bc Command a=10 b=2 c=12 d=5 sum=$(( a + b + c + d)) avg=`echo $sum / 4 | bc –l` printf %0.3f “$avg” OUTPUT : 7.250
  • 216. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 217 Division using bc Command # get the first number printf "Enter first integer number: " read a # get the second number printf "Enter second integer number: " read b # if b is 0 if [ $b == 0 ] then printf "Division by 0 not allowed.n" exit # exit the script fi # perform division result=`expr "$a / $b" | bc -l` printf "%d / %d = %.5fn" "$a" "$b" "$result"
  • 217. Prof. Maulik D Trivedi #3140702 (OS) ⬥ Unit 9 – Unix/Linux Operating 218
  • 218. Darshan Institute of Engineering & Technology, Rajkot Than k You Operating System (OS) GTU # 3140702 maulik.trivedi@darshan.ac.in 9998 265 805 Computer Engineering Department Prof. Maulik D Trivedi