Unix Shell and Commands
Dr. Alekha Kumar Mishra
Shell
●
This is one way to talk to computer
●
It is a command line interface
●
Accepts the commands from the user and converts them into a
●
language that the kernel can understand
●
In UNIX, shell is separated from other units of operating system.
●
Shell is just another program in UNIX whose task is to execute other
programs behalf of a user
●
Type of Shell includes Bourne shell, Korn shell, Bourne-again shell,
and C shell
●
Most popular/latest shell in UNIX is Bourne-again shell or bash
Dr. Alekha Kumar Mishra
bash shell
●
It is a command line interpreter that performs the
following tasks
– Launches programs
– Takes command
– Shell scripting (sequence of actions)
●
The shell prompt Home directory
Prompt for
regular user
username Machine or system name
Prompt for
root
Dr. Alekha Kumar Mishra
Types of command
●
Internal commands
– The shell has a number of built-in commands that are known as internal
commands.
– Example cd, mkdir, echo etc.
– These commands do not generate a process, and are directly executed by the
shell
– These commands are built into the shell and do not exist as separate files.
●
External commands
– External commands are Unix utilities and programs such as cat and ls.
– These commands exist in the form of individual files
– Found in either in the /bin directory, or in the /etc (admin commands) directory
Dr. Alekha Kumar Mishra
PATH
●
Bash shell keeps the list of directory to search for
command in an environmental variable (builtin)
called PATH
●
We can use echo command to display the PATH
variable
Dr. Alekha Kumar Mishra
UNIX Essential Commands
●
clear
– Clear the screen
●
who
– Prompts user name with login time currently
logged to this system
●
whoami
– Prompt the name of the current user
●
tty
– prints the file name of the terminal connected
to standard input
●
ls
– Lists all the files and directories.
– Accepts the path to the listing directory as
argument(s).
– Also accept patterns (RE) for selecting files
and directories.
Dr. Alekha Kumar Mishra
Some options for ls
$ ls chap* ↵
– list all files and directories begins with “chap”
$ ls -l ↵
– detailed listing of files and directories
$ ls -r ↵
– Shows files sorted in reverse alphabetical order
$ ls -R ↵
– Shows the recursive listing, that is, files of
directories as well as subdirectories are also
displayed
$ ls -a ↵ $ ls -i ↵
– include hidden files (starts with ‘.’) - Shows inode number of all the files
$ ls -d */ ↵
– Show the list of only directories
Dr. Alekha Kumar Mishra
UNIX Essential Commands(2)
●
cat
– Display and concatenate the content of a
file(s)
$cat filename ↵
●
Multiple files can be displayed by
providing names sequencially
– $cat file1.txt file2.txt
Dr. Alekha Kumar Mishra
Redirection command
●
> directs the output of a
command to an output file. It
overwrites the output file if
already exists
● $ ls -l > list.txt ↵
● $ cat list.txt ↵
●
>> directs and append the output
of a command to an existing file.
(Does not overwrites)
● $ who >> list.txt ↵
● $ cat list ↵
Dr. Alekha Kumar Mishra
UNIX Essential Commands(3)
●
wc
– Displays number of lines, number of words, and number of characters in
a given file
– $ wc filename ↵
– Output format : #oflines #noofwords #noofchars filename
●
wc options
– $ wc -l filename ↵ (only number of lines)
– $ wc -w filename ↵ (only number of words)
– $ wc -c filename ↵ (only number of characters)
●
| (‘the pipe’)
– Feeds output of one command to another command
– $ ls -l | wc -l ↵
Dr. Alekha Kumar Mishra
UNIX Essential Commands(4)
●
echo ●
uname
– Display strings, variables, constants etc. – Prompts the version of the linux/unix (kernel)
– Options
– $ echo ‘hello’ ↵
-a Displays basic information currently
– $ echo hello ↵ available in the system
– $x=5↵ -i Displays the name of the hardware platform
– $ echo $x ↵ -n Displays the node name, the name by
which it is connected to the communication
– Alternatively, we can use printf too network
●
type -r Displays the operating system release
level
– It tells the location of a unix
command/program -v Displays the operating system version
– $ type ls ↵ -s Displays the name of the operating system
(default)
ls is /bin/ls -S Used to get basic information of the
– Alternate commands are which and specified system name (superuser only)
whereis
Dr. Alekha Kumar Mishra
UNIX Essential Commands(5)
●
man
– Help manual for command
– $ man commandname ↵
– Alternate way : $ command - - help ↵
●
less
– Read the content of a file one page at a time. (allow scrolling)
– $ less myfile.txt ↵
– $ man ls | less ↵
●
more
– Read the content of a file one page at a time (does not allow scrolling of pages)
– $ more myfile.txt ↵
– $ man ls | more ↵
Dr. Alekha Kumar Mishra
UNIX Essential Commands(6)
●
date $ date +%m ↵
– Displays data and time
●
Date printing format options
$ date +%h ↵
– Options always prefixed with + $ date +%H:%M:%S ↵
%
– d : day of the month
– y : last two digit of the year
– m : month in number
– h : month in name
– H, M, S : hour, minute, and
second
Dr. Alekha Kumar Mishra
UNIX Essential Commands(7)
●
passwd
– Change the current password of the user
●
bc
– The command line calculator
●
time
– Shows time taken by a program execution at various resources
– $ time sort -o netlist invoice.lst ↵
– real : total time elapsed
– user : time spent executing itself
– sys : time spent by linux kernel system
Dr. Alekha Kumar Mishra
UNIX Essential Commands(8)
●
pwd
– Current working location or present working directory
●
cd
– Change directory by either providing sub-directory name or a director path
– $ cd Documents ↵
– $ cd /home/alekha/Downloads ↵
●
Special notations for directories
./ (dot slash)- current directory
~ (tilde) – home directory
. . (double dots)- parent directory
/ (slash) - root directory
Dr. Alekha Kumar Mishra
UNIX Essential Commands(9)
●
mkdir
– Create a new directory with the name or the directory path provide as argument
●
rm
– Remove a directory either by providing sub-directory name or the directory path
●
rmdir
– Remove an empty director.
●
cp
– Copy a file from the source location (path) to destination location (path)
– Syntax : $ cp sourcepath destinationpath ↵
●
mv
– Move or rename a file
– Syntax : $ mv oldfilepath newfilepath ↵
●
Some necessary options for mv
i : interactive mode
r : recursive mode (when a directory contains subdirectories)
f : force mode (opposite to interactive mode)
Dr. Alekha Kumar Mishra
UNIX Essential Commands(10)
●
ln
– To establish additional links to an existing file
– Link is either hard or symbolic type
– Syntax: $ ln –[sf] oldname newname
– By default hard link is created. The symbolic link is created
using option (-s)
– $ln mytext.txt akm ↵
– The number of links for a file is reflected in ls -l command
– A link can be removed by rm command
Dr. Alekha Kumar Mishra
Hard link vs symbolic link
●
Hard links
– Can point to programs and files, but not to directories.
– If the original program or file is renamed, moved, or deleted, the hard link is not broken.
– Cannot span different file systems, hard links share an inode number
●
Symbolic links (soft links)
– Symbolic links (symlinks) are used to link to a different file system
– It is a special type of file that references another file or directory
– Contains no actual data
– We can change the symlink to point to the desired files
– Inherit the permission of the folder they are pointing at
– Syntax: ln -s target_file symbolic_link
– $ ln -s mytext.txt akm ↵
●
unlink
– removes the specified file, including symbolic links.
– Syntax
– unlink filename
Dr. Alekha Kumar Mishra
UNIX Essential Commands(11)
●
touch ●
cmp
– The touch command is used for creating files and – Compare the content of two given files. It
changing time stamps.
shows the difference in the file if exists.
– Syntax: touch –[ma] time_expression filename However, shows no message if two files
– $ touch file1.txt are identical.
– $ touch 09211520 file1.txt – $ cmp file1 file2 ↵
– $ touch –m 11071015 file2.txt
●
comm
●
cut
– it is used for slicing (cutting) a file vertically.
– Finds common in two files
– Syntax: cut [-c –f] file_name ↵ – $ comm file1 file2 ↵
– Here, –c refers to columns or characters and –f ●
diff
refers to the fields
– Displays the differences
●
paste
– join textual data together
– $ diff file1 file2 ↵
– very useful if we want to put together textual
information located in various files.
– Syntax : $ paste file1 file2 ↵
●
Dr. Alekha Kumar Mishra
chmod command
●
It is used to change the permission of a
file ●
$ chmod go-r, u-x filename ↵
●
Types of permission ●
Numeric representation of permission
– Read (r) – 9 bits
– Write (w) rwx rwx rwx
– Execute (x) user group others
●
Types of user
●
Example
– 111 100 100
– User (u)
– 744
– Group (g)
$ chmod 744 filename ↵
– Other (o)
●
$ chmod 777 filename ↵ (all permission to all)
– For All (a)
●
$ chmod 444 filename ↵ (read permission to all)
●
Example
●
$ chmod 222 filename ↵ (write permission to all)
– $ chmod u+x filename ↵
●
$ chmod 111 filename ↵ (execute permission to all)
– $ chmod ugo+x filename ↵
$ chmod a+x filename ↵
Dr. Alekha Kumar Mishra
UNIX Essential Commands(12)
●
umask
– sets the default permissions for the files that will be created in the future.
– $ umask 342
●
Using semicolon (;) we can execute multiple commands in a single line
– $chmod 666 filename ; ls -l filename ; mkdir myfiles ↵
●
sort
– Ordering of a file
– $ sort [-n][-r][-f][-u] filename ↵
– $ sort sort +p1 - p2 filename ↵
●
uniq
– Locate repeated lines in a file
– $ uniq filename ↵
●
script
– is used to typescript or record all terminal processes.
– After running the script command, it begins recording everything that appears on the screen, including inputs and
outputs, until it exits (exit command).
– $script filename ↵
...
$exit
Dr. Alekha Kumar Mishra
UNIX Essential Commands(13)
●
locate
– Search for files whose name or path matches a particular search string and for which, the user has access
permissions
●
df
– reports the free disk space for all the file systems installed on our machines in terms of disk blocks.
– Syntax : df -h ↵
●
gzip
– compresses the specified file and replaces it with the .gz extension file, that is, the original file is deleted
and is replaced by the compressed version
●
zip and unzip
– The zip command compresses a set of files into a single archive
– unzip command is used to unzip the archive and extract all the files that were compressedin it.
●
tar
– create archive files using various compression algorithms such as xz, gzip, and bzip2.
– compress multiple directories file :$ tar -zcvf file.tar.gz dir1 dir2 dir3
– Extract file by tar -xzvf file.tar.gz
Dr. Alekha Kumar Mishra
UNIX Essential Commands(14)
●
chown
– used for changing the owner and group owner of a file.
– $ chown ravi notes.txt ↵
– $ chown ravi:mba notes.txt ↵
●
Group commands
– chgrp – change group of a file (files)
– groups – displays group membership of a user
– groupadd creates a new group
Dr. Alekha Kumar Mishra
Processes in UNIX
●
ps
– This command tells about all the processes that are currently running in os.
– $ ps ↲ : shows only the process of the current user
– $ ps -a ↲ : shows processes of all users
– $ ps -f ↲ : shows full listing of processes
●
‘&’
– This character is used to run a process in the background
– $ sort emp.txt > empsort.txt & ↲
22310 (returns the pid of the process)
●
fg
– Bring a background running process to foreground
– $ fg processname ↲
●
kill
– Kill or terminates a process
– $ kill pid ↲
– $ kill -9 pid ↲ : forcibly kills a current running process with given pid.
Dr. Alekha Kumar Mishra
grep command and regular
expression
●
The grep command scans for occurrence of a pattern
and can display the selected text matching the pattern
– Syntax : $ grep options pattern filename(s)
– Here, pattern accept text and regular expression
●
Example
– $ grep sales emp.txt ↲
– This command displays all the lines in the file ‘emp.txt’
containing the term ‘sales’
●
Grep is one of the UNIX command that silently returns
the prompt in case no pattern match found in the file.
Dr. Alekha Kumar Mishra
grep command(2)
●
Grep can use a series of string in the argument,
where first argument as the pattern, and rest as
filenames.
– In this case, the output shall be displayed along with
filenames.
– $ grep director emp1.txt emp2.txt ↲
Dr. Alekha Kumar Mishra
grep command(3)
●
If the pattern contains whitespace, then it is
mandatory to quote the pattern.
●
Otherwise, the first word of the pattern shall be
treated as actual pattern and rest of the words as
filenames
– $ grep ‘SCOTT ANALYST’ emp1.txt ↲
Dr. Alekha Kumar Mishra
grep command options
●
-c
– It is used to count the occurrence of the pattern
– $ grep -c ‘MANAGER’ emp1.txt ↲
●
-n
– Displays the line numbers of the line of pattern match
●
-v
– Option to select all but lines containing the pattern
●
-l
– Displays only filename containing the pattern
– $ grep -l ‘MANAGER’ *.txt
●
-i
– Ignore case in pattern
Dr. Alekha Kumar Mishra
Grep with RE
●
[ ]: Matches any one of a set characters
●
[ ] with -: Matches any one of a range characters
●
^: The pattern following it must occur at the beginning of each line
●
^ within [ ] : The pattern must not contain any character in the set specified
●
$: The pattern preceding it must occur at the end of each line
●
. (dot): Matches any one character
●
\ (backslash): Ignores the special meaning of the character following it
●
*: zero or more occurrences of the previous character
●
(dot).*: Nothing or any numbers of characters.
●
egrep(extended grep) – patterns
– {N} – matches exactly N characters
– {N,}- matches N or more number of characters
– {min,max} – matches minimum ‘min’ and maximum ‘max’ character
Dr. Alekha Kumar Mishra
sed – stream editor
●
It is used to perform basic text transformations on an input stream
●
sed works by making only one pass over the input(s), and is
consequently more efficient
●
The unique feature is it takes a file or input from a pipeline
●
SED syntax forms:
– sed [-n] [-e] 'command(s)' files
– sed [-n] -f scriptfile files
●
Two standard options
– Printing patterns, and editing
Dr. Alekha Kumar Mishra
Printing options
●
-n flag, suppresses the default printing
– $cat file1 | sed -n ↲ will produce no output.
– $cat file1 |sed -n '1,1p' ↲ prints the first line
– $cat file1 |sed -n '2,$p' ↲ prints second line onwards till last
line
– $cat file1 |sed '2,$p' ↲ prints all the line twice from second line
onwards
– $cat file1 | sed -n '/The/p' ↲ prints the lines containing pattern
‘The’
– $cat file1 | sed -n '/er$/p' ↲ prints the lines ending with ‘er’
Dr. Alekha Kumar Mishra
Editing options
●
Substitution
– $cat filename | sed 's/er/xx/' ↲ substitute ‘er’ with ‘xx’
●
Normally the s command only replaces the first string it finds on
a line.
– $cat filename | sed 's/er/xx/g' ↲ substitute ‘er’ with ‘xx’ in every
occurrences in a line
●
Deletion
– $cat filename | sed ‘/bad/d’ ↲ delete the line having pattern ‘bad’
– $cat filename | sed ‘/bad/!d’ ↲ delete the line not having pattern ‘bad’
– $cat filename | sed ‘2,5d’ ↲ deletes the lines from line no. 2 to 5.
Dr. Alekha Kumar Mishra
Adding a line
●
/String/a \
text lines
●
This command will add the text lines after every line
containing string.
– $cat filename | sed '/er/a \ good one ' ↲ add line ‘good one’ after the
line contains string ‘er’
– $cat filename | sed '/er/a \
good one \
2 good one \
– 3 good one \' ↲ will add three lines after the line containting string
Dr. Alekha Kumar Mishra
Sed script file
●
Let mysedfile contains /Bad/!d
– $cat filename | sed -f mysedfile ↲ will take the
command part from the file ‘mysedfile’ using -f option
Dr. Alekha Kumar Mishra