1 Introduction 25 04 2023
1 Introduction 25 04 2023
TECH
PHASE -I
Information
File management
Directories
Devices
Printing
Networking/communications
Geology programs
PHASE –II
VI COMMANDS
UTILITY COMMANDS
CAL
Print the calendar
cal 1998 all months of the year
cal 12 1998 December month of 1998
DATE
Print System date (date can be changed by system administrator)
Options:- d (day of a month), y(2 digits of an year), H,M,S (hour
,minutes and seconds )
date system date with time
date +%m print month number
date +%h print month name
date +”%m %h” name and number
date –u universal time
date +%r 12 hr time
WHO
Details about login users
Options:- am I ( about user), -Hu ( more information with header
details)
who am i
who –Hu
TTY
Know the terminal name. Unix treats terminals in multiuser as files in device
directory.
tty
UNAME
Print system information
uname –a all
options –s (print kernel name), -n (network name), -e (hardware platform)
,-o (os)
TPUT
Various displays
OPERATING SYSTEMS LAB-B.TECH
tput 12,30
echo “hh” position cursor
tput smso highlight text
echo “ddd”
tput rmso disable highlight
BC
Binary calculator
Provides all type of calculations
bc
12.3 + 9.9
<ctrl + d>
sacle =2 truncate to 2 decimal places
ibase=2 to set input bae
obase=16 set output base
c=a+b
c to print c
TIME
To calculate the time taken by a command
time sort –o 1.c 2.c
time shows real(elapsed time),user(execution time), sys time(time by os to
do the operation)
SCRIPT
To record the user activities for further use .Doesn‘t record commands in vi
editor
script write to typescript file
script –a appends to typescript file
script logfile writes to logfile
SPELL
Spell check of a file
spell filename displays incorrect words
ispell filename displays lines and suggestions
OPERATING SYSTEMS LAB-B.TECH
LS
Listing files
ls -x (display files in many columns)
ls -a hidden files
ls -f shows / for directory, * for exe file
ls -r list in reverse order
ls -R recursive listing
l (long listing) i ( inode number) t (modification time) u (access time)
SPLIT
split filename splits file into many of 1000 lines each in name fna, fnb,
fnc…
split –34 filename splits after 34
CMP
Compare two files
COMM
Displays common in 2 files
comm f1 f2
DIFF
Says the difference between 2 files, suggests which line to be
changed
CHMOD
Change permission mode of a file
chmod 744 filename
rwxrwxrwx -this combination stands for read, write, execute permissions of
user ,group ,public.
Either a number can be given or abbreviations can be given
chmod ugo+rwx filename r,w,x perm for u , g , o
chmod u-x,g+r filename remove exec for user add read for group
chmod –r u+x shell assign permissions for all files in shell directory
OPERATING SYSTEMS LAB-B.TECH
CHOWN
Change owner and/or group of a file
chown owner_name filename
chown owner:groupname filename
chown :groupname filename
CHGRP
Change group of a file
chgrp <name> filename
CP
Copy the contents of one file to another
cp t1.txt t2.txt
cp –i s1.doc s2.doc prompts an alert message
cp –r copying directory structures
RM
Remove a file from the directory
rm filename
rm –i filename interactive remove
rm –f filename forcibly remove a file
rm –r directory name delete a directory with contents
MV
Move a file to another(rename)
Move a file from one directory to another
mv d f
mv d ipc/shell/d
WC
Count of lines ,words, bytes in file
cat <filename> |wc (all)
cat <filename> |wc –l (only lines)
cat <filename> |wc -c
OPERATING SYSTEMS LAB-B.TECH
FILTERS
PAGINATING FILES
HEAD
Display the beginning of the file
head fn first 10 lines of a file
head –3 fn cut first 3 lines of a file
TAIL
Display the end of the file
tail –3 fn extract last 3 lines of a file
tail +11 fn from 11th line
CUT
Extract columns and fields
cut –c1 cut column one
cut –c1-11 form one to eleven
cut –c1,4,5 cut column 1,4,5
cut –f1-5 cut field one to five
cut -d “ “ –f5 cut field 5 by delimiter space
d can be ; / , “ “ :
TR
Translate characters
tr [options] < filename
cat filename | tr [a-z] [A-Z] translate lower to upper case
tr –d “ “ delete blank spaces
tr –s “ “ suppress many spaces to one
tr ‘|/’ ‘~-‘ change | to / ~ to –
OPERATING SYSTEMS LAB-B.TECH
SORT
Sort the contents of file
sort –t \ : +1 <filename> sort by : separated field by col 2
sort -t \ : +2 -3 +1 secondary key 2 ,primary key 3,resume after first
field( sort from 2 field to 3rd field
nd
PATTERN MATCHING
GREP
Globally search for a regular expression and print it.
grep sales filename prints all lines containing sales
grep ‘components number’ filename match phrase
options
-c count of match
-n line numbers of matching lines
-v inVerse match for other than pattern
-l filenames which are matching
grep –l ‘ipc’ *.c
-i ignore case
-e multiple options
-o give matched pattern
grep –e ‘agar’ –e ‘Agar’ –e ‘ Aagar’ filename
grep –5 “do loop” update.sql 5 lines above and below do loop
gr.ep –A 5 …. (below)
grep –B 5 … (above)
grep –N 2 ‘bill’ accounts.c two occurrences of bill
Regular Expressions
* Zero or more occurrences of a character
. Matches a single character
[pqr] Matches p or q or r
[c1-c2] Matches between c1 to c2
[^ pqr] Matches other than p q r
^ pattern Matches pattern at the beginning of line
Pattern$ Matches pattern at the end of line
OPERATING SYSTEMS LAB-B.TECH
EGREP
Extending grep
FGREP
faster grep
only patterns used regular expressions cannot be used
fgrep –f <pattern file > targetfile
SED
Stream Editor
sed ‘ed commands’ <filename>
sed –n ’20,30p’ print lines from 20 to 30
sed ‘1,10d’ delete 1 to 10 lines
sed ‘s/old/new/g’ search for old and replace by new
sed ‘y/str1/str2/ replace each in str1 by str2
PROCESS COMMANDS
echo $$
process id of current shell
ps
process status (pid, name, tty, time )
ps –f
full information(uid, ppid etc)
ps –f –u cra
full information of user cra
ps –a
processes of all users
ps –x
OPERATING SYSTEMS LAB-B.TECH
system process
nohup < any command> & makes to run a command without hangup
even after logout
NICE
Job execution with low priority
nice wc –l unix.txt
AT
Execute a command or file at a given time
at 14.00
sam.sh
<ctrl –d>
BATCH
Schedule the process for later execution
batch < filename
CRON
ChRONograph .It is a daemon process which ticks every minute.
It looks for the file in /var/spool/cron.
User can insert their file in cron directory by
cron cron.txt
crontab –l shows the contents of cron file
SCRIPT STATEMENTS
I/O
– echo name list /string
– echo $a,$b
– echo “enter a variable”
– echo “value of “ $a
– echo -n “give two int”
– read variables
OPERATING SYSTEMS LAB-B.TECH
Operators
– relational(numbers) -gt, -lt , -ge, -le ,-eq, -ne
– relational (strings) = , != , -n string,-Z string $a -gt $b , $abc -
ne $ghj
-n $a ( $a > 0)
-z $a ($a equal to zero)
Logical operators
File operators
-r , -w , -x , -c , -b , -d
-f , -s ,-k
if [ -r $fn ]
then
echo “ file has read”
fi
do
echo $i
done
for i in *
do
echo $i
done
$* parameters in command line
While
while [ $i -lt $n ]
do
..
..
expr ` $i + i `
Done
Case
case expr in case $i in
1) ;; 1)
2) ;; a)
*) default;; asd)
esac
Arithmetic Statements
+-*/%
expr 1 + 2
expr 1 \* 2
expr $a + $b
expr $a \* $b
21
OPERATING SYSTEMS LAB-B.TECH
REFERENCES
TEXT BOOK
Eric Foster-Jonson, John C. Welch and Micah Anderson, “Beginning Shell Scripting”, Wiley-
India
Introduction to Linux – A Hands on Guide
Ashley J.S Mills, "Unix Shell Scripting Tutorial", Copyright © 2005 The University Of
Birmingham
WEB REFERENCES
SHELLdorado (http://www.oase-shareware.org/shell/)
Shell Information from CUED (http://www-h.eng.cam.ac.uk/help/unix.html#Shell)
Linux Shell Scripting Tutorial - A Beginner’s handbook (http://www.freeos.com/guides/lsst/)
http://www.japarker.btinternet.co.uk/sh/
http://linux-training.be