Basic Unix Tutorial

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 27

UNIX Introduction

What is UNIX?
UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since !y operating system, we mean the suite of programs which ma"e the computer wor" It is a stable, multi#user, multi#tas"ing system for servers, des"tops and laptops UNIX systems also have a graphical user interface $%UI& similar to 'icrosoft (indows which provides an easy to use environment )owever, "nowledge of UNIX is re*uired for operations which aren+t covered by a graphical program, or for when there is no windows interface available, for e,ample, in a telnet session

Types of UNIX
-here are many different versions of UNIX, although they share common similarities -he most popular varieties of UNIX are .un .olaris, %NU/0inu,, and 'ac1. X )ere in the .chool, we use .olaris on our servers and wor"stations, and 2edora 3ore 0inu, on the servers and des"top 43s

The UNIX operating system


-he UNIX operating system is made up of three parts5 the "ernel, the shell and the programs

The kernel
-he "ernel of UNIX is the hub of the operating system6 it allocates time and memory to programs and handles the filestore and communications in response to system calls 7s an illustration of the way that the shell and the "ernel wor" together, suppose a user 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*uests the "ernel, through system calls, to e,ecute the program rm on myfile (hen the process rm myfile has finished running, the shell then returns the UNIX prompt 8 to the user, indicating that it is waiting for further commands

The shell
-he shell acts as an interface between the user and the "ernel (hen a user logs in, the login program chec"s the username and password, and then starts another program called the shell -he shell is a command line interpreter $30I& It interprets the commands the user types in and arranges for them to be carried out -he commands are themselves programs6 when they terminate, the shell gives the user another prompt $8 on our systems& -he adept user can customise his/her own shell, and users can use different shells on the same machine .taff and students in the school have the tcsh shell by default -he tcsh shell has certain features to help the user inputting commands

2ilename 3ompletion # !y typing part of the name of a command, filename or directory and pressing the 9Tab: "ey, the tcsh shell will complete the rest of the name automatically If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab "ey again )istory # -he shell "eeps a list of the commands you have typed in If you need to repeat a command, use the cursor "eys to scroll up and down the list or type history for a list of previous commands

Files and processes


;verything in UNIX is either a file or a process 7 process is an e,ecuting program identified by a uni*ue 4I< $process identifier& 7 file is a collection of data -hey are created by users using te,t editors, running compilers etc ;,amples of files6 a document $report, essay etc & the te,t of a program written in some high#level programming language instructions comprehensible directly to the machine and incomprehensible to a casual user, for e,ample, a collection of binary digits $an e,ecutable or binary file&5 a directory, containing information about its contents, which may be a mi,ture of other directories $subdirectories& and ordinary files

The Directory Structure


7ll the files are grouped together in the directory structure -he file#system is arranged in a hierarchical structure, li"e an inverted tree -he top of the hierarchy is traditionally called root $written as a slash / & In the diagram above, we see that the home directory of the undergraduate student ee!"#n contains two sub#directories $docs and pics& and a file called report$doc -he full path to the file report$doc is %home%its%ug"%ee!"#n%report$doc

Starting an UNIX terminal


-o open an UNIX terminal window, clic" on the =-erminal= icon from the drop#down menus

UNIX Tutorial &ne


"$" 'isting files and directories
ls (list)
(hen you first login, your current wor"ing directory is your home directory >our home directory has the same name as your user#name, for e,ample, ee*"ab, and it is where your personal files and subdirectories are saved -o find out what is in your home directory, type 8 ls -he ls command lists the contents of your current wor"ing directory -here may be no files visible in your home directory, in which case, the UNIX prompt will be returned 7lternatively, there may already be some files inserted by the .ystem 7dministrator when your account was created ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot $ & 2iles beginning with a dot $ & are "nown as hidden files and usually contain important program configuration information -hey are hidden because you should not change them unless you are very familiar with UNIX??? -o list all files in your home directory including those whose names begin with a dot, type 8 ls #a 7s you can see, ls #a lists files that are normally hidden ls is an e,ample of a command which can ta"e options6 +a is an e,ample of an option -he options change the behaviour of the command -here are online manual pages that tell you which options a particular command can ta"e, and how each option modifies the behaviour of the command $.ee later in this tutorial&

"$, -aking Directories


mkdir (make directory)
(e will now ma"e a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial -o ma"e a subdirectory called uni,stuff in your current wor"ing directory type 8 m"dir uni,stuff -o see the directory you have @ust created, type 8 ls

"$. /hanging to a different directory


cd (change directory)
-he command cd directory means change the current wor"ing directory to 'directory' -he current wor"ing directory may be thought of as the directory you are in, i e your

current position in the file#system tree -o change to the directory you have @ust made, type 8 cd uni,stuff -ype ls to see the contents $which should be empty&

01ercise "a
'a"e another directory inside the uni1stuff directory called backups

"$2 The directories $ and $$


.till in the uni1stuff directory, type 8 ls #a 7s you can see, in the uni1stuff directory $and in all other directories&, there are two special directories called $$& and $$$&

The current directory ($)


In UNIX, $$& means the current directory, so typing 8 cd N1-;6 there is a space between cd and the dot means stay where you are $the uni1stuff directory& -his may not seem very useful at first, but using $$& as the name of the current directory will save a lot of typing, as we shall see later in the tutorial

The parent directory ($$)


$$$& means the parent of the current directory, so typing 8 cd will ta"e you one directory up the hierarchy $bac" to your home directory& -ry it now Note6 typing cd with no argument always returns you to your home directory -his is very useful if you are lost in the file system

"$! 3athnames
p4d (print 4orking directory)
4athnames enable you to wor" out where you are in relation to the whole file#system 2or e,ample, to find out the absolute pathname of your home#directory, type cd to get bac" to your home#directory and then type 8 pwd -he full pathname will loo" something li"e this # %home%its%ug"%ee!"#n which means that ee!"#n $your home directory& is in the sub#directory ug" $the group directory&,which in turn is located in the its sub#directory, which is in the home sub# directory, which is in the top#level root directory called = / "

01ercise "b
Use the commands cd, ls and pwd to e,plore the file system $Aemember, if you get lost, type cd by itself to return to your home#directory&

"$5 -ore about home directories and pathnames


Understanding pathnames
2irst type cd to get bac" to your home#directory, then type 8 ls uni,stuff to list the conents of your uni,stuff directory Now type 8 ls bac"ups >ou will get a message li"e this # bac"ups6 No such file or directory -he reason is, backups is not in your current wor"ing directory -o use a command on a file $or directory& not in the current wor"ing directory $the directory you are currently in&, you must either cd to the correct directory, or specify its full pathname -o list the contents of your bac"ups directory, you must type 8 ls uni,stuff/bac"ups

6 (your home directory)


)ome directories can also be referred to by the tilde 6 character It can be used to specify paths starting at your home directory .o typing 8 ls B/uni,stuff will list the contents of your uni,stuff directory, no matter where you currently are in the file system (hat do you thin" 8 ls B would listC (hat do you thin" 8 ls B/ would listC

Summary
/ommand ls ls #a m"dir cd directory cd -eaning list files and directories list all files and directories ma"e a directory change to named directory change to home#directory

cd B cd pwd

change to home#directory change to parent directory display the path of the current directory

UNIX Tutorial T4o


,$" /opying Files
cp (copy)
cp file1 file2 is the command which ma"es a copy of file" in the current wor"ing directory and calls it file, (hat we are going to do now, is to ta"e a file stored in an open access area of the file system, and use the cp command to copy it to your uni,stuff directory 2irst, cd to your uni1stuff directory 8 cd B/uni,stuff -hen at the UNIX prompt, type, 8 cp /vol/e,amples/tutorial/science t,t Note6 <on+t forget the dot $ at the end Aemember, in UNIX, the dot means the current directory -he above command means copy the file science$t1t to the current directory, "eeping the name the same $Note6 -he directory %#ol%e1amples%tutorial% is an area to which everyone in the school has read and copy access If you are from outside the University, you can grab a copy of the file here Dscience t,tE Use +2ile/.ave 7s + from the menu bar to save it into your uni1stuff directory &

01ercise ,a
3reate a bac"up of your science$t1t file by copying it to a file called science$bak

,$, -o#ing files


m# (mo#e)
mv file1 file2 moves $or renames& file" to file, -o move a file from one place to another, use the mv command -his has the effect of moving rather than copying the file, so you end up with only one file rather than two It can also be used to rename a file, by moving the file to the same directory, but giving it a different name (e are now going to move the file science ba" to your bac"up directory 2irst, change directories to your uni,stuff directory $can you remember howC& -hen,

inside the uni1stuff directory, type 8 mv science ba" bac"ups/ -ype ls and ls bac"ups to see if it has wor"ed

,$. 7emo#ing files and directories


rm (remo#e)8 rmdir (remo#e directory)
-o delete $remove& a file, use the rm command 7s an e,ample, we are going to create a copy of the science$t1t file then delete it Inside your uni1stuff directory, type 8 cp science t,t tempfile t,t 8 ls 8 rm tempfile t,t 8 ls >ou can use the rmdir command to remove a directory $ma"e sure it is empty first& -ry to remove the backups directory >ou will not be able to since UNIX will not let you remove a non#empty directory

01ercise ,b
3reate a directory called tempstuff using m"dir , then remove it using the rmdir command

,$2 Displaying the contents of a file on the screen


clear (clear screen)
!efore you start the ne,t section, you may li"e to clear the terminal window of the previous commands so the output of the following commands can be clearly understood 7t the prompt, type 8 clear -his will clear all te,t and leave you with the 8 prompt at the top of the window

cat (concatenate)
-he command cat can be used to display the contents of a file on the screen -ype6 8 cat science t,t 7s you can see, the file is longer than than the siFe of the window, so it scrolls past ma"ing it unreadable

less
-he command less writes the contents of a file onto the screen a page at a time -ype 8 less science t,t 4ress the 9space+bar: if you want to see another page, and type 99: if you want to *uit reading 7s you can see, less is used in preference to cat for long files

head
-he head command writes the first ten lines of a file to the screen 2irst clear the screen then type 8 head science t,t -hen type 8 head #G science t,t (hat difference did the #G do to the head commandC

tail
-he tail command writes the last ten lines of a file to the screen 3lear the screen and type 8 tail science t,t H )ow can you view the last 1G lines of the fileC

,$! Searching the contents of a file


Simple searching using less
Using less, you can search though a te,t file for a "eyword $pattern& 2or e,ample, to search through science$t1t for the word :science:, type 8 less science t,t then, still in less, type a forward slash 9%: followed by the word to search /science 7s you can see, less finds and highlights the "eyword -ype 9n: to search for the ne,t occurrence of the word

grep (don:t ask 4hy it is called grep)


grep is one of many standard UNIX utilities It searches files for specified words or patterns 2irst clear the screen, then type 8 grep science science t,t 7s you can see, grep has printed out each line containg the word science 1r has it CCCC -ry typing 8 grep .cience science t,t -he grep command is case sensitive5 it distinguishes between .cience and science -o ignore upper/lower case distinctions, use the #i option, i e type 8 grep #i science science t,t -o search for a phrase or pattern, you must enclose it in single *uotes $the apostrophe symbol& 2or e,ample to search for spinning top, type

8 grep #i +spinning top+ science t,t .ome of the other options of grep are6 +# display those lines that do N1- match +n precede each matching line with the line number +c print only the total count of matched lines -ry some of them and see the different results <on+t forget, you can use more than one option at a time 2or e,ample, the number of lines without the words science or .cience is 8 grep #ivc science science t,t

4c (4ord count)
7 handy little utility is the wc command, short for word count -o do a word count on science$t1t, type 8 wc #w science t,t -o find out how many lines the file has, type 8 wc #l science t,t

Summary
/ommand cp file1 file2 mv file1 file2 rm file rmdir directory cat file less file head file tail file grep 'keyword' file wc file -eaning copy file1 and call it fileI move or rename file1 to fileI 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 search a file for "eywords count number of lines/words/characters in file

UNIX Tutorial Three


.$" 7edirection
'ost processes initiated by UNIX commands write to the standard output $that is, they write to the terminal screen&, and many ta"e their input from the standard input $that is, they read it from the "eyboard& -here is also the standard error, where processes write their error messages, by default, to the terminal screen (e have already seen one use of the cat command to write the contents of a file to the screen Now type cat without specifing a file to read

8 cat -hen type a few words on the "eyboard and press the 97eturn: "ey 2inally hold the 9/trl: "ey down and press 9d: $written as ;D for short& to end the input (hat has happenedC If you run the cat command without specifing a file to read, it reads the standard input $the "eyboard&, and on receiving the +end of file+ $;D&, copies it to the standard output $the screen& In UNIX, we can redirect both the input and the output of commands

.$, 7edirecting the &utput


(e use the E symbol to redirect the output of a command 2or e,ample, to create a file called list" containing a list of fruit, type 8 cat E list1 -hen type in the names of some fruit 4ress 97eturn: after each one pear banana apple J< Kthis means press 93trl: and 9d: to stopL (hat happens is the cat command reads the standard input $the "eyboard& and the E redirects the output, which normally goes to the screen, into a file called list" -o read the contents of the file, type 8 cat list1

01ercise .a
Using the above method, create another file called list, containing the following fruit6 orange, plum, mango, grapefruit Aead the contents of list,

.$,$" <ppending to a file


-he form EE appends standard output to a file .o to add more items to the file list", type 8 cat EE list1 -hen type in the names of more fruit peach grape orange J< $3ontrol < to stop& -o read the contents of the file, type 8 cat list1 >ou should now have two files 1ne contains si, fruit, the other contains four fruit (e will now use the cat command to @oin $concatenate& list" and list, into a new file called biglist -ype

8 cat list1 listI E biglist (hat this is doing is reading the contents of list" and list, in turn, then outputing the te,t to the file biglist -o read the contents of the new file, type 8 cat biglist

.$. 7edirecting the Input


(e use the D symbol to redirect the input of a command -he command sort alphabetically or numerically sorts a list -ype 8 sort -hen type in the names of some animals 4ress 9Aeturn: after each one dog cat bird ape J< $control d to stop& -he output will be ape bird cat dog Using D you can redirect the input to come from a file rather than the "eyboard 2or e,ample, to sort the list of fruit, type 8 sort D biglist and the sorted list will be output to the screen -o output the sorted list to a file, type, 8 sort D biglist E slist Use cat to read the contents of the file slist

.$2 3ipes
-o see who is on the system with you, type 8 who 1ne method to get a sorted list of names is to type, 8 who E names t,t 8 sort D names t,t -his is a bit slow and you have to remember to remove the temporary file called names when you have finished (hat you really want to do is connect the output of the who command directly to the input of the sort command -his is e,actly what pipes do -he symbol for a pipe is the vertical bar M

2or e,ample, typing 8 who M sort will give the same result as above, but *uic"er and cleaner -o find out how many users are logged on, type 8 who M wc #l

01ercise .b
Using pipes, display all lines of list" and list, containing the letter +p+, and sort the result 7nswer available here Dpipeanswer htmlE

Summary
/ommand command E file command EE file command D file command1 M command2 cat file1 file2 E file0 sort who -eaning redirect standard output to a file append standard output to a file redirect standard input from a file pipe the output of command1 to the input of commandI concatenate file1 and fileI to file0 sort data list users currently logged in

UNIX Tutorial Four


2$" Wildcards
The = 4ildcard
-he character N is called a wildcard, and will match against none or more character$s& in a file $or directory& name 2or e,ample, in your uni1stuff directory, type 8 ls listN -his will list all files in the current directory starting with list$$$$ -ry typing 8 ls Nlist -his will list all files in the current directory ending with $$$$list

The ? 4ildcard
-he character C will match e,actly one character .o ?ouse will match files li"e house and mouse, but not grouse -ry typing 8 ls Clist

2$, Filename con#entions


(e should note here that a directory is merely a special type of file .o the rules and conventions for naming files apply also to directories In naming files, characters with special meanings such as % = > ? , should be avoided 7lso, avoid using spaces within names -he safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with O $underscore& and $dot& @ood filenames pro@ect t,t myObigOprogram c fredOdave doc Aad filenames pro@ect my big program c fred P dave doc

2ile names conventionally start with a lower#case letter, and may end with a dot followed by a group of letters indicating the contents of the file 2or e,ample, all files consisting of 3 code may be named with the ending $c, for e,ample, prog"$c -hen in order to list all files containing 3 code in your home directory, you need only type ls N c in that directory

2$. @etting Belp


&n+line -anuals
-here are on#line manuals which gives information about most commands -he manual pages tell you which options a particular command can ta"e, and how each option modifies the behaviour of the command -ype man command to read the manual page for a particular command 2or e,ample, to find out more about the wc $word count& command, type 8 man wc 7lternatively 8 whatis wc gives a one#line description of the command, but omits any information about options etc

<propos
(hen you are not sure of the e,act name of a command, 8 apropos "eyword will give you the commands with "eyword in their manual page header 2or e,ample, try typing 8 apropos copy

Summary
/ommand N -eaning match any number of characters

C man command whatis command apropos keyword

match one character read the online manual page for a command brief description of a command match commands with "eyword in their man pages

UNIX Tutorial Fi#e


!$" File system security (access rights)
In your uni1stuff directory, type 8 ls #l $l for long listing?& >ou will see that you now get lots of details about the contents of your directory, similar to the e,ample below

;ach file $and directory& has associated access rights, which may be found by typing ls #l 7lso, ls #lg gives additional information as to which group owns the file $beng9G in the following e,ample&6 #rw,rw#r## 1 eeG1ab beng9G IQG0 .eptI9 116GI file1 In the left#hand column is a 10 symbol string consisting of the symbols d, r, w, ,, #, and, occasionally, s or . If d is present, it will be at the left hand end of the string, and indicates a directory6 otherwise # will be the starting symbol of the string -he 9 remaining symbols indicate the permissions, or access rights, and are ta"en as three groups of R -he left group of R gives the file permissions for the user that owns the file $or directory& $eeG1ab in the above e,ample&5 the middle group gives the permissions for the group of people to whom the file $or directory& belongs $eebeng9G in the above e,ample&5 the rightmost group gives the permissions for all others

-he symbols r, w, etc , have slightly different meanings depending on whether they refer to a simple file or to a directory

<ccess rights on files$


r $or #&, indicates read permission $or otherwise&, that is, the presence or absence 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,ecution permission $or otherwise&, that is, the permission to e,ecute a file, where appropriate r allows users to list files in the directory5 w means that users may delete files from the directory or move files into it5 , means the right to access files in the directory -his implies that you may read files in the directory provided you have read permission on the individual files

<ccess rights on directories$


.o, in order to read a file, you must have e,ecute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree

Some e1amples
# rw,rw,rw , #rw####### a file that everyone can read, write and e,ecute $and delete& a file that only the owner can read and write # no#one else can read or write and no#one has e,ecution rights $e g your mailbo, file&

!$, /hanging access rights


chmod (changing a file mode)
1nly the owner of a file can use chmod to change the permissions of a file -he options of chmod are as follows Symbol u g o a r w , S # -eaning user group other all read write $and delete& e,ecute $and access directory& add permission ta"e away permission

2or e,ample, to remove read write and e,ecute permissions on the file biglist for the group and others, type 8 chmod go#rw, biglist -his will leave the other permissions unaffected -o give read and write permissions on the file biglist to all, 8 chmod aSrw biglist

01ercise !a
-ry changing access permissions on the file science$t1t and on the directory backups Use ls #l to chec" that the permissions have changed

!$. 3rocesses and Cobs


7 process is an e,ecuting program identified by a uni*ue 4I< $process identifier& -o see information about your processes, with their associated 4I< and status, type 8 ps 7 process may be in the foreground, in the bac"ground, or be suspended In general the shell does not return the UNIX prompt until the current process has finished e,ecuting .ome processes ta"e a long time to run and hold up the terminal !ac"grounding a long process has the effect that the UNIX prompt is returned immediately, and other tas"s can be carried out while the original process continues e,ecuting

7unning background processes


-o bac"ground a process, type an > at the end of the command line 2or e,ample, the command sleep waits a given number of seconds before continuing -ype 8 sleep 10 -his will wait 10 seconds before returning the command prompt 8 Until the command prompt is returned, you can do nothing e,cept wait -o run sleep in the bac"ground, type 8 sleep 10 P 91: 6IG9 -he > runs the @ob in the bac"ground and returns the prompt straight away, allowing you do run other programs while waiting for that one to finish -he first line in the above e,ample is typed in by the user5 the ne,t line, indicating @ob number and 4I<, is returned by the machine -he user is be notified of a @ob number $numbered from 1& enclosed in s*uare brac"ets, together with a 4I< and is notified when a bac"ground process is finished !ac"grounding is useful for @obs which will ta"e a long time to complete

Aackgrounding a current foreground process


7t the prompt, type 8 sleep 1000 >ou can suspend the process running in the foreground by typing ;D, i e hold down the 9/trl: "ey and type 9E: -hen to put it in the bac"ground, type 8 bg Note6 do not bac"ground programs that re*uire user interaction e g vi

!$2 'isting suspended and background processes


(hen a process is running, bac"grounded or suspended, it will be entered onto a list along with a @ob number -o e,amine this list, type 8 @obs 7n e,ample of a @ob list could be 91: .uspended sleep 1000 9I: Aunning netscape 9R: Aunning matlab -o restart $foreground& a suspended processes, type 8 fg 8jobnumber 2or e,ample, to restart sleep 1000, type 8 fg 81 -yping fg with no @ob number foregrounds the last suspended process

!$! Filling a process


kill (terminate or signal a process)
It is sometimes necessary to "ill a process $for e,ample, when an e,ecuting program is in an infinite loop& -o "ill a @ob running in the foreground, type ;/ $control c& 2or e,ample, run 8 sleep 100 J3 -o "ill a suspended or bac"ground process, type 8 "ill 8jobnumber 2or e,ample, run 8 sleep 100 P 8 @obs If it is @ob number Q, type 8 "ill 8Q -o chec" whether this has wor"ed, e,amine the @ob list again to see if the process has been removed

ps (process status)
7lternatively, processes can be "illed by finding their process numbers $4I<s& and using "ill PID_number 8 sleep 1000 P 8 ps 4I< -- . -I'; 31''7N< I00TT pts/G . 060G sleep 1000

I1G6R pts/G - 0600 netscape I1UTR pts/G . 06IG nedit -o "ill off the process sleep "GGG, type 8 "ill I00TT and then type ps again to see if it has been removed from the list If a process refuses to be "illed, uses the +* option, i e type 8 "ill #9 I00TT Note6 It is not possible to "ill off other users+ processes ???

Summary
/ommand ls #lag chmod 9options: file command P J3 JV bg @obs fg 81 "ill 81 ps "ill I61GI -eaning list access rights for all files change access rights for named file run command in bac"ground "ill the @ob running in the foreground suspend the @ob running in the foreground bac"ground the suspended @ob list current @obs foreground @ob number 1 "ill @ob number 1 list current processes "ill process number I61GI

UNIX Tutorial Si1


&ther useful UNIX commands
9uota
7ll students are allocated a certain amount of dis" space on the file system for their personal files, usually about 100'b If you go over your *uota, you are given T days to remove e,cess files -o chec" your current *uota and how much of it you have used, type 8 *uota #v

df
-he df command reports on the space left on the file system 2or e,ample, to find out how much space is left on the fileserver, type 8 df

du
-he du command outputs the number of "ilobyes used by each subdirectory Useful if you have gone over *uota and you want to find out which directory has the most files In your home#directory, type 8 du #s N -he +s flag will display only a summary $total siFe& and the N means all files and directories

gEip
-his reduces the siFe of a file, thus freeing valuable dis" space 2or e,ample, type 8 ls #l science t,t and note the siFe of the file using ls #l -hen to compress science t,t, type 8 gFip science t,t -his will compress the file and place it in a file called science$t1t$gE -o see the change in siFe, type ls #l again -o e,pand the file, use the gunFip command 8 gunFip science t,t gF

Ecat
Fcat will read gFipped files without needing to uncompress them first 8 Fcat science t,t gF If the te,t scrolls too fast for you, pipe the output though less 8 Fcat science t,t gF M less

file
file classifies the named files according to the type of data they contain, for e,ample ascii $te,t&, pictures, compressed data, etc -o report on all files in your home directory, type 8 file N

diff
-his command compares the contents of two files and displays the differences .uppose you have a file called file" and you edit some part of it and save it as file, -o see the differences type 8 diff file1 fileI 0ines beginning with a H denotes file1, while lines beginning with a I denotes fileI

find
-his searches through the directories for files and directories with a given name, date, siFe, or any other attribute you care to specify It is a simple command but with many options # you can read the manual by typing man find -o search for all fies with the e,tention $t1t, starting at the current directory $ & and

wor"ing through all sub#directories, then printing the name of the file to the screen, type 8 find #name =N t,t= #print -o find files over 1'b in siFe, and display the result as a long listing, type 8 find #siFe S1' #ls

history
-he 3 shell "eeps an ordered list of all the commands that you have entered ;ach command is given a number according to the order it was entered 8 history $show command history list& If you are using the 3 shell, you can use the e,clamation character $J& to recall commands easily 8 ?? $recall last command& 8 ?#R $recall third most recent command& 8 ?G $recall Gth command in list& 8 ?grep $recall last command starting with grep& >ou can increase the siFe of the history buffer by typing 8 set historyW100

UNIX Tutorial Se#en


K$" /ompiling UNIX soft4are packages
(e have many public domain and commercial software pac"ages installed on our systems, which are available to all users )owever, students are allowed to download and install small software pac"ages in their own home directory, software usually only useful to them personally -here are a number of steps needed to install the software 0ocate and download the source code $which is usually compressed& Unpac" the source code 3ompile the code Install the resulting e,ecutable .et paths to the installation directory

1f the above steps, probably the most difficult is the compilation stage

/ompiling Source /ode


7ll high#level language code must be converted into a form the computer understands 2or e,ample, 3 language source code is converted into a lower#level language called assembly language -he assembly language code made by the previous stage is then converted into ob@ect code which are fragments of code which the computer understands

directly -he final stage in compiling a program involves lin"ing the ob@ect code to code libraries which contain certain built#in functions -his final stage produces an e,ecutable program -o do all these steps by hand is complicated and beyond the capability of the ordinary user 7 number of utilities and tools have been developed for programmers and end#users to simplify these steps

make and the -akefile


-he ma"e command allows programmers to manage large programs or groups of programs It aids in developing large programs by "eeping trac" of which portions of the entire program have been changed, compiling only those parts of the program which have changed since the last compile -he ma"e program gets its set of compile rules from a te,t file called -akefile which resides in the same directory as the source files It contains information on how to compile the software, e g the optimisation level, whether to include debugging info in the e,ecutable It also contains information on where to install the finished compiled binaries $e,ecutables&, manual pages, data files, dependent library files, configuration files, etc .ome pac"ages re*uire you to edit the 'a"efile by hand to set the final installation directory and any other parameters )owever, many pac"ages are now being distributed with the %NU configure utility

configure
7s the number of UNIX variants increased, it became harder to write programs which could run on all variants <evelopers fre*uently did not have access to every system, and the characteristics of some systems changed from version to version -he %NU configure and build system simplifies the building of programs distributed as source code 7ll programs are built using a simple, standardised, two step process -he program builder need not install any special tools in order to build the program -he configure shell script attempts to guess correct values for various system#dependent variables used during compilation It uses those values to create a -akefile in each directory of the pac"age -he simplest way to compile a pac"age is6 cd to the directory containing the pac"age+s source code -ype /configure to configure the pac"age for your system -ype ma"e to compile the pac"age 1ptionally, type ma"e chec" to run any self#tests that come with the pac"age -ype ma"e install to install the programs and any data files and documentation 1ptionally, type ma"e clean to remove the program binaries and ob@ect files from the source code directory -he configure utility supports a wide variety of options >ou can usually use the ++help option to get a list of interesting options for a particular configure script

-he only generic options you are li"ely to use are the ++prefi1 and ++e1ec+prefi1 options -hese options are used to specify the installation directories -he directory named by the ++prefi1 option will hold machine independent files such as documentation, data and configuration files -he directory named by the ++e1ec+prefi1 option, $which is normally a subdirectory of the ++prefi1 directory&, will hold machine dependent files such as e,ecutables

K$, Do4nloading source code


2or this e,ample, we will download a piece of free software that converts between different units of measurements 2irst create a download directory 8 m"dir download <ownload the software here D/-eaching/Uni,/units#1 TQ tar gFE and save it to your new download directory

K$. 01tracting the source code


%o into your do4nload directory and list the contents 8 cd download 8 ls #l 7s you can see, the filename ends in tar gF -he tar command turns several files and directories into one single tar file -his is then compressed using the gFip command $to create a tar gF file& 2irst unFip the file using the gunFip command -his will create a tar file 8 gunFip units#1 TQ tar gF -hen e,tract the contents of the tar file 8 tar #,vf units#1 TQ tar 7gain, list the contents of the do4nload directory, then go to the units+"$K2 sub# directory 8 cd units#1 TQ

K$2 /onfiguring and creating the -akefile


-he first thing to do is carefully read the 70<D-0 and INST<'' te,t files $use the less command& -hese contain important information on how to compile and run the software -he units pac"age uses the %NU configure system to compile the source code (e will need to specify the installation directory, since the default will be the main system area which you will not have write permissions for (e need to create an install directory in your home directory 8 m"dir B/units1TQ -hen run the configure utility setting the installation path to this

8 /configure ##prefi,WX)1';/units1TQ N1-;6 -he LB&-0 variable is an e,ample of an environment variable -he value of LB&-0 is the path to your home directory Yust type 8 echo X)1'; to show the contents of this variable (e will learn more about environment variables in a later chapter If configure has run correctly, it will have created a 'a"efile with all necessary options >ou can view the 'a"efile if you wish $use the less command&, but do not edit the contents of this

K$! Auilding the package


Now you can go ahead and build the pac"age by running the ma"e command 8 ma"e 7fter a minute or two $depending on the speed of the computer&, the e,ecutables will be created >ou can chec" to see everything compiled successfully by typing 8 ma"e chec" If everything is o"ay, you can now install the pac"age 8 ma"e install -his will install the files into the 6%units"K2 directory you created earlier

K$5 7unning the soft4are


>ou are now ready to run the software $assuming everything wor"ed& 8 cd B/units1TQ If you list the contents of the units directory, you will see a number of subdirectories bin info man share 8 /units 7s an e,ample, convert 6 feet to metres >ou have6 6 feet >ou want6 metres N 1 UIUU If you get the answer 1 UIUU, congratulations, it wor"ed -o view what units it can convert between, view the data file in the share directory $the list is *uite comprehensive& -he binary e,ecutables %NU info formatted documentation 'an pages .hared data files

-o run the program, change to the bin directory and type

-o read the full documentation, change into the info directory and type 8 info ##fileWunits info

K$K Stripping unnecessary code


(hen a piece of software is being developed, it is useful for the programmer to include debugging information into the resulting e,ecutable -his way, if there are problems encountered when running the e,ecutable, the programmer can load the e,ecutable into a debugging software pac"age and trac" down any software bugs -his is useful for the programmer, but unnecessary for the user (e can assume that the pac"age, once finished and available for download has already been tested and debugged )owever, when we compiled the software above, debugging information was still compiled into the final e,ecutable .ince it is unli"ey that we are going to need this debugging information, we can strip it out of the final e,ecutable 1ne of the advantages of this is a much smaller e,ecutable, which should run slightly faster (hat we are going to do is loo" at the before and after siFe of the binary file 2irst change into the bin directory of the units installation directory 8 cd B/units1TQ/bin 8 ls #l 7s you can see, the file is over 100 "bytes in siFe >ou can get more information on the type of file by using the file command 8 file units units6 ;02 RI#bit 0.! e,ecutable, Intel U0RU6, version 1, dynamically lin"ed $uses shared libs&, not stripped -o strip all the debug and line numbering information out of the binary file, use the strip command 8 strip units 8 ls #l 7s you can see, the file is now R6 "bytes # a third of its original siFe -wo thirds of the binary file was debug code??? 3hec" the file information again 8 file units units6 ;02 RI#bit 0.! e,ecutable, Intel U0RU6, version 1, dynamically lin"ed $uses shared libs&, stripped .ometimes you can use the ma"e command to install pre#stripped copies of all the binary files when you install the pac"age Instead of typing ma"e install, simply type ma"e install#strip

UNIX Tutorial 0ight


M$" UNIX Nariables
Zariables are a way of passing information from the shell to programs when you run them 4rograms loo" =in the environment= for particular variables and if they are found will use the values stored .ome are set by the system, others by you, yet others by the shell, or any program that loads another program .tandard UNIX variables are split into two categories, environment variables and shell variables In broad terms, shell variables apply only to the current instance of the shell and are used to set short#term wor"ing conditions5 environment variables have a farther reaching significance, and those set at login are valid for the duration of the session !y convention, environment variables have U44;A 37.; and shell variables have lower case names

M$, 0n#ironment Nariables


7n e,ample of an environment variable is the 1.->4; variable -he value of this is the current operating system you are using -ype 8 echo X1.->4; 'ore e,amples of environment variables are U.;A $your login name& )1'; $the path name of your home directory& )1.- $the name of the computer you are using& 7A3) $the architecture of the computers processor& <I.407> $the name of the computer screen to display X windows& 4AIN-;A $the default printer to send print @obs& 47-) $the directories the shell should search to find a command&

Finding out the current #alues of these #ariables$


;NZIA1N';N- variables are set using the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command -o show all values of these variables, type 8 printenv M less

M$. Shell Nariables


7n e,ample of a shell variable is the history variable -he value of this is how many shell commands to save, allow the user to scroll bac" through all the commands they have previously entered -ype 8 echo Xhistory 'ore e,amples of shell variables are

cwd $your current wor"ing directory& home $the path name of your home directory& path $the directories the shell should search to find a command& prompt $the te,t string used to prompt for interactive commands shell your login shell&

Finding out the current #alues of these #ariables$


.);00 variables are both set and displayed using the set command -hey can be unset by using the unset command -o show all values of these variables, type 8 set M less

So 4hat is the difference bet4een 3<TB and path ?


In general, environment and shell variables that have the same name $apart from the case& are distinct and independent, e,cept for possibly having the same initial values -here are, however, e,ceptions ;ach time the shell variables home, user and term are changed, the corresponding environment variables )1';, U.;A and -;A' receive the same values )owever, altering the environment variables has no effect on the corresponding shell variables 47-) and path specify directories to search for commands and programs !oth variables always represent the same directory list, and altering either automatically causes the other to be changed

M$2 Using and setting #ariables


;ach time you login to a UNIX host, the system loo"s in your home directory for initialisation files Information in these files is used to set up your wor"ing environment -he 3 and -3 shells uses two files called login and cshrc $note that both file names begin with a dot& 7t login the 3 shell first reads $cshrc followed by $login $login is to set conditions which will apply to the whole session and to perform actions that are relevant only at login $cshrc is used to set conditions and perform actions specific to the shell and to each invocation of it -he guidelines are to set ;NZIA1N';N- variables in the $login file and .);00 variables in the $cshrc file W<7NIN@O N;Z;A put commands that run graphical displays $e g a web browser& in your cshrc or login file

M$! Setting shell #ariables in the $cshrc file


2or e,ample, to change the number of shell commands saved in the history list, you need to set the shell variable history It is set to 100 by default, but you can increase this if you

wish 8 set history W I00 3hec" this has wor"ed by typing 8 echo Xhistory )owever, this has only set the variable for the lifetime of the current shell If you open a new ,term window, it will only have the default history value set -o 4;A'7N;N-0> set the value of history, you will need to add the set command to the cshrc file 2irst open the $cshrc file in a te,t editor 7n easy, user#friendly editor to use is nedit 8 nedit B/ cshrc 7dd the following line 72-;A the list of other commands set history W I00 .ave the file and force the shell to reread its cshrc file buy using the shell source command 8 source cshrc 3hec" this has wor"ed by typing 8 echo Xhistory

M$5 Setting the path


(hen you type a command, your path $or 47-)& variable defines in which directories the shell will loo" to find the command you typed If the system returns a message saying =command6 3ommand not found=, this indicates that either the command doesn+t e,ist at all on the system or it is simply not in your path 2or e,ample, to run units, you either need to directly specify the units path $6%units"K2%bin%units&, or you need to have the directory 6%units"K2%bin in your path >ou can add it to the end of your e,isting path $the Lpath represents this& by issuing the command6 8 set path W $Xpath B/units1TQ/bin& -est that this wor"ed by trying to run units in any directory other that where units is actually located 8 cd 8 units -o add this path 4;A'7N;N-0>, add the following line to your cshrc 72-;A the list of other commands set path W $Xpath B/units1TQ/bin&

You might also like