You are on page 1of 45

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 sta le, 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 availa le, 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 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 instr!ctions comprehensi le directly to the machine and incomprehensi le to a cas!al !ser, for e-ample, a collection of inary digits &an e-ec!ta le or inary file(6 a directory, containing information a o!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 a ove, 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/u 1/ee51vn/report.doc"

/tarting an UNIX terminal

.o open an UNIX terminal window, clic$ on the >.erminal> icon from the drop%down men!s"

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

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 lists the contents of yo!r c!rrent wor$ing directory"

.here may e no files visi le 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@@@ .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


cd "chan e 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 "..#


&..( 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 ena le yo! to wor$ o!t where yo! are in relation to the whole file% system" 3or e-ample, to find o!t the a sol!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 u 1 &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 > / " "

&'ercise 1b
Use the commands cd, ls and pwd to e-plore the file system" &Femem er, if yo! get lost, type cd y itself to ret!rn to yo!r home%directory(

1"6 )ore a o!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: No such file or directory

.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 ls ls -a +eanin list files and directories list all files and directories

mkdir

ma$e a directory

cd directory change to named directory cd cd ~ cd .. pwd change to home%directory change to home%directory change to parent directory 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 it file, 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 cp command 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" Femem er, in UNIX, the dot means the c!rrent directory"

.he a ove command means copy the file science.t't to the c!rrent directory, $eeping the name the same"

&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! remem er how?(" .hen, inside the uni'stuff directory, 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 the science.t't file then delete it" Inside yo!r uni'stuff directory, type

% 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 the backups directory" ?o! will not e a le 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 !nreada le"

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 -

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


/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!gh science.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#

!rep is one of many standard UNIX !tilities" It searches files for specified words or patterns" 3irst clear the screen, then type

% !rep science science.txt


8s yo! can see, !rep has printed o!t each line containg the word science" 2r has it ???? .ry typing

% !rep "cience science.txt


.he !rep command is case sensitive6 it disting!ishes etween /cience and science"

.o ignore !pper0lower case distinctions, !se the %i option, i"e" type

% !rep -i science science.txt


.o search for a phrase or pattern, yo! m!st enclose it in single +!otes &the apostrophe sym ol(" 3or e-ample to search for spinning top, type

% !rep -i #spinnin! 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!m er %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!m er of lines witho!t the words science or /cience is

% !rep -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 cp file1 file2 +eanin copy file1 and call it fileA

mv file1 file2 rm file rmdir directory cat file less file head file tail file

move or rename file1 to fileA remove a file remove a directory display a file display a file a page at a time display the first few lines of a file display the last few lines of a file

!rep 'keyword' file search a file for $eywords wc file co!nt n!m er 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 $ey oard(" .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 $ey oard 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 $ey oard(, 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"

C"A Fedirecting the 2!tp!t


We !se the I sym ol 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 $ list%
.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 $ey oard( 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 list%

&'ercise 4a
Using the a ove 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 $$ list%
.hen type in the names of more fr!it

peach !rape oran!e &' -*ontrol ' to stop.


.o read the contents of the file, type

% cat list%
?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 bi list" .ype

% cat list% list/ $ bi!list


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 bi list .o read the contents of the new file, type

% cat bi!list

C"C Fedirecting the Inp!t


We !se the J sym ol to redirect the inp!t of a command" .he command sort alpha etically or n!merically sorts a list" .ype

% sort

.hen type in the names of some animals" 4ress :Fet!rn; after each one"

do! cat bird ape &' -control d to stop.


.he o!tp!t will e

ape bird cat do!


Using J yo! can redirect the inp!t to come from a file rather than the $ey oard" 3or e-ample, to sort the list of fr!it, type

% sort 0 bi!list
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 bi!list $ 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 remem er 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 sym ol for a pipe is the vertical ar K 3or e-ample, typing

% who 1 sort
will give the same res!lt as a ove, !t +!ic$er and cleaner" .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 availa le here

/!mmary
*ommand command $ file command $$ file command 0 file command1 1 command2 +eanin redirect standard o!tp!t to a file append standard o!tp!t to a file redirect standard inp!t from a file pipe the o!tp!t of command1 to the inp!t of commandA

cat file1 file2 $ file0 concatenate file1 and fileA to file0 sort who sort data 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

% 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 .ry typing rouse"

% 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!m ers, together with L &!nderscore( and " &dot("

;ood filenames proBect"t-t

<ad filenames proBect

myL igLprogram"c my ig program"c fredLdave"doc fred M dave"doc

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, pro 1.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 a o!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" .ype man command to read the man!al page for a partic!lar command" 3or e-ample, to find o!t more a o!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 a o!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 2 3 man command whatis command +eanin match any n!m er of characters match one character read the online man!al page for a 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 lon! listin!4.


?o! will see that yo! now get lots of details a o!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 -l! gives additional information as to which gro!p owns the file & eng9E in the following e-ample(7

-rwxrw-r-- % ee %ab ben!5

/6 7 "ept/5 %%: / file%

In the left%hand col!mn is a 10 sym ol string consisting of the sym ols 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 sym ol of the string" .he 9 remaining sym ols 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 a ove e-ample(6 the middle gro!p gives the permissions for the gro!p of people to whom the file &or directory( elongs &ee eng9E in the a ove e-ample(6 the rightmost gro!p gives the permissions for all others"

.he sym ols r, w, etc", have slightly different meanings depending on whether they refer to a simple file or to a directory"

5ccess ri hts on files.



r &or %(, indicates read permission &or otherwise(, that is, the presence or a sence 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 ri hts 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
%rw-rw-rw- a file that everyone can read, write and e-ec!te &and delete(" a file that only the owner can read and write % no%one else %rw%%%%%%% can read or write and no%one has e-ec!tion rights &e"g" yo!r mail o- file("

E"A 5hanging access rights


chmod "chan in 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 ! g o a r !ser gro!p other all read

+eanin

w N %

write &and delete( e-ec!te &and access directory( add permission ta$e away permission

3or e-ample, to remove read write and e-ec!te permissions on the file bi list for the gro!p and others, type

% chmod !o-rwx bi!list


.his will leave the other permissions !naffected" .o give read and write permissions on the file bi list to all,

% chmod a8rw bi!list

&'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 Oo s


8 process is an e-ec!ting program identified y a !ni+!e 4I= &process identifier(" .o see information a o!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

back round 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!m er of seconds efore contin!ing" .ype

% sleep %7
.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 %7 9

)%+ :/ 5
.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 a ove e-ample is typed in y the !ser6 the ne-t line, indicating Bo n!m er and 4I=, is ret!rned y the machine" .he !ser is e notified of a Bo n!m er &n!m ered 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 Bo s which will ta$e a long time to complete"

<ack roundin
8t the prompt, type

a current fore round process

% sleep %777
?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

% b!

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!m er" .o e-amine this list, type

% ;obs
8n e-ample of a Bo list co!ld e

)%+ "uspended sleep %777 )/+ <unnin! netscape )=+ <unnin! matlab
.o restart &foregro!nd( a s!spended processes, type

% f! %jobnumber
3or e-ample, to restart sleep 1000, type

% f! %%
.yping f! with no Bo n!m er foregro!nds the last s!spended process"

E"E Pilling a process


kill "terminate or si nal 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 %77 &*


.o $ill a s!spended or ac$gro!nd process, type

% kill %jobnumber
3or e-ample, r!n

% sleep %77 9 % ;obs


If it is Bo n!m er 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!m ers &4I=s( and !sing $ill PID_number

% sleep %777 9 % ps

>?' @@ " @?AB /77EE pts/ " /% := pts/ @ /%FE= pts/ "

*CAADN' 7:7 sleep %777 7:77 netscape 7:/ 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 possi le to $ill off other !sers, processes @@@

/!mmary
*ommand ls -la! +eanin list access rights for all files

chmod )options+ file change access rights for named file command 9 &* &G b! ;obs f! %% kill %% ps kill /:% / r!n command in ac$gro!nd $ill the Bo r!nning in the foregro!nd s!spend the Bo r!nning in the foregro!nd ac$gro!nd the s!spended Bo list c!rrent Bo s foregro!nd Bo n!m er 1 $ill Bo n!m er 1 list c!rrent processes $ill process n!m er 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 a o!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!m er of $ilo yes !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
.his red!ces the siGe of a file, th!s freeing val!a le 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

% !Iip 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 !unIip command"

% !unIip science.txt.!I

>cat

Icat will read gGipped files witho!t needing to !ncompress them first"

% Icat science.txt.!I
If the te-t scrolls too fast for yo!, pipe the o!tp!t tho!gh less "

% Icat science.txt.!I 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 called file1 and yo! edit some part of it and save it as file," .o see the differences type

% diff file% file/


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 8%A -ls

history
.he 5 shell $eeps an ordered list of all the commands that yo! have entered" <ach command is given a n!m er 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. % 4 -recall th command in list.

% 4!rep -recall last command startin! with !rep.


?o! can increase the siGe of the history !ffer y typing

% set historyK%77

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 availa le 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!m er 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!ta le /et paths to the installation directory

2f the a ove steps, pro a ly 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 assem ly lang!age" .he assem ly lang!age code made y the previo!s stage is then converted into o Bect code which are fragments of code which the comp!ter !nderstands directly" .he final stage in compiling a program involves lin$ing the o Bect code to code li raries which contain certain !ilt%in f!nctions" .his final stage prod!ces an e-ec!ta le program" .o do all these steps y hand is complicated and eyond the capa ility of the ordinary !ser" 8 n!m er 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 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!ta le" It also contains information on where to install the finished compiled inaries &e-ec!ta les(, man!al pages, data files, dependent li rary 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"

confi ure

8s the n!m er 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 confi!ure shell script attempts to g!ess correct val!es for vario!s system% dependent varia les !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. 2. 3. 4. 5.
6.

cd to the directory containing the pac$age,s so!rce code" .ype ./confi!ure to config!re the pac$age for yo!r system" .ype make to compile the pac$age" 2ptionally, type make check to r!n any self%tests that come with the pac$age" .ype make install to install the programs and any data files and doc!mentation" 2ptionally, type make clean to remove the program inaries and o Bect files from the so!rce code directory

.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!ta les"

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"

% !unIip units-%.E6.tar.!I
.hen e-tract the contents of the tar file"

% tar -xvf units-%.E6.tar


8gain, list the contents of the do(nload directory, then go to the units%1.CD s! % directory"

% cd units-%.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 ~/units%E6
.hen r!n the config!re !tility setting the installation path to this"

% ./confi!ure --prefixKLMCAB/units%E6
N2.<7 .he FG!+& varia le is an e-ample of an environment varia le" .he val!e of FG!+& is the path to yo!r home directory" O!st type

% echo LMCAB

to show the contents of this varia le" We will learn more a o!t environment varia les in a later chapter" If confi!ure 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"

% make
8fter a min!te or two &depending on the speed of the comp!ter(, the e-ec!ta les 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 ~/units%E6
If yo! list the contents of the !nits directory, yo! will see a n!m er of s! directories"

in info man

.he inary e-ec!ta les 'NU info formatted doc!mentation )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 %.F/FF
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!ta le" .his way, if there are pro lems enco!ntered when r!nning the e-ec!ta le, the programmer can load the e-ec!ta le 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 availa le for download has already een tested and de !gged" *owever, when we compiled the software a ove, de !gging information was still compiled into the final e-ec!ta le" /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!ta le" 2ne of the advantages of this is a m!ch smaller e-ec!ta le, 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 bin directory of the !nits installation directory"

% cd ~/units%E6/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 %, dynamically linked -uses shared libs., not stripped
.o strip all the de !g and line n!m ering 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 %, 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 &i ht


S"1 UNIX Taria les
Taria les are a way of passing information from the shell to programs when yo! r!n them" 4rograms loo$ >in the environment> for partic!lar varia les 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 varia les are split into two categories, environment varia les and shell varia les" In road terms, shell varia les apply only to the c!rrent instance of the shell and are !sed to set short%term wor$ing conditions6 environment varia les have a farther reaching significance, and those set at login are valid for the d!ration of the session" #y convention, environment varia les have U44<F 58/< and shell varia les have lower case names"

S"A <nvironment Taria les


8n e-ample of an environment varia le is the 2/.?4< varia le" .he val!e of this is the c!rrent operating system yo! are !sing" .ype

% echo LC"@N>B
)ore e-amples of environment varia les 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 Bo s( 48.* &the directories the shell sho!ld search to find a command(

6indin

out the current values of these variables.

<NTIF2N)<N. varia les 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 varia les, type

% printenv 1 less

S"C /hell Taria les


8n e-ample of a shell varia le is the history varia le" .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 varia les 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( 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 varia les 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 varia les, type

% set 1 less

/o (hat is the difference bet(een H5TG and path 8


In general, environment and shell varia les that have the same name &apart from the case( are distinct and independent, e-cept for possi ly having the same initial val!es" .here are, however, e-ceptions"

<ach time the shell varia les home, !ser and term are changed, the corresponding environment varia les *2)<, U/<F and .<F) receive the same val!es" *owever, altering the environment varia les has no effect on the corresponding shell varia les" 48.* and path specify directories to search for commands and programs" #oth varia les always represent the same directory list, and altering either a!tomatically ca!ses the other to e changed"

S"D Using and setting varia les


<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 .lo in .lo in 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" .he g!idelines are to set <NTIF2N)<N. varia les in the .lo in file and /*<11 varia les in the .cshrc file"

I51NIN;J N<T<F p!t commands that r!n graphical displays &e"g" a we in yo!r "cshrc or "login file"

rowser(

S"E /etting shell varia les in the "cshrc file


3or e-ample, to change the n!m er of shell commands saved in the history list, yo! need to set the shell varia le 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 varia le 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


When yo! type a command, yo!r path &or 48.*( varia le 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 ~/units%E6/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 ~/units%E6/bin.

You might also like