You are on page 1of 44

UNIX Introduction

What is UNIX?
UNIX is an operating system which was first developed in the 1960s, and has een !nder
constant development ever since" #y operating system, we mean the s!ite of programs
which ma$e the comp!ter wor$" It is a stale, m!lti%!ser, m!lti%tas$ing system for servers,
des$tops and laptops"
UNIX systems also have a graphical !ser interface &'UI( similar to )icrosoft Windows which
provides an easy to !se environment" *owever, $nowledge of UNIX is re+!ired for
operations which aren,t covered y a graphical program, or for when there is no windows
interface availale, for e-ample, in a telnet session"
.ypes of UNIX
.here are many different versions of UNIX, altho!gh they share common similarities" .he
most pop!lar varieties of UNIX are /!n /olaris, 'NU01in!-, and )ac2/ X"
*ere in the /chool, we !se /olaris on o!r servers and wor$stations, and 3edora 1in!- on the
servers and des$top 45s"
.he UNIX operating system
.he UNIX operating system is made !p of three parts6 the $ernel, the shell and the
programs"
The kernel
.he $ernel of UNIX is the h! of the operating system7 it allocates time and memory to
programs and handles the filestore and comm!nications in response to system calls"
8s an ill!stration of the way that the shell and the $ernel wor$ together, s!ppose a !ser
types rm myfile&which has the effect of removing the file myfile(" .he shell searches the
filestore for the file containing the program rm, and then re+!ests the $ernel, thro!gh
system calls, to e-ec!te the program rm on myfile" When the process rm myfile has
1
finished r!nning, the shell then ret!rns the UNIX prompt 9 to the !ser, indicating that it is
waiting for f!rther commands"
The shell
.he shell acts as an interface etween the !ser and the $ernel" When a !ser logs in, the
login program chec$s the !sername and password, and then starts another program called
the shell" .he shell is a command line interpreter &51I(" It interprets the commands the !ser
types in and arranges for them to e carried o!t" .he commands are themselves programs7
when they terminate, the shell gives the !ser another prompt &9 on o!r systems("
.he adept !ser can c!stomise his0her own shell, and !sers can !se different shells on the
same machine" /taff and st!dents in the school have the tcsh shell y defa!lt"
.he tcsh shell has certain feat!res to help the !ser inp!tting commands"
3ilename 5ompletion % #y typing part of the name of a command, filename or directory and
pressing the :Tab; $ey, the tcsh shell will complete the rest of the name a!tomatically" If
the shell finds more than one name eginning with those letters yo! have typed, it will
eep, prompting yo! to type a few more letters efore pressing the ta $ey again"
*istory % .he shell $eeps a list of the commands yo! have typed in" If yo! need to repeat a
command, !se the c!rsor $eys to scroll !p and down the list or type history for a list of
previo!s commands"
3iles and processes
<verything in UNIX is either a file or a process"
8 process is an e-ec!ting program identified y a !ni+!e 4I= &process identifier("
8 file is a collection of data" .hey are created y !sers !sing te-t editors, r!nning compilers
etc"
<-amples of files7
a doc!ment &report, essay etc"(
the te-t of a program written in some high%level programming lang!age
2
instr!ctions comprehensile directly to the machine and incomprehensile to a cas!al
!ser, for e-ample, a collection of inary digits &an e-ec!tale or inary file(6
a directory, containing information ao!t its contents, which may e a mi-t!re of
other directories &s!directories( and ordinary files"
.he =irectory /tr!ct!re
8ll the files are gro!ped together in the directory str!ct!re" .he file%system is arranged in a
hierarchical str!ct!re, li$e an inverted tree" .he top of the hierarchy is traditionally
called root &written as a slash 0 (
In the diagram aove, we see that the home directory of the !ndergrad!ate
st!dent "ee51vn" contains two s!%directories &docs and pics( and a file
called report.doc"
.he f!ll path to the file report.doc is "/home/its/u1/ee51vn/report.doc"
/tarting an UNIX terminal
.o open an UNIX terminal window, clic$ on the >.erminal> icon from 8pplications08ccessories
men!s"
3

8n UNIX .erminal window will then appear with a 9 prompt, waiting for yo! to start
entering commands"

4
UNIX Tutorial !ne
1"1 1isting files and directories
ls "list#
When yo! first login, yo!r c!rrent wor$ing directory is yo!r home directory" ?o!r home
directory has the same name as yo!r !ser%name, for e-ample, ee$1ab, and it is where yo!r
personal files and s!directories are saved"
.o find o!t what is in yo!r home directory, type
% ls
.he ls command & lowercase 1 and lowercase / ( lists the contents of yo!r c!rrent wor$ing
directory"
.here may e no files visile in yo!r home directory, in which case, the UNIX prompt will e
ret!rned" 8lternatively, there may already e some files inserted y the /ystem
8dministrator when yo!r acco!nt was created"
ls does not, in fact, ca!se all the files in yo!r home directory to e listed, !t only those
ones whose name does not egin with a dot &"( 3iles eginning with a dot &"( are $nown as
hidden files and !s!ally contain important program config!ration information" .hey are
hidden eca!se yo! sho!ld not change them !nless yo! are very familiar with UNIX@@@
5
.o list all files in yo!r home directory incl!ding those whose names egin with a dot, type
% ls -a
8s yo! can see, ls -a lists files that are normally hidden"
ls is an e-ample of a command which can ta$e options7 %a is an e-ample of an option" .he
options change the ehavio!r of the command" .here are online man!al pages that tell yo!
which options a partic!lar command can ta$e, and how each option modifies the ehavio!r
of the command" &/ee later in this t!torial(
1"A )a$ing =irectories
mkdir "make directory#
We will now ma$e a s!directory in yo!r home directory to hold the files yo! will e creating
and !sing in the co!rse of this t!torial" .o ma$e a s!directory called !ni-st!ff in yo!r
c!rrent wor$ing directory type
% mkdir unixstuff
.o see the directory yo! have B!st created, type
% ls
1"C 5hanging to a different directory
6
cd "chane directory#
.he command cd directory means change the c!rrent wor$ing directory to 'directory'" .he
c!rrent wor$ing directory may e tho!ght of as the directory yo! are in, i"e" yo!r c!rrent
position in the file%system tree"
.o change to the directory yo! have B!st made, type
% cd unixstuff
.ype ls to see the contents &which sho!ld e empty(
&'ercise 1a
)a$e another directory inside the uni'stuff directory called backups
1"D .he directories " and ""
/till in the uni'stuff directory, type
% ls -a
8s yo! can see, in the uni'stuff directory &and in all other directories(, there are two
special directories called &.( and &..(
The current directory ".#
In UNIX, &.( means the c!rrent directory, so typing
% cd .
N2.<7 there is a space etween cd and the dot
means stay where yo! are &the uni'stuff directory("
.his may not seem very !sef!l at first, !t !sing &.( as the name of the c!rrent directory will
save a lot of typing, as we shall see later in the t!torial"
The parent directory "..#
7
&..( means the parent of the c!rrent directory, so typing
% cd ..
will ta$e yo! one directory !p the hierarchy &ac$ to yo!r home directory(" .ry it now"
Note7 typing cd with no arg!ment always ret!rns yo! to yo!r home directory" .his is very
!sef!l if yo! are lost in the file system"
1"E 4athnames
p(d "print (orkin directory#
4athnames enale yo! to wor$ o!t where yo! are in relation to the whole file%system" 3or
e-ample, to find o!t the asol!te pathname of yo!r home%directory, type cd to get ac$ to
yo!r home%directory and then type
% pwd
.he f!ll pathname will loo$ something li$e this %
/home/its/ug1/ee51vn
which means that ee51vn &yo!r home directory( is in the s!%directory u1 &the gro!p
directory(,which in t!rn is located in the its s!%directory, which is in the home s!%
directory, which is in the top%level root directory called > / " "
8
&'ercise 1b
Use the commands cd, ls and pwd to e-plore the file system"
&Fememer, if yo! get lost, type cd y itself to ret!rn to yo!r home%directory(
1"6 )ore ao!t home directories and pathnames
Understandin pathnames
3irst type cd to get ac$ to yo!r home%directory, then type
% ls unixstuff
to list the conents of yo!r !ni-st!ff directory"
Now type
% ls backups
?o! will get a message li$e this %
backups !o such file or directory
9
.he reason is, backups is not in yo!r c!rrent wor$ing directory" .o !se a command on a file
&or directory( not in the c!rrent wor$ing directory &the directory yo! are c!rrently in(, yo!
m!st either cd to the correct directory, or specify its f!ll pathname" .o list the contents of
yo!r ac$!ps directory, yo! m!st type
% ls unixstuff/backups

) "your home directory#
*ome directories can also e referred to y the tilde ) character" It can e !sed to specify
paths starting at yo!r home directory" /o typing
% ls "/unixstuff
will list the contents of yo!r !ni-st!ff directory, no matter where yo! c!rrently are in the file
system"
What do yo! thin$
% ls "
wo!ld list?
What do yo! thin$
% ls "/..
wo!ld list?
/!mmary
*ommand +eanin
ls list files and directories
ls -a list all files and directories
10
mkdir ma$e a directory
cd directory change to named directory
cd change to home%directory
cd " change to home%directory
cd .. change to parent directory
pwd display the path of the c!rrent directory
UNIX Tutorial T(o
A"1 5opying 3iles
cp "copy#
cp file1 file2 is the command which ma$es a copy of file1 in the c!rrent wor$ing
directory and calls itfile,
What we are going to do now, is to ta$e a file stored in an open access area of the file
system, and !se the cpcommand to copy it to yo!r !ni-st!ff directory"
3irst, cd to yo!r uni'stuff directory"
% cd "/unixstuff
.hen at the UNIX prompt, type,
% cp /vol/examples/tutorial/science.txt .
Note7 =on,t forget the dot . at the end" Fememer, in UNIX, the dot means the c!rrent
directory"
.he aove command means copy the file science.t't to the c!rrent directory, $eeping the
name the same"
11
&Note7 .he directory /vol/e'amples/tutorial/ is an area to which everyone in the school
has read and copy access" If yo! are from o!tside the University, yo! can gra a copy of the
file here" Use ,3ile0/ave 8s"", from the men! ar to save it into yo!r uni'stuff directory"(
&'ercise ,a
5reate a ac$!p of yo!r science.t't file y copying it to a file called science.bak
A"A )oving files
mv "move#
mv file1 file2 moves &or renames( file1 to file,
.o move a file from one place to another, !se the mv command" .his has the effect of
moving rather than copying the file, so yo! end !p with only one file rather than two"
It can also e !sed to rename a file, y moving the file to the same directory, !t giving it a
different name"
We are now going to move the file science"a$ to yo!r ac$!p directory"
3irst, change directories to yo!r !ni-st!ff directory &can yo! rememer how?(" .hen, inside
the uni'stuffdirectory, type
% mv science.bak backups/.
.ype ls and ls ac$!ps to see if it has wor$ed"
A"C Femoving files and directories
rm "remove#- rmdir "remove directory#
.o delete &remove( a file, !se the rm command" 8s an e-ample, we are going to create a
copy of thescience.t't file then delete it"
Inside yo!r uni'stuff directory, type
12
% cp science.txt tempfile.txt
% ls
% rm tempfile.txt
% ls
?o! can !se the rmdir command to remove a directory &ma$e s!re it is empty first(" .ry to
remove thebackups directory" ?o! will not e ale to since UNIX will not let yo! remove a
non%empty directory"
&'ercise ,b
5reate a directory called tempstuff !sing mkdir , then remove it !sing
the rmdir command"
A"D =isplaying the contents of a file on the screen
clear "clear screen#
#efore yo! start the ne-t section, yo! may li$e to clear the terminal window of the previo!s
commands so the o!tp!t of the following commands can e clearly !nderstood"
8t the prompt, type
% clear
.his will clear all te-t and leave yo! with the 9 prompt at the top of the window"

cat "concatenate#
.he command cat can e !sed to display the contents of a file on the screen" .ype7
% cat science.txt
8s yo! can see, the file is longer than than the siGe of the window, so it scrolls past ma$ing
it !nreadale"

13
less
.he command less writes the contents of a file onto the screen a page at a time" .ype
% less science.txt
4ress the :space%bar; if yo! want to see another page, and type :.; if yo! want to +!it
reading" 8s yo! can see, less is !sed in preference to cat for long files"

head
.he head command writes the first ten lines of a file to the screen"
3irst clear the screen then type
% head science.txt
.hen type
% head -5 science.txt
What difference did the %E do to the head command?

tail
.he tail command writes the last ten lines of a file to the screen"
5lear the screen and type
% tail science.txt
H" *ow can yo! view the last 1E lines of the file?

A"E /earching the contents of a file
14
/imple searchin usin less
Using less, yo! can search tho!gh a te-t file for a $eyword &pattern(" 3or e-ample, to
search thro!ghscience.t't for the word 0science0, type
% less science.txt
then, still in less, type a forward slash :/; followed y the word to search
/science
8s yo! can see, less finds and highlights the $eyword" .ype :n; to search for the ne-t
occ!rrence of the word"

rep "don0t ask (hy it is called rep#
grep is one of many standard UNIX !tilities" It searches files for specified words or patterns"
3irst clear the screen, then type
% grep science science.txt
8s yo! can see, grep has printed o!t each line containg the word science"
2r has it ????
.ry typing
% grep #cience science.txt
.he grep command is case sensitive6 it disting!ishes etween /cience and science"
.o ignore !pper0lower case distinctions, !se the %i option, i"e" type
% grep -i science science.txt
.o search for a phrase or pattern, yo! m!st enclose it in single +!otes &the apostrophe
symol(" 3or e-ample to search for spinning top, type
15
% grep -i $spinning top$ science.txt
/ome of the other options of grep are7
%v display those lines that do N2. match
%n precede each matching line with the line n!mer
%c print only the total co!nt of matched lines
.ry some of them and see the different res!lts" =on,t forget, yo! can !se more than one
option at a time" 3or e-ample, the n!mer of lines witho!t the words science or /cience is
% grep -ivc science science.txt

(c "(ord count#
8 handy little !tility is the wc command, short for word co!nt" .o do a word co!nt
on science.t't, type
% wc -w science.txt
.o find o!t how many lines the file has, type
% wc -l science.txt
/!mmary
*ommand +eanin
cp file1 file2 copy file1 and call it fileA
mv file1 file2 move or rename file1 to fileA
rm file remove a file
rmdir directory remove a directory
cat file display a file
16
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for $eywords
wc file co!nt n!mer of lines0words0characters in file
UNIX Tutorial Three
C"1 Fedirection
)ost processes initiated y UNIX commands write to the standard o!tp!t &that is, they write
to the terminal screen(, and many ta$e their inp!t from the standard inp!t &that is, they
read it from the $eyoard(" .here is also the standard error, where processes write their
error messages, y defa!lt, to the terminal screen"
We have already seen one !se of the cat command to write the contents of a file to the
screen"
Now type cat witho!t specifing a file to read
% cat
.hen type a few words on the $eyoard and press the :1eturn; $ey"
3inally hold the :*trl; $ey down and press :d; &written as 23 for short( to end the inp!t"
What has happened?
If yo! r!n the cat command witho!t specifing a file to read, it reads the standard inp!t &the
$eyoard(, and on receiving the ,end of file, &23(, copies it to the standard o!tp!t &the
screen("
In UNIX, we can redirect oth the inp!t and the o!tp!t of commands"
17
C"A Fedirecting the 2!tp!t
We !se the I symol to redirect the o!tp!t of a command" 3or e-ample, to create a file
called list1 containing a list of fr!it, type
% cat % list1
.hen type in the names of some fr!it" 4ress :1eturn; after each one"
pear
banana
apple
&' (this means press )*trl+ and )d+ to stop,
What happens is the cat command reads the standard inp!t &the $eyoard( and the I
redirects the o!tp!t, which normally goes to the screen, into a file called list1
.o read the contents of the file, type
% cat list1
&'ercise 4a
Using the aove method, create another file called list, containing the following fr!it7
orange, pl!m, mango, grapefr!it" Fead the contents of list,

4.,.1 5ppendin to a file
.he form II appends standard o!tp!t to a file" /o to add more items to the file list1, type
% cat %% list1
.hen type in the names of more fr!it
peach
grape
18
orange
&' -*ontrol ' to stop.
.o read the contents of the file, type
% cat list1
?o! sho!ld now have two files" 2ne contains si- fr!it, the other contains fo!r fr!it"
We will now !se the cat command to Boin &concatenate( list1 and list, into a new file
called bilist" .ype
% cat list1 list/ % biglist
What this is doing is reading the contents of list1 and list, in t!rn, then o!tp!ting the te-t
to the file bilist
.o read the contents of the new file, type
% cat biglist
C"C Fedirecting the Inp!t
We !se the J symol to redirect the inp!t of a command"
.he command sort alphaetically or n!merically sorts a list" .ype
% sort
.hen type in the names of some animals" 4ress :Fet!rn; after each one"
dog
cat
bird
ape
&' -control d to stop.
.he o!tp!t will e
19
ape
bird
cat
dog
Using J yo! can redirect the inp!t to come from a file rather than the $eyoard" 3or
e-ample, to sort the list of fr!it, type
% sort 0 biglist
and the sorted list will e o!tp!t to the screen"
.o o!tp!t the sorted list to a file, type,
% sort 0 biglist % slist
Use cat to read the contents of the file slist
C"D 4ipes
.o see who is on the system with yo!, type
% who
2ne method to get a sorted list of names is to type,
% who % names.txt
% sort 0 names.txt
.his is a it slow and yo! have to rememer to remove the temporary file called names
when yo! have finished" What yo! really want to do is connect the o!tp!t of the who
command directly to the inp!t of the sort command" .his is e-actly what pipes do" .he
symol for a pipe is the vertical ar K
3or e-ample, typing
% who 1 sort
will give the same res!lt as aove, !t +!ic$er and cleaner"
20
.o find o!t how many !sers are logged on, type
% who 1 wc -l
&'ercise 4b
Using pipes, display all lines of list1 and list, containing the letter ,p,, and sort the res!lt"
8nswer availale here
/!mmary
*ommand +eanin
command % file redirect standard o!tp!t to a file
command %% file append standard o!tp!t to a file
command 0 file redirect standard inp!t from a file
command1 1 command2
pipe the o!tp!t of command1 to the inp!t of
commandA
cat file1
file2 % file0
concatenate file1 and fileA to file0
sort sort data
who list !sers c!rrently logged in
UNIX Tutorial 6our
D"1 Wildcards
The 7 (ildcard
.he character 2 is called a wildcard, and will match against none or more character&s( in a
file &or directory( name" 3or e-ample, in yo!r uni'stuff directory, type
21
% ls list2
.his will list all files in the c!rrent directory starting with list....
.ry typing
% ls 2list
.his will list all files in the c!rrent directory ending with ....list
The 8 (ildcard
.he character 3 will match e-actly one character"
/o 8ouse will match files li$e house and mouse, !t not rouse"
.ry typing
% ls 3list

D"A 3ilename conventions
We sho!ld note here that a directory is merely a special type of file" /o the r!les and
conventions for naming files apply also to directories"
In naming files, characters with special meanings s!ch as / 7 9 : , sho!ld e avoided"
8lso, avoid !sing spaces within names" .he safest way to name a file is to !se only
alphan!meric characters, that is, letters and n!mers, together with L &!nderscore( and "
&dot("
;ood filenames <ad filenames
proBect"t-t proBect
myLigLprogram"c my ig program"c
fredLdave"doc fred M dave"doc
22
3ile names conventionally start with a lower%case letter, and may end with a dot followed y
a gro!p of letters indicating the contents of the file" 3or e-ample, all files consisting of 5
code may e named with the ending .c, for e-ample, pro1.c " .hen in order to list all files
containing 5 code in yo!r home directory, yo! need only type ls 2.c in that directory"
D"C 'etting *elp
!n%line +anuals
.here are on%line man!als which gives information ao!t most commands" .he man!al
pages tell yo! which options a partic!lar command can ta$e, and how each option modifies
the ehavio!r of the command" .ypeman command to read the man!al page for a partic!lar
command"
3or e-ample, to find o!t more ao!t the wc &word co!nt( command, type
% man wc
8lternatively
% whatis wc
gives a one%line description of the command, !t omits any information ao!t options etc"
5propos
When yo! are not s!re of the e-act name of a command,
% apropos keyword
will give yo! the commands with $eyword in their man!al page header" 3or e-ample, try
typing
% apropos copy
/!mmary
*ommand +eanin
23
2 match any n!mer of characters
3 match one character
man command read the online man!al page for a command
whatis command rief description of a command
apropos keyword match commands with $eyword in their man pages
UNIX Tutorial 6ive
E"1 3ile system sec!rity &access rights(
In yo!r uni'stuff directory, type
% ls -l -l for long listing4.
?o! will see that yo! now get lots of details ao!t the contents of yo!r directory, similar to
the e-ample elow"
<ach file &and directory( has associated access rights, which may e fo!nd y typing ls -l"
8lso, ls -lg gives additional information as to which gro!p owns the file &eng9E in the
following e-ample(7
-rwxrw-r-- 1 ee51ab beng55 /657 #ept/5 115/ file1
24
In the left%hand col!mn is a 10 symol string consisting of the symols d, r, w, -, %, and,
occasionally, s or /" If d is present, it will e at the left hand end of the string, and indicates
a directory7 otherwise % will e the starting symol of the string"
.he 9 remaining symols indicate the permissions, or access rights, and are ta$en as three
gro!ps of C"
.he left gro!p of C gives the file permissions for the !ser that owns the file &or
directory( &eeE1a in the aove e-ample(6
the middle gro!p gives the permissions for the gro!p of people to whom the file &or
directory( elongs &eeeng9E in the aove e-ample(6
the rightmost gro!p gives the permissions for all others"
.he symols r, w, etc", have slightly different meanings depending on whether they refer to
a simple file or to a directory"
5ccess rihts on files.
r &or %(, indicates read permission &or otherwise(, that is, the presence or asence of
permission to read and copy the file
w &or %(, indicates write permission &or otherwise(, that is, the permission &or
otherwise( to change a file
- &or %(, indicates e-ec!tion permission &or otherwise(, that is, the permission to
e-ec!te a file, where appropriate
5ccess rihts on directories.
r allows !sers to list files in the directory6
w means that !sers may delete files from the directory or move files into it6
- means the right to access files in the directory" .his implies that yo! may read files
in the directory provided yo! have read permission on the individ!al files"
/o, in order to read a file, yo! m!st have e-ec!te permission on the directory containing
that file, and hence on any directory containing that directory as a s!directory, and so on,
!p the tree"
/ome e'amples
25
%rw-rw-rw- a file that everyone can read, write and e-ec!te &and delete("
%rw%%%%%%%
a file that only the owner can read and write % no%one else
can read or write and no%one has e-ec!tion rights &e"g" yo!r
mailo- file("
E"A 5hanging access rights
chmod "chanin a file mode#
2nly the owner of a file can !se chmod to change the permissions of a file" .he options of
chmod are as follows
/ymbol +eanin
! !ser
g gro!p
o other
a all
r read
w write &and delete(
- e-ec!te &and access directory(
N add permission
% ta$e away permission
3or e-ample, to remove read write and e-ec!te permissions on the file bilist for the gro!p
and others, type
% chmod go-rwx biglist
.his will leave the other permissions !naffected"
.o give read and write permissions on the file bilist to all,
26
% chmod a8rw biglist
&'ercise 5a
.ry changing access permissions on the file science.t't and on the directory backups
Use ls -l to chec$ that the permissions have changed"
E"C 4rocesses and Oos
8 process is an e-ec!ting program identified y a !ni+!e 4I= &process identifier(" .o see
information ao!t yo!r processes, with their associated 4I= and stat!s, type
% ps
8 process may e in the foregro!nd, in the ac$gro!nd, or e s!spended" In general the
shell does not ret!rn the UNIX prompt !ntil the c!rrent process has finished e-ec!ting"
/ome processes ta$e a long time to r!n and hold !p the terminal" #ac$gro!nding a long
process has the effect that the UNIX prompt is ret!rned immediately, and other tas$s can e
carried o!t while the original process contin!es e-ec!ting"
1unnin backround processes
.o ac$gro!nd a process, type an 9 at the end of the command line" 3or e-ample, the
command sleep waits a given n!mer of seconds efore contin!ing" .ype
% sleep 17
.his will wait 10 seconds efore ret!rning the command prompt 9" Until the command
prompt is ret!rned, yo! can do nothing e-cept wait"
.o r!n sleep in the ac$gro!nd, type
% sleep 17 9
)1+ :/55
27
.he 9 r!ns the Bo in the ac$gro!nd and ret!rns the prompt straight away, allowing yo! do
r!n other programs while waiting for that one to finish"
.he first line in the aove e-ample is typed in y the !ser6 the ne-t line, indicating Bo
n!mer and 4I=, is ret!rned y the machine" .he !ser is e notified of a Bo n!mer
&n!mered from 1( enclosed in s+!are rac$ets, together with a 4I= and is notified when a
ac$gro!nd process is finished" #ac$gro!nding is !sef!l for Bos which will ta$e a long time
to complete"
<ackroundin a current foreround process
8t the prompt, type
% sleep 1777
?o! can s!spend the process r!nning in the foregro!nd y typing 2=, i"e"hold down the
:*trl; $ey and type :>;" .hen to p!t it in the ac$gro!nd, type
% bg
Note7 do not ac$gro!nd programs that re+!ire !ser interaction e"g" vi
E"D 1isting s!spended and ac$gro!nd processes
When a process is r!nning, ac$gro!nded or s!spended, it will e entered onto a list along
with a Bo n!mer" .o e-amine this list, type
% ;obs
8n e-ample of a Bo list co!ld e
)1+ #uspended sleep 1777
)/+ <unning netscape
)=+ <unning matlab
.o restart &foregro!nd( a s!spended processes, type
% fg %jobnumber
28
3or e-ample, to restart sleep 1000, type
% fg %1
.yping fg with no Bo n!mer foregro!nds the last s!spended process"
E"E Pilling a process
kill "terminate or sinal a process#
It is sometimes necessary to $ill a process &for e-ample, when an e-ec!ting program is in
an infinite loop(
.o $ill a Bo r!nning in the foregro!nd, type 2* &control c(" 3or e-ample, r!n
% sleep 177
&*
.o $ill a s!spended or ac$gro!nd process, type
% kill %jobnumber
3or e-ample, r!n
% sleep 177 9
% ;obs
If it is Bo n!mer D, type
% kill %6
.o chec$ whether this has wor$ed, e-amine the Bo list again to see if the process has een
removed"
ps "process status#
8lternatively, processes can e $illed y finding their process n!mers &4I=s( and !sing
$ill PID_number
29
% sleep 1777 9
% ps
>?' @@ # @?AB *CAAD!'
/77EE pts/5 # 775 sleep 1777
/15:= pts/5 @ 777 netscape
/1FE= pts/5 # 7/5 nedit
.o $ill off the process sleep 1???, type
% kill /77EE
and then type ps again to see if it has een removed from the list"
If a process ref!ses to e $illed, !ses the %$ option, i"e" type
% kill -5 /77EE
Note7 It is not possile to $ill off other !sers, processes @@@
/!mmary
*ommand +eanin
ls -lag list access rights for all files
chmod )options+ file change access rights for named file
command 9 r!n command in ac$gro!nd
&* $ill the Bo r!nning in the foregro!nd
&G s!spend the Bo r!nning in the foregro!nd
bg ac$gro!nd the s!spended Bo
;obs list c!rrent Bos
fg %1 foregro!nd Bo n!mer 1
30
kill %1 $ill Bo n!mer 1
ps list c!rrent processes
kill /:15/ $ill process n!mer A61EA
UNIX Tutorial /i'
2ther !sef!l UNIX commands
.uota
8ll st!dents are allocated a certain amo!nt of dis$ space on the file system for their
personal files, !s!ally ao!t 100)" If yo! go over yo!r +!ota, yo! are given Q days to
remove e-cess files"
.o chec$ yo!r c!rrent +!ota and how m!ch of it yo! have !sed, type
% Huota -v
df
.he df command reports on the space left on the file system" 3or e-ample, to find o!t how
m!ch space is left on the fileserver, type
% df .
du
.he du command o!tp!ts the n!mer of $iloyes !sed y each s!directory" Usef!l if yo!
have gone over +!ota and yo! want to find o!t which directory has the most files" In yo!r
home%directory, type
% du -s 2
.he %s flag will display only a s!mmary &total siGe( and the R means all files and directories"
>ip
31
.his red!ces the siGe of a file, th!s freeing val!ale dis$ space" 3or e-ample, type
% ls -l science.txt
and note the siGe of the file !sing ls -l " .hen to compress science"t-t, type
% gIip science.txt
.his will compress the file and place it in a file called science.t't.>
.o see the change in siGe, type ls -l again"
.o e-pand the file, !se the gunIip command"
% gunIip science.txt.gI
>cat
Icat will read gGipped files witho!t needing to !ncompress them first"
% Icat science.txt.gI
If the te-t scrolls too fast for yo!, pipe the o!tp!t tho!gh less "
% Icat science.txt.gI 1 less
file
file classifies the named files according to the type of data they contain, for e-ample ascii
&te-t(, pict!res, compressed data, etc"" .o report on all files in yo!r home directory, type
% file 2
diff
.his command compares the contents of two files and displays the differences" /!ppose yo!
have a file calledfile1 and yo! edit some part of it and save it as file," .o see the
differences type
% diff file1 file/
32
1ines eginning with a @ denotes file1, while lines eginning with a A denotes fileA"
find
.his searches thro!gh the directories for files and directories with a given name, date, siGe,
or any other attri!te yo! care to specify" It is a simple command !t with many options %
yo! can read the man!al y typing man find"
.o search for all fies with the e-tention .t't, starting at the c!rrent directory &"( and wor$ing
thro!gh all s!%directories, then printing the name of the file to the screen, type
% find . -name J2.txtJ -print
.o find files over 1) in siGe, and display the res!lt as a long listing, type
% find . -siIe 81A -ls
history
.he 5 shell $eeps an ordered list of all the commands that yo! have entered" <ach
command is given a n!mer according to the order it was entered"
% history -show command history list.
If yo! are !sing the 5 shell, yo! can !se the e-clamation character &B( to recall commands
easily"
% 44 -recall last command.
% 4-= -recall third most recent command.
% 45 -recall 5th command in list.
% 4grep -recall last command starting with grep.
?o! can increase the siGe of the history !ffer y typing
% set historyK177
33
UNIX Tutorial /even
Q"1 5ompiling UNIX software pac$ages
We have many p!lic domain and commercial software pac$ages installed on o!r systems,
which are availale to all !sers" *owever, st!dents are allowed to download and install small
software pac$ages in their own home directory, software !s!ally only !sef!l to them
personally"
.here are a n!mer of steps needed to install the software"
1ocate and download the so!rce code &which is !s!ally compressed(
Unpac$ the so!rce code
5ompile the code
Install the res!lting e-ec!tale
/et paths to the installation directory
2f the aove steps, proaly the most diffic!lt is the compilation stage"
*ompilin /ource *ode
8ll high%level lang!age code m!st e converted into a form the comp!ter !nderstands" 3or
e-ample, 5 lang!age so!rce code is converted into a lower%level lang!age called assemly
lang!age" .he assemly lang!age code made y the previo!s stage is then converted into
oBect code which are fragments of code which the comp!ter !nderstands directly" .he final
stage in compiling a program involves lin$ing the oBect code to code liraries which contain
certain !ilt%in f!nctions" .his final stage prod!ces an e-ec!tale program"
.o do all these steps y hand is complicated and eyond the capaility of the ordinary !ser"
8 n!mer of !tilities and tools have een developed for programmers and end%!sers to
simplify these steps"
make and the +akefile
.he make command allows programmers to manage large programs or gro!ps of programs"
It aids in developing large programs y $eeping trac$ of which portions of the entire
34
program have een changed, compiling only those parts of the program which have changed
since the last compile"
.he make program gets its set of compile r!les from a te-t file called +akefile which resides
in the same directory as the so!rce files" It contains information on how to compile the
software, e"g" the optimisation level, whether to incl!de de!gging info in the e-ec!tale" It
also contains information on where to install the finished compiled inaries &e-ec!tales(,
man!al pages, data files, dependent lirary files, config!ration files, etc"
/ome pac$ages re+!ire yo! to edit the )a$efile y hand to set the final installation directory
and any other parameters" *owever, many pac$ages are now eing distri!ted with the 'NU
config!re !tility"
confiure
8s the n!mer of UNIX variants increased, it ecame harder to write programs which co!ld
r!n on all variants" =evelopers fre+!ently did not have access to every system, and the
characteristics of some systems changed from version to version" .he 'NU config!re and
!ild system simplifies the !ilding of programs distri!ted as so!rce code" 8ll programs are
!ilt !sing a simple, standardised, two step process" .he program !ilder need not install
any special tools in order to !ild the program"
.he configure shell script attempts to g!ess correct val!es for vario!s system%dependent
variales !sed d!ring compilation" It !ses those val!es to create a +akefile in each
directory of the pac$age"
.he simplest way to compile a pac$age is7
1" cd to the directory containing the pac$age,s so!rce code"
A" .ype ./configure to config!re the pac$age for yo!r system"
C" .ype make to compile the pac$age"
D" 2ptionally, type make check to r!n any self%tests that come with the pac$age"
E" .ype make install to install the programs and any data files and doc!mentation"
6" 2ptionally, type make clean to remove the program inaries and oBect files from
the so!rce code directory
35
.he config!re !tility s!pports a wide variety of options" ?o! can !s!ally !se the %%
help option to get a list of interesting options for a partic!lar config!re script"
.he only generic options yo! are li$ely to !se are the %%prefi' and %%e'ec%prefi' options"
.hese options are !sed to specify the installation directories"
.he directory named y the %%prefi' option will hold machine independent files s!ch as
doc!mentation, data and config!ration files"
.he directory named y the %%e'ec%prefi' option, &which is normally a s!directory of
the %%prefi' directory(, will hold machine dependent files s!ch as e-ec!tales"
Q"A =ownloading so!rce code
3or this e-ample, we will download a piece of free software that converts etween different
!nits of meas!rements"
3irst create a download directory
% mkdir download
=ownload the software here and save it to yo!r new download directory"
Q"C <-tracting the so!rce code
'o into yo!r do(nload directory and list the contents"
% cd download
% ls -l
8s yo! can see, the filename ends in tar"gG" .he tar command t!rns several files and
directories into one single tar file" .his is then compressed !sing the gGip command &to
create a tar"gG file("
3irst !nGip the file !sing the g!nGip command" .his will create a "tar file"
% gunIip units-1.E6.tar.gI
.hen e-tract the contents of the tar file"
36
% tar -xvf units-1.E6.tar
8gain, list the contents of the do(nload directory, then go to the units%1.CD s!%
directory"
% cd units-1.E6
Q"D 5onfig!ring and creating the )a$efile
.he first thing to do is caref!lly read the 1&53+& and IN/T5EE te-t files &!se the less
command(" .hese contain important information on how to compile and r!n the software"
.he !nits pac$age !ses the 'NU config!re system to compile the so!rce code" We will need
to specify the installation directory, since the defa!lt will e the main system area which yo!
will not have write permissions for" We need to create an install directory in yo!r home
directory"
% mkdir "/units1E6
.hen r!n the config!re !tility setting the installation path to this"
% ./configure --prefixKLMCAB/units1E6
N2.<7 .he FG!+& variale is an e-ample of an environment variale" .he val!e
of FG!+& is the path to yo!r home directory" O!st type
% echo LMCAB
to show the contents of this variale" We will learn more ao!t environment variales in a
later chapter"
If configure has r!n correctly, it will have created a )a$efile with all necessary options" ?o!
can view the )a$efile if yo! wish &!se the less command(, !t do not edit the contents of
this"
Q"E #!ilding the pac$age
Now yo! can go ahead and !ild the pac$age y r!nning the ma$e command"
37
% make
8fter a min!te or two &depending on the speed of the comp!ter(, the e-ec!tales will e
created" ?o! can chec$ to see everything compiled s!ccessf!lly y typing
% make check
If everything is o$ay, yo! can now install the pac$age"
% make install
.his will install the files into the )/units1CD directory yo! created earlier"
Q"6 F!nning the software
?o! are now ready to r!n the software &ass!ming everything wor$ed("
% cd "/units1E6
If yo! list the contents of the !nits directory, yo! will see a n!mer of s!directories"
in .he inary e-ec!tales
info 'NU info formatted doc!mentation
man )an pages
share /hared data files
.o r!n the program, change to the bin directory and type
% ./units
8s an e-ample, convert 6 feet to metres"
Nou have : feet
Nou want metres
2 1.F/FF
38
If yo! get the answer 1"SASS, congrat!lations, it wor$ed"
.o view what !nits it can convert etween, view the data file in the share directory &the list
is +!ite comprehensive("
.o read the f!ll doc!mentation, change into the info directory and type
% info --fileKunits.info
Q"Q /tripping !nnecessary code
When a piece of software is eing developed, it is !sef!l for the programmer to incl!de
de!gging information into the res!lting e-ec!tale" .his way, if there are prolems
enco!ntered when r!nning the e-ec!tale, the programmer can load the e-ec!tale into a
de!gging software pac$age and trac$ down any software !gs"
.his is !sef!l for the programmer, !t !nnecessary for the !ser" We can ass!me that the
pac$age, once finished and availale for download has already een tested and de!gged"
*owever, when we compiled the software aove, de!gging information was still compiled
into the final e-ec!tale" /ince it is !nli$ey that we are going to need this de!gging
information, we can strip it o!t of the final e-ec!tale" 2ne of the advantages of this is a
m!ch smaller e-ec!tale, which sho!ld r!n slightly faster"
What we are going to do is loo$ at the efore and after siGe of the inary file" 3irst change
into the bindirectory of the !nits installation directory"
% cd "/units1E6/bin
% ls -l
8s yo! can see, the file is over 100 $ytes in siGe" ?o! can get more information on the type
of file y !sing the file command"
% file units
units BOP =/-bit O#Q executable, ?ntel F7=F:, version 1,
dynamically linked -uses shared libs., not stripped
39
.o strip all the de!g and line n!mering information o!t of the inary file, !se the strip
command
% strip units
% ls -l
8s yo! can see, the file is now C6 $ytes % a third of its original siGe" .wo thirds of the inary
file was de!g code@@@
5hec$ the file information again"
% file units
units BOP =/-bit O#Q executable, ?ntel F7=F:, version 1,
dynamically linked -uses shared libs., stripped
/ometimes yo! can !se the make command to install pre%stripped copies of all the inary
files when yo! install the pac$age" Instead of typing make install, simply type make
install-strip
UNIX Tutorial &iht
S"1 UNIX Tariales
Tariales are a way of passing information from the shell to programs when yo! r!n them"
4rograms loo$ >in the environment> for partic!lar variales and if they are fo!nd will !se the
val!es stored" /ome are set y the system, others y yo!, yet others y the shell, or any
program that loads another program"
/tandard UNIX variales are split into two categories, environment variales and shell
variales" In road terms, shell variales apply only to the c!rrent instance of the shell and
are !sed to set short%term wor$ing conditions6 environment variales have a farther
reaching significance, and those set at login are valid for the d!ration of the session" #y
convention, environment variales have U44<F 58/< and shell variales have lower case
names"
S"A <nvironment Tariales
40
8n e-ample of an environment variale is the 2/.?4< variale" .he val!e of this is the
c!rrent operating system yo! are !sing" .ype
% echo LC#@N>B
)ore e-amples of environment variales are
U/<F &yo!r login name(
*2)< &the path name of yo!r home directory(
*2/. &the name of the comp!ter yo! are !sing(
8F5* &the architect!re of the comp!ters processor(
=I/418? &the name of the comp!ter screen to display X windows(
4FIN.<F &the defa!lt printer to send print Bos(
48.* &the directories the shell sho!ld search to find a command(
6indin out the current values of these variables.
<NTIF2N)<N. variales are set !sing the setenv command, displayed !sing
the printenv or env commands, and !nset !sing the unsetenv command"
.o show all val!es of these variales, type
% printenv 1 less
S"C /hell Tariales
8n e-ample of a shell variale is the history variale" .he val!e of this is how many shell
commands to save, allow the !ser to scroll ac$ thro!gh all the commands they have
previo!sly entered" .ype
% echo Lhistory
)ore e-amples of shell variales are
cwd &yo!r c!rrent wor$ing directory(
home &the path name of yo!r home directory(
path &the directories the shell sho!ld search to find a command(
41
prompt &the te-t string !sed to prompt for interactive commands shell yo!r login
shell(
6indin out the current values of these variables.
/*<11 variales are oth set and displayed !sing the set command" .hey can e !nset y
!sing the !nset command"
.o show all val!es of these variales, type
% set 1 less
/o (hat is the difference bet(een H5TG and path 8
In general, environment and shell variales that have the same name &apart from the case(
are distinct and independent, e-cept for possily having the same initial val!es" .here are,
however, e-ceptions"
<ach time the shell variales home, !ser and term are changed, the corresponding
environment variales *2)<, U/<F and .<F) receive the same val!es" *owever, altering
the environment variales has no effect on the corresponding shell variales"
48.* and path specify directories to search for commands and programs" #oth variales
always represent the same directory list, and altering either a!tomatically ca!ses the other
to e changed"
S"D Using and setting variales
<ach time yo! login to a UNIX host, the system loo$s in yo!r home directory for initialisation
files" Information in these files is !sed to set !p yo!r wor$ing environment" .he 5 and .5
shells !ses two files called "login and "cshrc &note that oth file names egin with a dot("
8t login the 5 shell first reads .cshrc followed y .loin
.loin is to set conditions which will apply to the whole session and to perform actions that
are relevant only at login"
.cshrc is !sed to set conditions and perform actions specific to the shell and to each
invocation of it"
42
.he g!idelines are to set <NTIF2N)<N. variales in the .loin file and /*<11 variales in
the .cshrc file"
I51NIN;J N<T<F p!t commands that r!n graphical displays &e"g" a we rowser( in
yo!r "cshrc or "login file"
S"E /etting shell variales in the "cshrc file
3or e-ample, to change the n!mer of shell commands saved in the history list, yo! need to
set the shell variale history" It is set to 100 y defa!lt, !t yo! can increase this if yo!
wish"
% set history K /77
5hec$ this has wor$ed y typing
% echo Lhistory
*owever, this has only set the variale for the lifetime of the c!rrent shell" If yo! open a
new -term window, it will only have the defa!lt history val!e set" .o 4<F)8N<N.1? set the
val!e of history, yo! will need to add the set command to the "cshrc file"
3irst open the .cshrc file in a te-t editor" 8n easy, !ser%friendly editor to !se is nedit"
% nedit "/.cshrc
8dd the following line 83.<F the list of other commands"
set history U A00
/ave the file and force the shell to reread its "cshrc file !y !sing the shell so!rce command"
% source .cshrc
5hec$ this has wor$ed y typing
% echo Lhistory
S"6 /etting the path
43
When yo! type a command, yo!r path &or 48.*( variale defines in which directories the
shell will loo$ to find the command yo! typed" If the system ret!rns a message saying
>command7 5ommand not fo!nd>, this indicates that either the command doesn,t e-ist at all
on the system or it is simply not in yo!r path"
3or e-ample, to r!n !nits, yo! either need to directly specify the !nits path
&)/units1CD/bin/units(, or yo! need to have the directory )/units1CD/bin in yo!r
path"
?o! can add it to the end of yo!r e-isting path &the Fpath represents this( y iss!ing the
command7
% set path K -Lpath "/units1E6/bin.
.est that this wor$ed y trying to r!n !nits in any directory other that where !nits is act!ally
located"
% cd
% units
.o add this path 4<F)8N<N.1?, add the following line to yo!r "cshrc 83.<F the list of other
commands"
set path K -Lpath "/units1E6/bin.
44

You might also like