Professional Documents
Culture Documents
Basic Unix Tutorial
Basic Unix Tutorial
Basic Unix Tutorial
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 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
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
"$! 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&
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
01ercise ,a
3reate a bac"up of your science$t1t file by copying it to a file called science$bak
inside the uni1stuff directory, type 8 mv science ba" bac"ups/ -ype ls and ls bac"ups to see if it has wor"ed
01ercise ,b
3reate a directory called tempstuff using m"dir , then remove it using the rmdir command
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
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
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
01ercise .a
Using the above method, create another file called list, containing the following fruit6 orange, plum, mango, grapefruit Aead the contents of list,
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
.$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
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
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
<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
match one character read the online manual page for a command brief description of a command match commands with "eyword in their man pages
;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
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
.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&
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
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
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
1f the above steps, probably the most difficult is the compilation stage
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
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
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
-o read the full documentation, change into the info directory and type 8 info ##fileWunits info
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&
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