Professional Documents
Culture Documents
Contents :
1.
2.
File System
3.
Basic Utilities
4.
Shell Features
5.
Advanced Utilities
6.
Communication Features
7.
System Calls
Evolution
645
Chapter 1
Introduction
of UNIX
and
History
Version 6
Version 7
Version 8
Written in C
Moved to PDP - 11/45 and
PDP - 11/70
Released in 1975
For use by universities only
Released in 1978
First commercial version but
primarly used in universities
32V - VAX version
UNIX Support Group(USG)
takes responsibility
Research Group still busy
System V
Release 1
System V
Release 2
System V
Release 3
System V
Release 4.0
Released in 1981
Commercial orientation
Over 100 manufacturers of UNIX-based
micro , mini and mainframes
UNIX users > 100,000
Released in 1983
Enhancement of System III
Released in 1984
Enhancements of System V , Release 1
Released in 1988
BSD UNIX
(from Berkeley Software
Distributions)
* UNIX's entry into University of California, Berkeley Campus during
1976-77
* Inputs of UCB Faculty/researchers incorporated in System V of
AT&T UNIX
UNIX 3BSD
Released in 1978
Developed by Bill Joy and Ozalp Baboglu
Added Virtual memory, Demand paging
to the VAX version 32V
UNIX 4BSD
Released in 1983
UNIX Features
Multitasking
Performing tasks simultaneously
rather than
sequentially
e.g., While printing a document , start
editing
another document
Each task is considered to be a process
Multi-user
computer
simultaneously
i.e , more than one keyboard and
terminal
can be connected to one computer
Highly cost-effective
Portability
*
*
*
*
*
*
Kernel
Shell
A program that
interprets the user commands/requests
calls programs from memory and
executes them one at a time or in a
series
(called a pipe)
Tools &
OS
Networking
Services
File Management
And Security
Input / Output
Services
UNIX
Signal
Handling
SYSTEM
KERNEL
System
Memory
Administration
Management
and Accounting
Process
Scheduling
Chapter 2
File System
1.
2.
3.
4.
5.
6.
7.
8.
9.
Super
block
Inode
list
Data
block
Boot block
-
Super block
Inode list
Data block
Ordinary Files
Directory Files
Special Files
Standard Files
Examples
Text Files
Commands
Sequence of commands
interpreted by UNIX text
Data
Executable
* Types :
Character file
by
Block file
or 1024
File Names
*
Path Names
Absolute Path name
*
e.g.,
/usr/trg/c/test.c
where
e.g.,
If current directory is
/usr/trg
then the file could be just referenced as c/test.c
Features
* Hierarchical
* Security on each file
- Owner
- Group
- All others
*
Removable
File Independence -
Unix
dev
bin
lib
/
bin
console
lp0
tty0
src
sh
csh
--Libc.a
---
uch
tmp
man
---
bin
local
lib
---
usr
include
spool
etc
spell
troff
---
*.h
tmac
passwd
group
init
---
lib
troff
---
tmp
---
/dev
fd0, lp0
/etc
Administrative Programs
passwd, shutdown
/lib
libc.a
/usr/bin
Unix utilities
cal, bc,spell
/usr/adm
/tmp
Administrative commands
and files
Temporary files created
on error conditions
cp, mv, ln
adduser
/usr/games
Game Programs
/usr/include
procedure
/usr/lib
/usr/mail
Mail files
Example : mailbox
/usr/news
News files
/usr/spool
Spool files
/usr/tmp
Temporary files
/usr/src
Access Mode
Directory
Ordinary
Read
Examination of
File Contents
Listing of
files within
Directory
Write
Allows changing
of file contents
Creating new
files within
Directory
Execute
Executing file as
a command
allowed
Searching the
Directory
allowed
login :
User can type his name and password to
identify himself
login command can be used as
$ exec login
to log-on onto another user account after identifying yourself
in response to prompts for user name and password
su
setuser
$su - trg2 -c
$ logout
or
$ exit
or
$ <ctrl-d>
Directory
Security
Management
Operation
cd
pwd
mkdir
rmdir
mvdir
File Comp.
cmp
comm
File
contents
File
compression
Mountable
file
cat
ls
wc
file
pack
unpack
mount
umount
passwd
crypt
chown
chgrp
umask
chmod
Copy, Move
Remove & Time
cp
ln
mv
rm
touch
mkdir
rm
removes a file
rmdir
removes a directory
du
df
touch
find
file
pwd
directory
Command Structure
General Structure:
Command Name
[Options]
[Arguments]
E.g.,
Command Name
Tells the shell what to do
(in this case list files)
Options
Control how the command will work
(in this case ask for a long listing)
Arguments
on which the command works
(in this case the directory
usr/trg )
ls
-l
/usr/trg
Directory Management
cd
cd..
cd.
e.g.,
$ cd /usr/trg/c
$ cd ..
$ cd ./c
or $ cd c
$ cd
mkdir
$ mkdir pathname
rmdir
*
rm -r
Remove a Directory
$ rmdir pathname
e.g.,
$ rmdir /usr/trg2
(removes directory
trg2)
pwd
Directory
Print Working
File Contents
cat
Options -
characters
-b
lines
e.g.,
$ cat try.c
$ cat
displays on
$ cat f1 > f2
$ cat f2 > f3
$ cat f4 >> f3
$ cat try[0-3] > final
$ cat test* > report
Is[Options]
Options
-1
-l
-a
-s
-i
inode no.
-t
$ Is -I
List the files along with the protection bits and the user
$ Is -a
$ Is -1
symtab.c
symtab.o
treegen
test
$ Is -I
$ Is -a
$ Is -iI
..
.profile
.cshrc
symtab.c
...
wc
Word Count
$wc [Options] filename
Options
e.g.,
nl
the file
Display
-I
Display
-w
Display
-c
Display
$ wc test.c
20 200 5678
20 - lines
200 - words
5678- characters
no.
no.
no.
no.
of
of
of
of
file
Options
-f
filelist
Normal File Types
C program text
assembler program text
commands text
ASCII text
English text
e.g.,
$ file test.c
C Program test
cp
copy a file
-i
e.g.,
In
e.g.,
$ ln first.c second.c
The file is referenced by two different names
No Extra disk storage
mv
interactive mode
e.g.,
rm
$ mv old.c new.c
Renames the file old.c as new.c
Deletes the indicated file(s) files
rm
-i
-f
-r
touch
e.g.,
File Comparison
cmp
0
1
2
e.g.,
diff
convert
e.g.,
$ diff test1 test2
Outputs:
n1 a n3,n4
n1,n2 d n3
n1,n1 c n3,n4
where
*
*
respectively
comm
$comm -[123] f1 f2
Prints a three column output:
- lines that occur only in f1
- lines that occur only in f2
- lines that occur in both
comm -12
- prints lines common to the two files
comm -23
- prints only lines in the first file but not in
the
second
comm -123
- prints nothing
e.g.,
$ comm test1 test2
File Compression
pack
$ pack <filename>
e.g.,
$ pack try
- Creates a file try.z which is packed
- Normally the executables are packed
- The size is reduced by 25 - 40 %
unpack
or
pcat
e.g.,
$ unpack try.z
or
$ pcat try.z
unpacks the file try.z
mount
e.g.,
umount
e.g.,
File Security
passwd
chown
file
chmod
e.g.,
$ chmod a=rw test.c
* users, group, others have read
and write permissions
$ chmod u+r, g+w, o+x test.c
* read for users write for groups execute for others
$ chmod 777 test.c
* Sets read, write, execute Permissions
umask
$ umask nnn
(nnn set file creation mode)
umask can also be set as a shell variable
e.g.,
umask 022
- Files normally created with 777 mode is assigned
755 permission
The value of each digit is subtracted from the corresponding "digit"
specified by the system for the creation of a file.
File Operation
tail
head
split
lines
Chapter 3
BASIC
UTILITIES
Basic Utilities
1.
Line Editor
Ed
2.
Visual Editor Vi
3.
Debuggers
4.
Profiling Tools
5.
C-Beautification
$ ed filename
w
Exiting ed :
q
Example :
Quit from ed
$ ed test.c
? test.c
P
*a
<>
<>
<>
*w
*q
The edit buffer can be written to some other file also as *w filename
Pattern Matching
abc
abc
a*bc
abc,
aabc,
aaa...bc
aac,
acc,
axc,
abc,
axyc,
a$+pmc,
a[b2m]c
abc,
a2c,
amc
a[0-2b-d]c
adc
a0c,
a1c,
a2c,
a[^0-2]c
a3c,
a5c,
axc, ....
a[xy]*c
ac,
axc,
axxc,
a\{3,\}
aaa,
aaaa -
3 or more as
a\{3,5\}
aaa,
aaaa,
aaaaa
between 3 & 5 as
a.c
a.*c
abcdefgc
abc,
a$c
abc,
ayyyc
acc,
Explanation
print current line (s)
1,3p
2,4l
characters like tabs etc.)
number before it
1,5n
a
3i
4a
line
Command
Explanation
.=
Current line is n
+n
Examples
*-
*-
Current line
Move up by n lines
-n
/.../
Previous
*$= Prints
total no. lines
but does not
changecurrent
line
Modifying Text
Command
Explanation
Usage
d
2,5d
c
j
u
2,4c
2,3m5
2,3t5
2,3j
Substitute Commands
(n,n)s/oldstr/newstr
replaces oldstr with newstr (n,n) range of
lines - optional
s/oldstr/newstr/g all oldstrs' on the current line
are replaced with newstr
s/oldstr/newstr/p the replacement is only effected in print but not
executed
Finding Text
/^ pattern/
/pattern $/
line
/pattern/
//
??
forward search
backward search
Command Execution
Command
Explanation
Special ed Commands
Command
Explanation
w file2
r file2
e file2
!shellcmd
The ex editor
*
an improved version of ed
*
less terse
*
provides display options like
numbered
line
*
allows shorthand versions of
commands
*
clear response for error messages
editor
*
*
*
*
*
*
Invoking
$ vi filename
e.g., $ vi pgm.c
Modes :
*
*
*
Command mode
Insert mode
From Insert mode Pressing
<ESC> remitts Command mode
Types of commands
* vi-commands (invisible)
Command mode
Commands can be categorised as :
* Cursor movement
* Text manipulations
- insert, delete, copy, change
* Marking/Selecting, Positioning
* Search
Objects of interest recognized by vi:
* characters
* words
* lines
* block
Format of commands
[count] command [operand]
Use . to repeat last command
Use u to undo last command
Cursor Movement
Line Oriented :
^ or (zero)
$
Character oriented
h
l
j
k
beginning of line
end of line
move
move
move
move
left
right
down
up
Block Oriented :
)
^g
& )
Text Manipulation
Insert mode :
a
A
i
I
o
O
esc
Delete mode :
u
nx
ndw
ndd
Changing Text :
mode
nr
s
replace char
delete current char, enter insert
S
cw
yank n line
Named Buffers : a to z ( 26 )
Unnamed Buffers : 1
print buffer
Join :
Search
Searching for a character :
fc
tc
Fc
Tc
continue
search
in
reverse direction
Search
File Related :
G
nG
Text Manipulation
Screen Adjustments :
^d
^u
^f
Page forward
^b
Page backward
^e
^y
Text Manipulation
Searching for a string :
/string
?string
s/oldstr/newstr
set noai
set nu
set wrapmargin = n
set bf
set
Text Manipulation
:w!
:q
quit normally
:q!
quit, no write
:n
:n!
:n args
PROFILING TOOLS :
PROF
and
GPROF
*
The program should have been compiled with -p
option
for prof and -g option for gprof
*
*
Gives an idea as to which function used up the
maximum
time
Example of prof
$ cc -p test.c
$ a.out
$ prof a.out
Generates an output like....
%time
msec/call
91.2
7.2
1.0
cumsecs
name
9.34
9.34
10.08
0.74
10.18
0.10
0.0
10.24
%time
cumsecs
seconds
0.06
seconds
20100
#calls
_func2
1
100
1
calls
name
Function name
acnt
_func1
_main
Chapter 4
Shell Features
Shell Features
Shell Features
1.
2.
3.
4.
5.
6.
7.
8.
1.
Bourne Shell
sh
2.
C Shell
csh
3.
Korn Shell
ksh
4.
Restricted Shell
5.
Visual Shell
rsh
vsh
Shell Commands
Simple commands using shell Metacharacters
Exit status
Redirection
file
file
Pipeline
Default
Default
<
>
>>
n>&m
sequential execution
&
asynchronous execution
&&
returns
||
returns
$var
zero value
...
...
...
... and \
e.g.,
$ (sleep 5 ; echo The date is `date` ) & date
The Output of the above command will be
6345
Wed Sep 23 10:20:45 EDT 1991
$The date is Wed Sep 23 10:20:50 EDT 1991
PATH
*
e.g.,
PATH = :.:/usr/bin:/bin:
Executes .logout for C Shell when user logs out from C-Shell
Shell Variables
User defined
assignment : name = value
e.g., (on Bourne shell)
$
$
$
$
$
PATH=$PATH:/usr/games
export PATH
echo $PATH
:.:/usr/bin:/bin:/usr/games:
Positional Parameters
e.g.,
shellscript
arg2
$0
arg1
$1
$2
* ${var}
characters
e.g.,
$
var=hello
varx=goodbye
echo $var
hello
echo $varx
goodbye
echo ${var}x
hellox
echo ${junk?}
junk : parameter not set
${var-thing}
* ${var=thing}
thing
e.g.,
echo ${junk?}
junk : parameter not set
echo ${junk?}
Hi there
* $ {var+thing}
nothing
Command Environment
*
Shell 1
Shell 2
Export A
e.g.,
export A
*
$?
$$
$*
$!
HOME
login dir
PATH
PS1
PS2
IFS
TERM
terminal type
EXINIT
The C Shell
Permits aliasing
History
execute previous
Command Format :
! <event> [ <word> <modifier> ]
<event>
- absolute
- relative
- name
expr
<word>-
!1
!-3
!c
!?su?
absolute
0, 1, ...n
range
a-e
last
$
All arguments *
<modifier>
- Substitute
:s/trg1/trg
- Print only
:s/trg1/trg/p
History Mechanism
Keeps track of commands
% history
% ^old^new
e.g.,
% cd /usr/trg/SRC
% ^SRC^src
% cd /usr/trg/src
% !15
% !c
beginning with c
Run command 15
Run previous command
Changing
Commands
:S/pattern/replacement
e.g.,
% !cd:s/trg/trg1
cd /usr/trg1/src
% !ls : p
Prints the previous command
beginning with ls
but does not execute it
Using Arguments
!$
last argument
!^
Begin argument
!*
All arguments
!10:5-9
command 10
e.g.,
%ls !15:2-4
Arguments 5 to 9 for
Alias mechanism
Short names for frequently-used long commands
e.g.,
%
%
%
Filename Grouping
{
%cp
%ls
{/usr/bin,/bin}/{ls,more}
/usr/bin/ls : not found
/bin/ls
/usr/bin/more
/bin/more
: not found
Using C - Shell
e.g.,
% cc test.c
% vi test.c
% !c
% a.out
% !v
% !c - o test
% test
% car /usr/trg2/document/report
% !! : s/car/cat
or
% ^car ^cat
% set variable=value
histchars :
% set histchars=";,"
will thereafter use
; as execution character
, as substitution character
history :
% set history=40
path:
% set path=:.:/bin:/usr/bin:
prompt :
% set prompt=\!>
setenv
filec
% set filec
Its usage :
:
file name completion
:
Predefined
% set variable
% unset variable
ignoreeof
% set ignoreeof
noclobber
:
redirection
% set noclobber
appends to
Test Conditions
TESTing Strings
test - z $string String length equal to zero
test - n $string String length not equal to zero
test $str1 = $str2
test $string
File related
$ test -f file
$ test -r file
Shell Programming
- The Bourne Shell
Control flows
if command
then
commands
[ elif commands ] ...
[ else commands ]
fi
case word in,
pattern) commands ;;
pattern) commands ;;
...
esac
break n
continue n
exit n
export [name..]
export
test expr
evaluate conditions
read [arg]
shift n
Examples
$
>
>
>
>
>
>
>
for i in *.dat
do
size=wc -c $i
if test $size -eq 0
then
rm $i
fi
done
Example
IF
Control Flows
if (expr) then
command;
[ else if ]
command;
[ else ]
command;
endif
SWITCH
switch (string)
command;
breaksw
case string2:
command;
breaksw
default :
command;
breaksw
case string1:
FOREACH
WHILE
while ( expr )
commands
end
UNTIL
until ( expr )
commands
end
Chapter 5
Advanced Utilities
Advanced Utilities
1.
2.
3.
4.
5.
6.
7.
join, csplit
8.
9.
10.
11.
12.
13.
14.
15.
16.
Redirection
Pipes
Pipe fitting with tee utility
Filters
Find
Sort
Utilities that process tabular data- cut,
Ps and Kill
Timely Execution- nohup, at, time
User backup utilities- tar, cpio, dd , doscp
Output related commands- pr, lpr,lp
Other utilities- sleep, sync, wait, clear, tr
Introduction to Make
Lint
Lex
Yacc
Redirection
e.g.,
Metacharacter
Performs
<
input
>
output write
>>
output append
$ who
trg1
trg2
tty00
tty02
Apr 8
Apr 8
09:33
11:10
Apr 8
Apr 8
09:33
11:10
$ cat date_out
Fri
Apr 8
14 : 30 :
10 est
14 : 30 : 10 est
Apr 8
09:33
Apr 8
11:10
1983
Pipes :
Metacharacter
|
Performs
piping
e.g.,
Without Pipe
With Pipe
A UNIX Pipeline
Standard Input
>Prgm #1
Prgm #2
Prgm #3
>Prgm #5
Standard Output<
Prgm #4
Prgm #6
FILTERS :
grep
fgrep
egrep
powerful
time
-c
-i
ignore case
-n
-v
e.g.,
$ grep -ni func1 *.c
that match
ignoring the case
$ grep * *
$ ls -l | grep ^d
e.g.,
$ egrep int|long test.c
Searches for all those lines
containing
either int or long in test.c
$ egrep (^[A-Z]) testfile
$ cat exprfile
commas
,.*,.*,
exprfile
$ egrep -n -f exprfile testfile
Find
To locate one or more files
find path-list cond-list action-list
path-list
cond-list
-type x
-user name
-group name
-size n
-links n
-atime n
-mtime n
-ctime n
-perm nnn
-name fname
x can be d, f, or c
d directory
f ordinary file
c character special file
actionlist
- print
- exec command
- cpio device
- ok command
e.g.,
find / -atime 7 -print
find / -atime +7 -print
find / -atime -7 -print
Examples
$
$
$
$
$
find
find
find
find
find
Sort
*
*
line.
ignore case
n
r
tp
u
o filenameout
numeric sorting
reverse order
use p as separator
output only lines with unique keys
save output in filenameout
e.g.,
$ sort -t: +2n -3 /etc/passwd
Print the passwd file sorted by numeric user id
$ who | sort +4n
Sorts according to the login time stamp
of the user
uniq
tsort
*
which
Cut (contd.)
cut -c list { file1 file2 ...}
e.g.,
cut -c 5-70 file1
cut would pass 5-70 characters from file1
cut -flist {-d char } {file1 file2....}
e.g.,
who | cut -d -f1
gives a list of user login names
cut -d: -f 1,5 /etc/passwd
gives a list of user ID and names
JOIN
*
*
files
-on.m
-tc
e.g.,
$ join -j1 4 -j2 3 -o 1.1 2.1 1.6 -t: etc/passwd
etc/group
-
group
name
login
dir
csplit
context split
-k
xx
csplit(contd.)
e.g.,
$ csplit -f cobol inpfile /Procedure division/
/ Para5./ /Para6./ /Para 7./
*
*
*
PS, kill
$ somecommand &
5511 - pid
$ps
pid
tty
time
3432
5765
5511
2
2
2
0 : 24
0 : 03
0 : 51
tty
time
2
2
0 : 24
0 : 03
$ kill 5511
$ ps
pid
3432
5985
command
-sh
ps
somecommand
command
-sh
ps
$ stubborn-cmd &
pid
3432
6004
5995
$ kill 5995
tty
2
2
2
time
0 : 24
0 : 03
0 : 44
command
-sh
ps
stubborn-cmd
pid
tty
3432 2
6004 2
5995 2
$ kill -9 5995
time
0 : 24
0 : 03
0 : 44
command
-sh
ps
stubborn-cmd
time
0 : 24
0 : 03
command
-sh
ps
$ ps
$ ps
pid
3432
6103
tty
2
2
Timely Execution :
time
Time a command
signals
or interrupts
The standard output is sent to nohup.out
e.g.,
$ nohup du / &
820
Sending output to nohup.out
$ logout
nice
*
*
$ nice +n
Raise priority
$ nice -n
Lower priority
$ nice n
set priority
at
$ at 5 pm
echo ^G ^G Time to logoff >/dev/tty04
^D
$
tar
tape archiver
Copies files on backup medium such as floppy
or tape in tar format
t
x
v
f
tar (example)
$ tar cvf /dev/fd096 *
directory
medium /dev/fd096
$ tar xvf /dev/fd096 Extracts all the files from the backup
medium
/dev/fd096 onto the
current directory
Creates necessary directories
tar xvf /dev/fd096 try.c
/dev/fd096
tar tvf /dev/fd096
cpio
*
Copy files archives in and out
*
Bundles all the files into one package
cpio [options] file list devicename
Options
-o
-i
-p
-v
-c
character information in ASCII for portability
considerations
-t
-O
-d
-k
cpio (Examples)
dd
dd {option = value }
Options
Values
if
of
ibs
obs
cbs
skip
seek
count
conv
e.g.,
$ dd if=/dev/rmt/0n of=x ibs=800 cbs=80 conv=ascii,lcase
df
clear
goes to
tr
e.g.,
Chapter 6
Communication Features
Communication Features
1.
2.
Networking commands
uucp
uuto
rlogin
telnet
ftp
rcp
uux
ct
cu
Write
Talk
Post
News
Mesg
users
Sending mail on local system
At the end of the message press ^D to exit
$ mail <loginnames>
Example : (Assume the current user is trg)
$ mail trg1
Sending mail on remote system
$ mail recipient@remote_system.domain_info
Receiving mail
$ mail
?
q
s [file]
w [file]
d
n or +
r
m [user]
! cmd
Prompt
quit
save (defalut mbox)
write without header
delete
next message
previous
reply to the sender
mail to user
run the shell command
Write
from
another terminal
The message is ended by pressing ^D.
$ write <username>
Talk
two
different
terminals.
$ talk <username>
Post
Messages can be posted on a bulletin board for
previewing by all users.
News
first
*
Keeps the user informed of current events
Refers file /usr/news
e.g.,
$ news
$ mesg [option]
option : - y
write allowed
-n
mesg is y or n
$ mesg
Networking Commands
*
uucp
uuto
telnet
rlogin
ftp
rcp
uux
ct
cu
uucp
to
another
* Refers file uuname and uulog
uucp [options] source-files destination files
options
uuto
*
*
*
Allows remote login over the network
*
Similar to telnet, but a much more flexible
facility.
$ rlogin system_name!username
ftp
*
*
$ ftp
ftp > open sys1
Name login:
passwd
ftp > ?
rcp
facility
uux
remote
redirect the
stdout to a file on a specified computer
$ uux [options] commandstring
All shell special characters must be quoted
$ pr minutes | uux -p host!lp
Command line queues the file minutes to be printed
on the area of the computer host
ct
*
*
modem
This command dials the phone number of the modem
$ ct [options] telno
$ ct -h -w5 -s1200 9=5553497
1200
is not
local
cu
*
*
simultaneously
Examples
cu -s2400 9=5557867
Connected
login :
~%take proposal
computer to local
~%put minutes minutes.bak
file minutes on the
minutes.bak
~%!comma
~%$command
output
~%.