Linux Basics
Linux Basics
Linux Basics
5
Running Commands from the Shell ....................................................................................5
Using Virtual Terminals......................................................................................................5
Choosing Your Shell............................................................................................................6
Checking Your Login Session.............................................................................................6
Checking irectories and Permissions................................................................................!
Checking S"stem #cti$it"...................................................................................................%
&'iting the Shell...................................................................................................................(
Using the Shell in Linu'.....................................................................................................(
Locating Commands..........................................................................................................)*
Starting +ackground Processes..........................................................................................),
Using -oreground and +ackground Commands................................................................),
.orking /ith the Linu' -ile S"stem.................................................................................)0
Using -ile1Redirection 2etacharacters.............................................................................)6
Listing -iles.......................................................................................................................)6
Cop"ing -iles.....................................................................................................................)6
2o$ing and Renaming -iles..............................................................................................)!
eleting -iles and irectories...........................................................................................)!
Changing irectories.........................................................................................................)%
2aking irectories............................................................................................................)%
Remo$ing irectories........................................................................................................)%
2aking Links to -iles or irectories.................................................................................)%
Concatenating -iles............................................................................................................)(
Vie/ing -iles /ith more and less......................................................................................)(
Vie/ing the Start or &nd of -iles......................................................................................,*
Searching -iles /ith grep...................................................................................................,*
-inding -iles /ith find and locate.....................................................................................,*
+asic User and 3roup Concepts........................................................................................,)
Creating Users and 3roups................................................................................................,,
.orking /ith -ile 4/nership and Permissions................................................................,,
2ounting and Unmounting -iles"stems............................................................................,5
S"stem information related commands..............................................................................,6
2emor" Reporting /ith the free Command......................................................................,6
Virtual 2emor" Reporting /ith the $mstat.......................................................................,6
Reclaiming 2emor" /ith the kill Command....................................................................,!
etermining 5o/ Long Linu' 5as +een Running...........................................................,!
Runle$els............................................................................................................................,%
Using the $i Te't &ditor.....................................................................................................,(
#utomated Tasks...............................................................................................................00
Cron....................................................................................................................................00
6-S....................................................................................................................................06
Setting Up an 6-S Ser$er..................................................................................................06
3etting the ser$ices Started...............................................................................................7)
The aemons.....................................................................................................................7)
Verif"ing that 6-S is running............................................................................................7,
Setting up an 6-S Client...................................................................................................70
)
2ounting Remote irectories............................................................................................70
3etting 6-S -ile S"stems to 8e 2ounted at +oot Time...................................................77
2ount 4ptions...................................................................................................................75
69S ....................................................................................................................................76
5o/ 69S /orks .............................................................................................................76
5o/ 69S: /orks ..........................................................................................................7!
2anaging S"stem Logs......................................................................................................7!
Logrotate............................................................................................................................5*
The difference 8et/een hard and soft links.......................................................................5,
-ile Compression and #rchi$ing.......................................................................................5!
Package 2anagement /ith RP2.......................................................................................6*
Compiling from the original source...................................................................................6(
"um....................................................................................................................................!0
s"sctl .................................................................................................................................!%
Linu' Partitions .................................................................................................................!(
Partition T"pes...................................................................................................................%0
LV2...................................................................................................................................%(
U69; Sumar"....................................................................................................................(0
9ntroduction .......................................................................................................................(0
The U69; operating s"stem .........................................................................................(7
The kernel .................................................................................................................(7
The shell.....................................................................................................................(7
-iles and processes ........................................................................................................(5
The irector" Structure ................................................................................................(5
Starting an ;terminal session .......................................................................................(5
Part 4ne ............................................................................................................................(!
).) Listing files and directories .....................................................................................(!
ls <list= .......................................................................................................................(!
)., 2aking irectories .................................................................................................(%
mkdir <make director"= .............................................................................................(%
).0 Changing to a different director" ............................................................................(%
cd <change director"=.................................................................................................(%
&'ercise )a.................................................................................................................(%
).7 The directories . and .. .............................................................................................(%
).5 Pathnames ...............................................................................................................((
p/d <print /orking director"= ...................................................................................((
.................................................................................................................................)**
&'ercise )8...............................................................................................................)**
).6 2ore a8out home directories and pathnames .......................................................)**
Understanding pathnames........................................................................................)**
> <"our home director"= ..........................................................................................)*)
Summar" .....................................................................................................................)*)
Part T/o ..........................................................................................................................)*,
,.) Cop"ing -iles.........................................................................................................)*,
cp <cop"=..................................................................................................................)*,
&'ercise ,a...............................................................................................................)*,
,
,., 2o$ing files...........................................................................................................)*,
m$ <mo$e=................................................................................................................)*,
,.0 Remo$ing files and directories .............................................................................)*0
rm <remo$e=? rmdir <remo$e director"=...................................................................)*0
&'ercise ,8...............................................................................................................)*0
,.7 ispla"ing the contents of a file on the screen .....................................................)*7
clear <clear screen=...................................................................................................)*7
cat <concatenate=......................................................................................................)*7
less............................................................................................................................)*7
head..........................................................................................................................)*7
tail............................................................................................................................)*5
,.5 Searching the contents of a file .............................................................................)*5
Simple searching using less.....................................................................................)*5
grep <don@t ask /h" it is called grep=.......................................................................)*5
/c </ord count=.......................................................................................................)*6
Summar" .....................................................................................................................)*!
Part Three ........................................................................................................................)*!
0.) Redirection ..........................................................................................................)*!
0., Redirecting the 4utput ........................................................................................)*%
&'ercise 0a...............................................................................................................)*%
0.0 Redirecting the 9nput ...........................................................................................)*(
0.7 Pipes.......................................................................................................................))*
&'ercise 08...............................................................................................................))*
Summar" .....................................................................................................................)))
Part -our ..........................................................................................................................)))
7.) .ildcards...............................................................................................................)))
The characters A and B..............................................................................................)))
7., -ilename con$entions ...........................................................................................)),
7.0 3etting 5elp...........................................................................................................)),
4n1line 2anuals.......................................................................................................)),
#propos....................................................................................................................))0
Summar" .....................................................................................................................))0
Part -i$e ..........................................................................................................................))0
5.) -ile s"stem securit" <access rights= .......................................................................))0
#ccess rights on files...............................................................................................))7
#ccess rights on directories.....................................................................................))5
Some e'amples........................................................................................................))6
5., Changing access rights...........................................................................................))6
chmod <changing a file mode=.................................................................................))6
&'ercise 5a...............................................................................................................))!
5.0 Processes and Co8s ................................................................................................))!
Running 8ackground processes................................................................................))!
+ackgrounding a current foreground process..........................................................))%
5.7 Listing suspended and 8ackground processes .......................................................))%
5.5 Dilling a process ...................................................................................................))%
kill <terminate or signal a process=...........................................................................))%
0
ps <process status=....................................................................................................))(
Summar" .....................................................................................................................))(
Part Si' ............................................................................................................................),*
4ther useful U69; commands .................................................................................),*
Euota.........................................................................................................................),*
df..............................................................................................................................),*
du..............................................................................................................................),)
compress..................................................................................................................),)
gFip...........................................................................................................................),)
file............................................................................................................................),)
histor"......................................................................................................................),,
Part Se$en .......................................................................................................................),,
!.) Compiling U69; soft/are packages ..................................................................),,
Compiling Source Code...........................................................................................),0
make and the 2akefile.............................................................................................),0
configure..................................................................................................................),0
!., o/nloading source code......................................................................................),7
!.0 &'tracting the source code ..................................................................................),7
!.7 Configuring and creating the 2akefile ...............................................................),5
!.5 +uilding the package ............................................................................................),5
!.6 Running the soft/are.............................................................................................),6
!.! Stripping unnecessar" code....................................................................................),!
Part &ight ........................................................................................................................),%
%.) U69; Varia8les.....................................................................................................),%
%., &n$ironment Varia8les..........................................................................................),%
-inding out the current $alues of these $aria8les.....................................................),(
%.0 Shell Varia8les.......................................................................................................),(
-inding out the current $alues of these $aria8les.....................................................),(
So /hat is the difference 8et/een P#T5 and path B...............................................),(
%.7 Using and setting $aria8les....................................................................................)0*
%.5 Setting shell $aria8les in the .cshrc file.................................................................)0*
%.6 Setting the path......................................................................................................)0)
Uni' 1 -reEuentl" #sked Guestions <)= H-reEuent postingI............................................)0,
Uni' 1 -reEuentl" #sked Guestions <,= H-reEuent postingI............................................)0!
Uni' 1 -reEuentl" #sked Guestions <0= H-reEuent postingI............................................)5)
Uni' 1 -reEuentl" #sked Guestions <7= H-reEuent postingI............................................)6!
Uni' 1 -reEuentl" #sked Guestions <5= H-reEuent postingI............................................)!6
7
Using the Shell Prompt
9f "our Linu' s"stem has no graphical user interface <or one that isnJt /orking at the
moment=? "ou /ill most likel" see a shell prompt after "ou log in.? T"ping commands
from the shell /ill pro8a8l" 8e "our primar" means of using the Linu' s"stem.
The default prompt for a regular user is simpl" a dollar signK
L
The default prompt for the root user is a pound sign <also called a hash mark=K
M
Running Commands from the Shell
9n most Linu' s"stems? the L and M prompts are preceded 8" "our username? s"stem
name? and current director" name. -or e'ample? a login prompt for the user
named Nake on a computer named pine /ith Otmp as the current director" /ould
appear asK
[jake@pine tmp]$
You can change the prompt to displa" an" characters "ou likeP"ou can use the
current director"? the date? the local computer name? or an" string of characters as
"our prompt? for e'ample.
#lthough there are a tremendous num8er of features a$aila8le /ith the shell? itJs eas" to
8egin 8" Nust t"ping a fe/ commands. Tr" some of the commands sho/n in the
remainder of this section to 8ecome familiar /ith "our current shell en$ironment.
9n the e'amples that follo/? the L and M s"m8ols indicate a prompt. The prompt is
follo/ed 8" the command that "ou t"pe <and then "ou press &nter or Return?
depending on "our ke"8oard=. The lines that follo/ sho/ the output resulting from
the command.
Using Virtual Terminals
2an" Linu' s"stems? including -edora and Red 5at &nterprise Linu'? start multiple
$irtual terminals running on the computer. Virtual terminals are a /a" to ha$e multiple
shell sessions open at once /ithout ha$ing a 3U9 running.
You can s/itch 8et/een $irtual terminals much the same /a" that "ou /ould
s/itch 8et/een /orkspaces on a 3U9. Press Ctrl:#lt:-) <or -,? -0? -7? and so on up to
-6 on -edora and other Linu' s"stems= to displa" one of si' $irtual terminals.
The ne't $irtual /orkspace after the $irtual terminals is /here the 3U9 is? so if there
are si' $irtual terminals? "ou can return to the 3U9 <if one is running= 8" pressing
Ctrl:#lt:-!. <-or a s"stem /ith four $irtual terminals? "ouJd return to the 3U9 8"
pressing Ctrl:#lt:-5.=
5
Choosing Your Shell
9n most Linu' s"stems? "our default shell is the 8ash shell. To find out /hat "our
current login shell is? t"pe the follo/ing commandK
$ echo $SHELL
/bin/bash
9n this e'ample? itJs the 8ash shell. There are man" other shells? and "ou can acti$ate a
different one 8" simpl" t"ping the ne/ shellJs command <ksh? tcsh? csh? sh? 8ash? and so
forth= from the current shell.
2ost full Linu' s"stems include all of the shells descri8ed in this section. 5o/e$er?
some smaller Linu' distri8utions ma" include onl" one or t/o shells. The 8est /a"
to find out if a particular shell is a$aila8le is to t"pe the command and see if the
shell starts.
You might /ant to choose a different shell to use 8ecauseK
You are used to using U69; S"stem V s"stems <often ksh 8" default= or Sun
2icros"stems and other +erkele" U69;Q8ased distri8utions <freEuentl" csh
8" default=? and "ou are more comforta8le using default shells from those
en$ironments.
You /ant to run shell scripts that /ere created for a particular shell en$ironment?
and "ou need to run the shell for /hich the" /ere made so "ou can test or use
those scripts.
You might simpl" like features in one shell o$er those in another. -or e'ample?
a mem8er of m" Linu' Users 3roup prefers ksh o$er 8ash 8ecause he doesnJt
like the /a" aliases are al/a"s set up /ith 8ash.
9f "ou donJt like "our default shell? simpl" t"pe the name of the shell "ou /ant to
tr" out temporaril". To change "our shell permanentl"? use the usermod command.
-or e'ample? to change "our shell to the csh shell for the user named chris?
t"pe the follo/ing as root user from a shellK
# usermod -s /bin/csh chris
Checking Your Login Session
.hen "ou log in to a Linu' s"stem? Linu' $ie/s "ou as ha$ing a particular identit"?
/hich includes "our username? group name? user 9? and group 9. Linu' also keeps
track of "our login sessionK it kno/s /hen "ou logged in? ho/ long "ou ha$e 8een
idle? and /here "ou logged in from.
To find out information a8out "our identit"? use the id command as follo/sK
$ id
uid=501(chris) gid=105(sales) groups=105(sales),4(adm),7(lp)
9n this e'ample? the username is chris? /hich is represented 8" the numeric user
9 <uid= 5*). The primar" group for chris is called sales? /hich has a group 9
<gid= of )*5. The user chris also 8elongs to other groups called adm <gid 7= and lp
6
<gid !=. These names and num8ers represent the permissions that chris has to
access computer resources. <Permissions are descri8ed in the RUnderstanding -ile
PermissionsS section later in this chapter.=
You can see information a8out "our current login session 8" using the /ho command.
9n the follo/ing e'ample? the 1u option sa"s to add information a8out idle
time and the process 9 and 15 asks that a header 8e printedK
$ who -uH
N!" #$N" %$!" $&#" '$& ()!!"N%
chris **+1 ,an 1- .0/57 0 .01-
The output from this /ho command sho/s that the user chris is logged in on tt")
</hich is the monitor connected to the computer=? and his login session 8egan at
,*K5! on Canuar" )0. The 9L& time sho/s ho/ long the shell has 8een open /ithout
an" command 8eing t"ped <the dot indicates that it is currentl" acti$e=. P9
sho/s the process 9 of the userJs login shell. C422&6T /ould sho/ the name of the
remote computer the user had logged in from? if that user had logged in from
another computer on the net/ork? or the name of the local ; displa" if "ou /ere
using a Terminal /indo/ <such as K*.*=.
Checking Directories and Permissions
#ssociated /ith each shell is a location in the Linu' file s"stem kno/n as the current
or working directory. &ach user has a director" that is identified as the userJs
home director". .hen "ou first log in to Linu'? "ou 8egin /ith "our home director"
as the current director".
.hen "ou reEuest to open or sa$e a file? "our shell uses the current director" as
the point of reference. Simpl" pro$ide a filename /hen "ou sa$e a file? and it is
placed in the current director". #lternati$el"? "ou can identif" a file 8" its relation
to the current director" <relati$e path=? or "ou can ignore the current director" and
identif" a file 8" the full director" hierarch" that locates it <a8solute path=. The
structure and use of the file s"stem is descri8ed in detail later in this chapter.
To find out /hat "our current director" is? t"pe the p/d commandK
$ pwd
/usr/bin
9n this e'ample? the currentO/orking director" is OusrO8in. To find out the name of
"our home director"? t"pe the echo command? follo/ed 8" the L542& $aria8leK
$ echo $HOME
/home/chris
5ere the home director" is OhomeOchris. To get 8ack to "our home director"? Nust
t"pe the change director" <cd= command. <#lthough cd follo/ed 8" a director"
name changes the current director" to the director" that "ou choose? simpl" t"ping
!
cd /ith no director" name takes "ou to "our home director"=K
$ cd
9nstead of t"ping L542&? "ou can use the tilde <>= to refer to "our home director".
So? to return to "our home director"? "ou could simpl" t"peK
cd ~
To list the contents of "our home director"? either t"pe the full path to "our home
director"? or use the ls command /ithout a director" name. Using the 1a option to
ls ena8les "ou to $ie/ the hidden files <dot files= as /ell as all other files. .ith the
1l option? "ou can see a long? detailed list of information on each file. <You can put
multiple single1letter options together after a single dash? for e'ample? 1la.=
$ ls -la /home/chris
*o*al 151
dr23r23r23 . chris sales 4045 !a+ 1. 1-/55 0
dr23r63r63 - roo* roo* 4045 !a+ 10 01/44 00
6r26666666 1 chris sales ..04 !a+ 11 .1/-0 0bash7his*or+
6r26r66r66 1 chris sales .4 !a+ 10 01/50 0bash7logou*
6r26r66r66 1 chris sales .-0 !a+ 10 01/50 0bash7pro8ile
6r26r66r66 1 chris sales 1.4 !a+ 10 01/50 0bashrc
dr26r66r66 1 chris sales 4045 !a+ 10 01/50 09de
6r26r26r66 1 chris sales 14417. !a+ 11 ../44 le**er
ispla"ing a long list <1l option= of the contents of "our home director" sho/s "ou
more a8out file siFes and directories. The total line sho/s the total amount of disk
space used 8" the files in the list <)5% kilo8"tes in this e'ample=. irectories such
as the current director" <.= and the parent director" <..=Pthe director" a8o$e
the current director"Pare noted as directories 8" the letter d at the 8eginning of
each entr" <each director" 8egins /ith a d and each file 8egins /ith a 1=. The file
and director" names are sho/n in column !. 9n this e'ample? a dot <.= represents
OhomeOchris and t/o dots <..= represents Ohome. 2ost of the files in this e'ample
are dot <.= files that are used to store 3U9 properties <.kde director"= or shell properties
<.8ash files=. The onl" non1dot file in this list is the one named letter.
The num8er of characters sho/n for a director" <7*(6 8"tes in these e'amples=
reflects the siFe of the file containing information a8out the director". .hile this
num8er can gro/ a8o$e 7*(6 8"tes for a director" that contains a lot of files? this
num8er doesnJt reflect the siFe of files contained in that director".
Checking System Actiity
9n addition to 8eing a multiuser operating s"stem? Linu' is also a multitasking s"stem.
Multitasking means that man" programs can 8e running at the same time. #n
instance of a running program is referred to as a process. Linu' pro$ides tools for
listing running processes? monitoring s"stem usage? and stopping <or killing= processes
/hen necessar".
%
The most common utilit" for checking running processes is the ps command. Use it
to see /hich programs are running? the resources the" are using? and /ho is running
them. 5ereJs an e'ample of the ps commandK
$ ps -au
:;"< '$& =(': =!"! >;? <;; %%@ ;%% ;%<% %$!" ()!!N&
roo* .145 000 001 1401 1100 **+p0 ; 14/50 0/00 login 66 Aa9e
Aa9e .147 000 007 11-5 10.0 **+p0 ; 14/50 0/00 6bash
Aa9e .-10 000 007 .54. 41. **+p0 < 11/.. 0/00 ps Bau
9n this e'ample? the 1a option asks to sho/ processes of all users /ho are associated
/ith "our current terminal? and the 1u option asks that usernames 8e sho/n?
as /ell as other information such as the time the process started and memor" and
CPU usage.
4n this shell session? there isnJt much happening. The first process sho/s that the
user named Nake logged in to the login process </hich is controlled 8" the root
user=. The ne't process sho/s that Nake is using a 8ash shell and has Nust run the
ps 1au command. The terminal de$ice tt"p* is 8eing used for the login session.
The ST#T column represents the state of the process? /ith R indicating a currentl"
running process and S representing a sleeping process.
The US&R column sho/s the name of the user /ho started the process. &ach process
is represented 8" a uniEue 9 num8er referred to as a process 9 <P9=. <You can use
the P9 if "ou e$er need to kill a runa/a" process.= The TCPU and T2&2 columns
sho/ the percentage of the processor and random access memor"? respecti$el"? that the
process is consuming. VSU <$irtual set siFe= sho/s the siFe of the image process
<in kilo8"tes=? and RSS <resident set siFe= sho/s the siFe of the program in memor".
ST#RT sho/s the time the process 8egan running? and T92& sho/s the cumulati$e
s"stem time used.
#lso tr" t"ping top? free and vmstat commands.
!"iting the Shell
To e'it the shell /hen "ou are done? t"pe exit or press Ctr!"#
YouJ$e Nust seen a fe/ commands that can help "ou Euickl" familiariFe "ourself
/ith "our Linu' s"stem. There are hundreds of other commands that "ou can tr".
YouJll find man" in the O8in and OusrO8in directories? and "ou can use ls to see a
director"Js command listK ls O8in? for e'ample? results in a list of commands in the
O8in. Then use the man command <for e'ample? man hostname to see /hat each
command does. There are also administrati$e commands in Os8in or OusrOs8in
directories.
Using the Shell in Linu"
.hen "ou t"pe a command in a shell? "ou can include other characters that change
or add to ho/ the command /orks. 9n addition to the command itself? these are
some of the other items that "ou can t"pe on a shell command lineK
(
$ptionsP2ost commands ha$e one or more options "ou can add to change
their 8eha$ior. 4ptions t"picall" consist of a single letter? preceded 8" a dash.
You can also often com8ine se$eral options after a single dash. -or e'ample?
the command ls 1la lists the contents of the current director". The 1l asks
for a detailed <long= list of information? and the 1a asks that files 8eginning
/ith a dot <.= also 8e listed. .hen a single option consists of a /ord? it is usuall"
preceded 8" a dou8le dash <11=. -or e'ample? to use the help option on
man" commands? "ou enter 11help on the command line.
You can use the 11help option /ith most commands to see the options and
arguments that the" support. -or e'ample? hostname 11help.
%r&umentsP2an" commands also accept arguments after certain options
are entered or at the end of the entire command line. #n argument is an e'tra
piece of information? such as a filename? that can 8e used 8" the command.
-or e'ample? cat OetcOpass/d displa"s the contents of the OetcOpass/d file
on "our screen. 9n this case? OetcOpass/d is the argument.
Environment varia'esPThe shell itself stores information that ma" 8e useful
to the userJs shell session in /hat are called environment variables.
&'amples of en$ironment $aria8les include LS5&LL </hich identifies the shell
"ou are using=? LPS) </hich defines "our shell prompt=? and L2#9L </hich
identifies the location of "our mail8o'=. See the RUsing Shell &n$ironment
Varia8lesS section later in this chapter for more information.
You can check "our en$ironment $aria8les at an" time. T"pe decare to list the current
en$ironment $aria8les. 4r "ou can t"pe echo $VALUE? /here VALUE is
replaced 8" the name of a particular en$ironment $aria8le "ou /ant to list.
(etacharactersPThese are characters that ha$e special meaning to the
shell. The" can 8e used to direct the output of a command to a file <V=? pipe
the output to another command <W=? and run a command in the 8ackground
<X=? to name a fe/. 2etacharacters are discussed later in this chapter.
Locating Commands
9f "ou kno/ the director" that contains the command "ou /ant to run? one /a" to
run it is to t"pe the full path to that command. -or e'ample? "ou run the date command
from the O8in director" 8" t"pingK
$ /bin/date
4f course? this can 8e incon$enient? especiall" if the command resides in a director"
/ith a long path name. The 8etter /a" is to ha$e commands stored in /ellkno/n
directories? and then add those directories to "our shellJs P#T5 en$ironment
$aria8le. The path consists of a list of directories that are checked seEuentiall" for
the commands "ou enter. To see "our current path? t"pe the follo/ingK
$ echo $PATH
/bin//usr/bin//usr/local/bin//usr/bin/C11//usr/C11<5/bin//home/chris/bin
5ere are some places "ou can look to supplement /hat "ou learn in this chapterK
)*
Check the P#T5PT"pe echo $)%*H. You see a list of the directories containing
commands
that are immediatel" accessi8le to "ou. Listing the contents of those directories
displa"s most standard Linu' commands.
Use the help commandPSome commands are 8uilt into the shell? so the" do not
appear in a director". The help command lists those commands and sho/s options
a$aila8le /ith each of them. <T"pe help W less to page through the list.= -or help
/ith a particular 8uilt1in command? t"pe hep command? replacing command /ith
the name that interests "ou. The help command /orks /ith the 8ash shell onl".
Use 11help /ith the commandP2an" commands include a 11help option that
"ou can use to get information a8out ho/ the command is used. -or e'ample? t"pe
date --hep + ess. The output sho/s not onl" options? 8ut also time formats "ou
can use /ith the date command.
Use the man commandPTo learn more a8out a particular command? t"pe man
command. <Replace command /ith the command name "ou /ant.= # description
of the command and its options appears on the screen.
$ type bash
bash is /bin/bash
To tr" out a 8it of command1line editing? t"pe the follo/ingK
$ ls /usr/bin | sort - | less
This command lists the contents of the OusrO8in director"? sorts the contents in
alpha8etical order <regardless of case=? and pipes the output to less. The less
command displa"s the first page of output? after /hich "ou can go through the rest
of the output a line <press &nter= or a page <press space 8ar= at a time <press G
/hen "ou are done=.
To $ie/ "our histor" list? use the histor" command. T"pe the command /ithout
options or follo/ed 8" a num8er to list that man" of the most recent commands.
-or e'ampleK
$ history !
-1. da*e
-1- ls /usr/bin D sor* 6a D more
-14 man sor*
-15 cd /usr/local/bin
-15 man more
-17 useradd 6m /home/chris 6u 101 chris
-14 his*or+ 1
# num8er precedes each command line in the list. There are se$eral /a"s to run a
command immediatel" from this list? includingK
YnPRun command num8er. Replace the n /ith the num8er of the command
))
line? and that line is run. -or e'ample? hereJs ho/ to repeat the date command
sho/n as command num8er 0%, in the preceding histor" listingK
$ "#$%
da*e
%hu pr 1- .1/-0/05 '&% .005
EE Run previous command. Runs the previous command line. Heres how
youd immediately run that same da*e command:
$ EE
da*e
%hu pr 1- .1/-0/-4 '&% .005
Starting #ackground Processes
9f "ou ha$e programs that "ou /ant to run /hile "ou continue to /ork in the shell?
"ou can place the programs in the 8ackground. To place a program in the 8ackground
at the time "ou run the program? t"pe an ampersand <X= at the end of the
command line? like thisK
L find ,usr - ,tmp,ausrfies .
This e'ample command finds all files on "our Linu' s"stem <starting from Ousr=?
prints those filenames? and puts those names in the file OtmpOallusrfiles. The
ampersand <X= runs that command line in the 8ackground. To check /hich commands
"ou ha$e running in the 8ackground? use the No8s command? as follo/sK
$ &obs
F1G ;*opped (**+ ou*pu*) Hi /*mp/m+8ile
F.G <unning 8ind /usr 6prin* I /*mp/allusr8iles J
F-G <unning nro88 6man /usr/man./K I/*mp/man. J
F4G6 <unning nro88 6man /usr/man-/K I/*mp/man- J
F5GL ;*opped nro88 6man /usr/man4/K I/*mp/man4
The first job shows a text-editin command !Hi" that # placed in the bac$round
and stopped by pressin %trl&' while # was editin. (ob ) shows the 8ind command
# just ran. (obs * and + show nro88 commands currently runnin in the bac$round.
(ob , had been runnin in the shell !foreround" until # decided too many
processes were runnin and pressed %trl&' to stop job , until a few processes had
completed.
Using $oreground and #ackground Commands
Continuing /ith the e'ample? "ou can 8ring an" of the commands on the No8s list to
the foreground. -or e'ample? to edit m"file again? t"peK
$ ' ()
#s a result? the $i command opens again? /ith all te't as it /as /hen "ou stopped
the $i No8.
TPRefers to the most recent command put into the 8ackground <indicated
8" the plus sign /hen "ou t"pe the No8s command=. This action 8rings the
command to the foreground.
),
TstringPRefers to a No8 /here the command 8egins /ith a particular
string of characters. The string must 8e unam8iguous. <9n other /ords?
t"ping T$i /hen there are t/o $i commands in the 8ackground results in an
error message.=
TBstringPRefers to a No8 /here the command line contains a string at an"
point. The string must 8e unam8iguous or the match /ill fail.
T11PRefers to the pre$ious No8 stopped 8efore the one most recentl"
stopped.
9f a command is stopped? "ou can start it running again in the 8ackground using the
8g command. -or e'ample? take No8 5 from the No8s list in the pre$ious e'ampleK
H5I: Stopped nroff 1man man7OA VOtmpOman7
T"pe the follo/ingK
$ b' (*
#fter that? the No8 runs in the 8ackground. 9ts No8s entr" appears as follo/sK
H5I Running nroff 1man man7OA VOtmpOman7 X
%orking &ith the Linu" $ile System
The Linu' file s"stem is the structure in /hich all the information on "our computer
is stored. -iles are organiFed /ithin a hierarch" of directories. &ach director"
can contain files? as /ell as other directories.
9f "ou /ere to map out the files and directories in Linu'? it /ould look like an
upside1do/n tree. #t the top is the root director"? /hich is represented 8" a single
slash <O=. +elo/ that is a set of common directories in the Linu' s"stem? such as
8in? de$? home? li8? and tmp? to name a fe/. &ach of those directories? as /ell as
directories added to the root? can contain su8directories.
-igure ,1) illustrates ho/ the Linu' file s"stem is organiFed as a hierarch". To
demonstrate ho/ directories are connected? the figure sho/s a Ohome director"
that contains su8directories for three usersK chris? mar"? and tom. .ithin the
chris director" are su8directoriesK 8riefs? memos? and personal. To refer to a file
called in$entor" in the chrisOmemos director"? "ou can t"pe the full path of
OhomeOchrisOmemosOin$entor". 9f "our current director" is OhomeOchrisOmemos?
"ou can refer to the file as simpl" in$entor".
Some of the Linu' directories that ma" interest "ou include the follo/ingK
O8inPContains common Linu' user commands? such as ls? sort? date? and
chmod.
O8ootP5as the 8oota8le Linu' kernel and 8oot loader configuration files
<3RU+=.
Ode$PContains files representing access points to de$ices on "our s"stems.
These include terminal de$ices <tt"A=? flopp" disks <fdA=? hard disks <hdA=?
R#2 <ramA=? and C1R42 <cdA=. <Users normall" access these de$ices
directl" through the de$ice files.=
)0
OetcPContains administrati$e configuration files.
OhomePContains directories assigned to each user /ith a login account.
OmediaPPro$ides a standard location for mounting and automounting
de$ices? such as remote file s"stems and remo$a8le media </ith director"
names of cdrecorder? flopp"? and so on=.
OmntP# common mount point for man" de$ices 8efore it /as supplanted 8"
the standard Omedia director". Some 8oota8le Linu' s"stems still used this
director" to mount hard disk partitions and remote file s"stems.
OprocPContains information a8out s"stem resources.
OrootPRepresents the root userJs home director".
Os8inPContains administrati$e commands and daemon processes.
Os"s <# Oproc1like file s"stem? ne/ in the Linu' ,.6 kernel and intended to
contain files for getting hard/are status and reflecting the s"stemJs de$ice
tree as it is seen 8" the kernel. 9t pulls man" of its functions from Oproc.
OtmpPContains temporar" files used 8" applications.
OusrPContains user documentation? games? graphical files <;))=? li8raries
<li8=? and a $ariet" of other user and administrati$e commands and files.
O$arPContains directories of data used 8" $arious applications. 9n particular?
this is /here "ou /ould place files that "ou share as an -TP ser$er
<O$arOftp= or a .e8 ser$er <O$arO///=. 9t also contains all s"stem log files
<O$arOlog=.
+sin' ,ile-Matchin' Metacharacters
AP2atches an" num8er of characters.
BP2atches an" one character.
H...IP 2atches an" one of the characters 8et/een the 8rackets? /hich can
include a dash1separated range of letters or num8ers.
Tr" out some of these file1matching metacharacters 8" first going to an empt"
)7
director" <such as the test director" descri8ed in the pre$ious section= and creating
some empt" filesK
$ touch apple banana 'rape 'raperuit watermelon
The touch command creates empt" files. The ne't fe/ commands sho/ "ou ho/ to
use shell metacharacters /ith the ls command to match filenames. Tr" the follo/ing
commands to see if "ou get the same responsesK
$ ls a-
apple
$ ls '-
grape
grape8rui*
$ ls '-t
grape8rui*
$ ls -e-
apple grape grape8rui* 2a*ermelon
$ ls -n-
banana 2a*ermelon
The first e'ample matches an" file that 8egins /ith an a <apple=. The ne't e'ample
matches an" files that 8egin /ith g <grape? grapefruit=. 6e't? files 8eginning /ith
g and ending in t are matched <grapefruit=. 6e't? an" file that contains an e in the
name is matched <apple? grape? grapefruit? /atermelon=. -inall"? an" file that
contains an n is matched <8anana? /atermelon=.
5ere are a fe/ e'amples of pattern matching /ith the Euestion mark <B=K
$ ls ....e
apple grape
$ ls '...e-
grape grape8rui*
The first e'ample matches an" fi$e1character file that ends in e <apple? grape=. The
second matches an" file that 8egins /ith g and has e as its fifth character <grape?
grapefruit=.
5ere are a couple of e'amples using 8races to do pattern matchingK
$ ls /abw0-
apple banana 2a*ermelon
$ ls /a'w0-/ne0
apple grape 2a*ermelon
9n the first e'ample? an" file 8eginning /ith a? 8? or / is matched. 9n the second? an"
file that 8egins /ith a? g? or / and also ends /ith either n or e is matched. You can
also include ranges /ithin 8rackets. -or e'ampleK
$ ls /a-'0-
apple banana grape grape8rui*
5ere? an" filenames 8eginning /ith a letter from a through g is matched.
)5
Using $ile'Redirection (etacharacters
Commands recei$e data from standard input and send it to standard output. Using
pipes <descri8ed earlier=? "ou can direct standard output from one command to the
standard input of another. .ith files? "ou can use less than <Z= and greater than <V=
signs to direct data to and from files. 5ere are the file1redirection charactersK
ZPirects the contents of a file to the command.
VPirects the output of a command to a file? deleting the e'isting file.
VVPirects the output of a command to a file? adding the output to the end
of the e'isting file.
5ere are some e'amples of command lines /here information is directed to and
from filesK
$ mail root 1 ~/2bashrc
$ man chmod | col -b 3 /tmp/chmod
$ echo 45 inished the pro&ect on $6date78 33 ~/pro&ects
9n the first e'ample? the contents of the .8ashrc file in the home director" are sent
in a mail message to the computerJs root user. The second command line formats
the chmod man page <using the man command=? remo$es e'tra 8ack spaces <col 1
8=? and sends the output to the file OtmpOchmod <erasing the pre$ious OtmpOchmod
file? if it e'ists=. The final command results in the follo/ing te'tJs 8eing added to the
userJs proNect fileK
9 finished the proNect on Sat Can ,5 )0K76K7( PST ,**6
Listing $iles
The ls <list= command lists files in the current director". The command ls has a $er"
large num8er of options? 8ut /hat "ou reall" need to kno/ is that ls 1l gi$es a RlongS
listing sho/ing the file siFes and permissions? and that the 1a option sho/s e$en
RhiddenS filesPthose /ith a dot at the start of their names. The shell e'pands the A
character to mean Ran" string of characters not starting /ith [.J.S <See the discussion
of /ildcards in the R#d$anced Shell -eaturesS section earlier in this chapter for more
information a8out ho/ and /h" this /orks.= Therefore? A.doc is interpreted as an"
filename ending /ith .doc that does not start /ith a dot and aA means Ran" filename
starting /ith the letter a.S -or e'ampleK
ls 1laP3i$es a long listing of all files in the current director" including RhiddenS
files /ith names staring /ith a dot
ls aAPLists all files in the current director" /hose names start /ith a
ls 1l A.docP3i$es a long listing of all files in the current director" /hose
names end /ith .doc
Copying $iles
The cp <cop"= command copies a file? files? or director" to another location. The
option 1R allo/s "ou to cop" directories recursi$el" <in general? 1R or 1r in commands
)6
often has the meaning of Rrecursi$eS=. 9f the last argument to the cp command
is a director"? the files mentioned /ill 8e copied into that director". 6ote that 8"
default? cp /ill Rclo88erS e'isting files? so in the second e'ample that follo/s? if there
is alread" a file called afile in the director" OhomeO8i8le? it /ill 8e o$er/ritten /ithout
asking for an" confirmation. Consider the follo/ing e'amplesK
cp afile afile.8akPCopies the file afile to a ne/ file afile.8ak.
cp afile OhomeO8i8leOPCopies the file afile from the current director" to the
director" OhomeO8i8leO.
cp A OtmpPCopies all nonhidden files in the current director" to OtmpO.
cp 1a docs docs.8akPRecursi$el" copies the director" docs 8eneath the current
director" to a ne/ director" docs.8ak? /hile preser$ing file attri8utes and
cop"ing all files including hidden files /hose names start /ith a dot. The 1a
option implies the 1R option? as a con$enience.
cp QiP+" default? if "ou cop" a file to a location /here a file of the same
name alread" e'ists? the old file /ill 8e silentl" o$er/ritten. The 1i option
makes the command interacti$e\ in other /ords it asks 8efore o$er/riting.
cp Q$P.ith the Q$ <$er8ose= option? the cp command /ill tell "ou /hat it is
doing. # great man" Linu' commands ha$e a Q$ option /ith the same meaning.
(oing and Renaming $iles
The m$ <mo$e= command has the meaning 8oth of Rmo$eS and of Rrename.S 9n the
first e'ample that follo/s? the file afile /ill 8e renamed to the name 8file. 9n the
second e'ample? the file afile in the current director" /ill 8e mo$ed to the director"
OtmpO.
m$ afile 8filePRenames the e'isting file afile /ith the ne/ name 8file
m$ afile OtmpP2o$es the file afile in the current director" to the director"
Otmp
Deleting $iles and Directories
The r m <remo$e= command ena8les "ou to delete files and directories. +e /arnedK
r m is a dangerous command. 9t doesnJt reall" offer "ou a second chance. .hen files
are deleted? the"Jre gone. You can use r m 1i as in the last e'ample that follo/s.
That at least gi$es "ou a second chance to think a8out it? 8ut as soon as "ou agree?
once again? the file is gone.
Some people like to create an alias <see Chapter )7= that makes the r m command
act like rm 1i. .e /ould ad$ise at least to 8e careful a8out thisK 9t /ill lull "ou into
a false sense of securit"? and /hen "ouJre /orking on a s"stem /here this change
has not 8een made? "ou ma" regret it.
oug 3/"n? a /ell1kno/n 9nternet personalit"? once said? RUni' /as ne$er designed
to keep people from doing stupid things 8ecause that polic" /ould also keep them
from doing cle$er things.S You can? of course? use r m to delete e$er" file on "our
s"stem as simpl" as thisK rm 1rf O. <You ha$e to 8e logged in as a user? such as the
)!
root user? /ho has the pri$ileges to do this? 8ut "ou get the idea.= Some 8etter
e'amples of using the rm command in dail" use areK
rm afilePRemo$es the file afile.
rm A PRemo$es all <nonhidden= files in the current director". The r m command
/ill not remo$e directories unless "ou also specif" the 1r <recursi$e=
option.
rm 1rf doomedPRemo$es the director" doomed and e$er"thing in it.
rm Qi aAPRemo$es all files /ith names 8eginning /ith a in the current director"?
asking for confirmation each time.
Changing Directories
You use the cd <change director"= command to change directoriesK
cd >PChanges to "our home director"
cd OtmpPChanges to the director" Otmp
4n most Linu' s"stems? "our prompt /ill tell "ou /hat director" "ouJre in
<depending on the setting "ouJ$e used for the PS) en$ironment $aria8le=.
5o/e$er\ if "ou e$er e'plicitl" need to kno/ /hat director" "ouJre in? "ou can use
the p/d command to identif" the /orking director" for the current process <process
/orking director"? hence p/d=.
(aking Directories
You can use the mkdir <make director"= command to make directories. -or e'ampleK
mkdir photosP2akes a director" called photos /ithin the current director".
mkdir 1p thisOthatOtheotherP2akes the nested su8directories named
/ithin the current director".
Remoing Directories
The command rmdir /ill remo$e a director" that is empt".
(aking Links to $iles or Directories
9n Linu'? "ou can use the ln <link= command to make links to a file or director". #
file can ha$e an" num8er of so1called RhardS links to it. &ffecti$el"? these are alternati$e
names for the file. So if "ou create a file called afile? and make a link to it called
8file? there are no/ t/o names for the same file. 9f "ou edit afile? the changes
"ouJ$e made /ill 8e in 8file. +ut if "ou delete afile? 8file /ill still e'ist\ it /ill disappear
onl" /hen there are no links left to it. 5ard links can 8e made onl" on the same
files"stemP"ou canJt create a hard link to a file on another partition 8ecause the
link operates at the files"stem le$el? referring to the actual files"stem data structure
that holds information a8out the file. You can create a hard link onl" to a file? not to
a director".
)%
You can also create a symbolic link to a file. # s"m8olic link is a special kind of file
that redirects an" usage of the link to the original file. This is some/hat similar to
the use of RshortcutsS in .indo/s. You can also create s"m8olic links to directories?
/hich can 8e $er" useful if "ou freEuentl" use a su8director" that is hidden
se$eral le$els deep 8elo/ "our home director". 9n the last e'ample that follo/s?
"ou /ill end up /ith a s"m8olic link called useful in the current director". Thus? the
command cd useful /ill ha$e the same effect as cd docsOlinu'OsuseOuseful.
ln afile 8fileP2akes a RhardS link to afile called 8file
ln 1s afile linkfileP2akes a s"m8olic link to afile called linkfile
ln 1s docsOlinu'OsuseOusefulP2akes a s"m8olic link to the named director"
in the current director"
Concatenating $iles
The command cat <concatenate= displa"s files to standard output. 9f "ou /ant to
$ie/ the contents of a short te't file? the easiest thing to do is to cat it? /hich sends
its contents to the shellJs standard output? /hich is the shell in /hich "ou t"ped the
cat command. 9f "ou cat t/o files? "ou /ill see the contents of each fl"ing past on
the screen. +ut if "ou /ant to com8ine those t/o files into one? all "ou need to do is
cat them and redirect the output to the cat command to a file using V.
Linu' has a sense of humor. The cat command displa"s files to standard output?
starting /ith the first line and ending /ith the last. The tac command <cat spelled
8ack/ard= displa"s files in re$erse order? 8eginning /ith the last line and ending
/ith the first. The command tac is amusingK Tr" itY
cat OetcOpass/dPPrints OetcOpass/d to the screen
cat afile 8filePPrints the contents of afile to the screen follo/ed 8" the contents
of 8file
cat afile 8file V cfilePCom8ines the contents of afile and 8file and /rites
them to a ne/ file? cfile
Vie&ing $iles &ith more and less
The more and less commands are kno/n as pagers 8ecause the" allo/ "ou to $ie/
the contents of a te't file one screen at a time and to page for/ard and 8ack/ard
through the file </ithout editing it=. The name of the more command is deri$ed from
the fact that it allo/s "ou to see a file one screen at a time? there8" seeing RmoreS of
it. The name of the less command comes from the fact that it originall" 8egan as an
open source $ersion of the more command <8efore more itself 8ecame an open
source command= and 8ecause it originall" did less than the more command <the
author had a sense of humor=. 6o/ada"s? the less command has man" added features?
including the fact that "ou can use ke"8oard shortcuts such as pressing the
letter b /hen $ie/ing a file to mo$e 8ack/ard through the file. The man page of less
lists all the other hot ke"s that can 8e used for na$igating through a file /hile reading
it using less. +oth more and less use the hot ke" q to e'it.
more OetcOpass/dPVie/s the contents of OetcOpass/d
)(
less OetcOpass/dPVie/s the contents of OetcOpass/d
Vie&ing the Start or !nd of $iles
The head and tail commands allo/ "ou to see a specified num8er of lines from the
top or 8ottom of a file. The tail command has the $er" useful feature that "ou can
use tail 1f to keep an e"e on a file as it gro/s. This is particularl" useful for /atching
/hat is 8eing /ritten to a log file /hile "ou make changes in the s"stem.
Consider the follo/ing e'amplesK
head 1n5 OetcOpass/dPPrints the first fi$e lines of the file OetcOpass/d to
the screen
tail 1n5 OetcOpass/dPPrints the last fi$e lines of OetcOpass/d to the
screen
tail 1f O$arOlogOmessagesPVie/s the last fe/ lines of O$arOlogO
messages and continues to displa" changes to the end of the file in real time
Searching $iles &ith grep
The grep <glo8al regular e'pression print= command is a $er" useful tool for finding
stuff in files. 9t can do much more than e$en the e'amples that follo/ this paragraph
indicate. +e"ond simpl" searching for te't? it can search for regular e'pressions.
9tJs a regular e'pression parser? and regular e'pressions are a su8Nect for a
8ook in themsel$es.
grep 8i8le OetcOe'portsPLooks for all lines in the file OetcOe'ports that
include the string bible
tail 1)** O$arOlogOapacheOaccess.logWgrep 7*7PLooks for the string 404?
the /e8 ser$erJs Rfile not foundS code? in the last hundred lines of the /e8
ser$er log
tail 1)** O$arOlogOapacheOaccess.logWgrep 1$ google8otPLooks in the last
)** lines of the /e8 ser$er log for lines that donJt indicate accesses 8" the
3oogle search ro8ot
grep 1$ ]M OetcOapache,Ohttpd.confPLooks for all lines that are not
commented out in the main #pache configuration file.
$inding $iles &ith find and locate
The find command searches the files"stem for files that match a specified pattern.
The locate command pro$ides a faster /a" of finding files 8ut depends on a
data8ase that it creates and refreshes at regular inter$als. The locate command is
fast and con$enient? 8ut the information it displa"s ma" not al/a"s 8e up1to1dateP
this depends on /hether its data8ase is up1to1date. To use the locate command?
"ou need to ha$e the package findutils1locate installed.
find is a po/erful command /ith man" options? including the a8ilit" to search for
files /ith date stamps in a particular range <useful for 8ackups= and to search for
,*
files /ith particular permissions? o/ners? and other attri8utes. The documentation
for find can 8e found in its info pagesK info find.
find .1name A.rpmP-inds RP2 packages in the current director"
find .Wgrep pageP-inds files in the current director" and its su8directories
/ith the string page in their names
locate tracerouteP-inds files /ith names including the string traceroute an"/here
on the s"stem.
#asic User and )roup Concepts
Linu' is a trul" multiuser operating s"stem. The concept of users and groups in
Linu' is inherited from the Uni' tradition? and among other things pro$ides a $er"
clear and precise distinction 8et/een /hat normal users can do and /hat a pri$ileged
user can do <such as the root user? the superuser and ultimate administrator
on a Linu' s"stem? /ho can do an"thing=. The fact that the s"stem of users and
groups and the associated s"stem of permissions is 8uilt into the s"stem at the
deepest le$el is one of the reasons /h" Linu' <and Uni' in general= is fundamentall"
secure in a /a" that 2icrosoft .indo/s is not. #lthough modern $ersions of
.indo/s ha$e a similar concept of users and groups? the associated concept of the
permissions /ith /hich a process can 8e run lea$es a lot to 8e desired. This is /h"
there are so man" .indo/s $ulnera8ilities that are 8ased on e'ploiting the scripting
capa8ilities of programs that are run /ith user pri$ileges 8ut that turn out to 8e
capa8le of su8$erting the s"stem.
9f "ouJre interested in the differences 8et/een the maNor operating s"stems? &ric
Ra"mond? noted open source guru and philosopher? offers some interesting
comparisons and discussion at ///.cat8.orgO>esrO/ritingsOtaoupO
htmlOch*0s*,.html.
&$er" Linu' s"stem has a num8er of users accountsK Some of these are human
users? and some of them are s"stem users? /hich are user identities that the s"stem
uses to perform certain tasks.
The users on a s"stem <pro$ided it does authentication locall"= are listed in the file
OetcOpass/d. Look at "our o/n entr" in OetcOpass/d\ it /ill look something like thisK
rogerK'K)***K)**KRoger .hittakerKOhomeOrogerKO8inO8ash
This sho/s? among other things? that the user /ith username roger has the real
name Roger .hittaker? that his home director" is OhomeOroger? and that his
default shell is O8inO8ash <the 8ash shell=.
There /ill almost certainl" also 8e an entr" for the s"stem user postfi'? looking
something like thisK
postfi'K'K5)K5)KPostfi' aemonKO$arOspoolOpostfi'KO8inOfalse
This is the postfi' daemon? /hich looks after mail. This user canJt log in 8ecause
its shell is O8inOfalse? 8ut its home director" is O$arOspoolOpostfi'? and it o/ns
the spool directories in /hich mail 8eing sent and deli$ered is held. The fact that
these directories are o/ned 8" the user postfi' rather than 8" root is a securit"
featurePit means that an" possi8le $ulnera8ilit" in postfi' is less likel" to lead to
,)
a su8$ersion of the /hole s"stem. Similar s"stem users e'ist for the /e8 ser$er
<the user ///run= and $arious other ser$ices. You /onJt often need to consider
these? 8ut it is important to understand that the" e'ist and that the correct o/nerships
of certain files and directories 8" these users is part of the o$erall securit"
model of the s"stem as a /hole.
&ach user 8elongs to one or more groups. The groups on the s"stem are listed in the
file OetcOgroups. To find out /hat groups "ou 8elong to? "ou can simpl" t"pe the
command &roups <alternati$el" look at the file OetcOgroup and look for "our username=.
+" default? on a SUS& s"stem? "ou /ill find that "ou 8elong to the group
users and also to a fe/ s"stem groups? including the groups dialout and audio. This
is to gi$e normal human users the right to use the modem and sound de$ices
</hich is arranged through file permissions as "ou shall see later in this chapter=.
Creating Users and )roups
The useradd command has options that allo/ "ou to specif" the groups to /hich
the ne/ user /ill 8elongK
useradd -c Guest User u 5555 g 500 G 501 m d /home/guest s /bin/bash p
password guest
9 /ouldnJt recommend to add the users directl" in the OetcOpass/d file unless "ou ha$e
some e'perience in Linu'. #ltough if "ou choose to do so please check the OetcOgroups
and OetcOshado/ files to 8e in order.
To delete a user the command is userdel
4ther useful commands areK groupadd? groupdel. 9 think itJs prett" o8$ious /hat these
commands do.
To $erif" user logged on the s"stem "ou can tr"
- last
.ou miht also want to see what commands li$e : who/ whoami, id do.
%orking &ith $ile *&nership and Permissions
The users and groups discussed in the pre$ious section are useful onl" 8ecause
each file on the s"stem is o/ned 8" a certain user and group and 8ecause the s"stem
of file permissions can 8e used to restrict or control access to the files 8ased
on the user /ho is tr"ing to access them.
The section that follo/s is a crash course in file permissions\ /e go into greater
detail in Chapter )0.
9f "ou look at a $ariet" of files and directories from across the s"stem and list them
/ith the ls 1l command? "ou can see different patterns of o/nership and permissions.
9n each case the output from the ls command is gi$ing "ou se$eral pieces of
informationK the permissions on the file e'pressed as a ten1place string? the num8er
of links to the file? the o/nership of the file <user and group=? the siFe of the file in
,,
8"tes? the modification time? and the filename. 4f the ten places in the permissions
string? the first differs from the othersK The last nine can 8e 8roken up into three
groups of three? representing /hat the user can do /ith the file? /hat mem8ers of
the group can do /ith the file? and /hat others can do /ith the file? respecti$el". 9n
most cases? these permissions are represented 8" the presence or a8sence of the
letters r <read=? / </rite=? and ' <e'ecute= in the three positions. SoK
r/' means permission to read? /rite? and e'ecute
r11 means permission to read 8ut not to /rite or e'ecute
r1' means permission to read and e'ecute 8ut not to /rite
Permission to /rite to a file includes the right to o$er/rite or delete it.
So for e'ampleK
ls -l screenshot1.png
-rw-r--r-- 1 roger users 432686 2004-0 5-17 20:33 screenshot1.png
This file can 8e read and /ritten 8" its o/ner <roger=? can 8e read 8" mem8ers of
the group users? and can 8e read 8" others.
ls -l /home/roger/afile
-r-------- 1 roger users 0 2004-0 5-17 21:07 afile
This file is not e'ecuta8le or /rita8le? and can 8e read onl" 8" its o/ner <roger=.
&$en roger /ould ha$e to change the permissions on this file to 8e a8le to /rite it.
ls -l /etc/passwd
-rw-r--r-- 1 root root 1598 2004-0 5-17 19:36 /etc/passwd
This is the pass/ord filePit is o/ned 8" root <and the group root to /hich onl"
root 8elongs=? is reada8le 8" an"one? 8ut is group /rita8le onl" 8" root.
ls -l /etc/shadow
-rw-r----- 1 root shadow 796 2004-0 5-17 19:36 /etc/shadow
This is the shado/ file? /hich holds the encr"pted pass/ords for users. 9t can 8e
read onl" 8" root and the s"stem group shado/ and can 8e /ritten onl" 8" root.
ls -l /usr/sbin/traceroute
-rwxr-xr-x 1 root root 14228 2004-0 4-06 02:27 /usr/sbin/traceroute
This is an e'ecuta8le file that can 8e read and e'ecuted 8" an"one? 8ut /ritten onl"
8" root.
ls -ld /home
drwxr-xr-x 6 root root 4096 2004-0 5-17 19:36 /home
This is a director" <note the use of the 1d flag to the ls command and the d in the
first position in the permissions=. 9t can 8e read and /ritten 8" the root user? and
read and e'ecuted 8" e$er"one. .hen used in director" permissions? the ' <e'ecuta8le=
permission translates into the a8ilit" to search or e'amine the director"P
"ou cannot e'ecute a director".
,0
ls -ld /root
drwx------ 18 root root 584 2004-0 5-14 08:29 /root
9n the preceding code? Oroot is the root userJs home director". 6o user apart from
root can access it in an" /a".
ls -l /bin/mount
-rwsr-xr-x 1 root root 87296 2004-0 4-06 14:17 /bin/mount
This is a more interesting e'ampleK notice the letter s /here until no/ /e sa/ an '.
This indicates that the file runs /ith the permissions of its o/ner <root= e$en /hen
it is e'ecuted 8" another userK Such a file is kno/n as 8eing suid root <set user 9
upon e'ecution=. There are a small num8er of e'ecuta8les on the s"stem that need
to ha$e these permissions. This num8er is kept as small as possi8le 8ecause there
is a potential for securit" pro8lems if e$er a /a" could 8e found to make such a file
perform a task other than /hat it /as /ritten for.
ls -l alink
lrwxrwxrwx 1 roger users 8 2004-0 5-17 22:19 alink -> file.bz2
6ote the lin the first positionK This is a s"m8olic link to file.8F, in the same director".
9umerical Permissions
4n man" occasions /hen permissions are discussed? "ou /ill see them 8eing
descri8ed in a three1digit numerical form <sometimes more digits for e'ceptional
cases=? such as 677. 9f a file has permissions 677? it has read and /rite permissions
for the o/ner and read permissions for the group and for others. This /orks
8ecause Linu' actuall" stores file permissions as seEuences of octal num8ers. This
is easiest to see 8" e'ampleK
421421421
-rw-r--r-- 644
-rwxr-xr-x 755
-r--r--r-- 444
-r-------- 400
So for each o/ner? group? and others? a read permission is represented 8" 7 <the
high 8it of a 018it octal $alue=? a /rite permission is represented 8" , <the middle
8it of a 018it octal $alue=? and an e'ecute permission is represented 8" ) <the lo/
8it of a 018it octal $alue=.
:han'in' Ownership and Permissions
You can change the o/nership of a file /ith the command cho/n. 9f "ou are logged
in as root? "ou can issue a command like thisK
cho/n harpoKusers file.t't
This changes the o/nership of the file file.t't to the user harpo and the group
users.
To change the o/nership of a director" and e$er"thing in it? "ou can use the command
/ith the 1R <recursi$e= option? like thisK
cho/n 1R harpoKusers OhomeOharpoOsome^director"O
,7
The chmod command is used to change file permissions. You can use chmod /ith
8oth the numerical and the r/' notation /e discussed earlier in the chapter. #gain?
this is easiest to follo/ 8" looking at a fe/ e'amplesK
chmod u:' afileP#dds e'ecute permissions for the o/ner of the file
chmod g:r afileP#dds read permissions for the group o/ning the file
chmod o1r afilePRemo$es read permission for others
chmod a:/ afileP#dds /rite permissions for all
chmod 677 afilePChanges the permissions to 677 <o/ner can read and
/rite\ group mem8ers and others can onl" read=
chmod !55 afilePChanges the permissions to !55 <o/ner can read? /rite
and e'ecute\ group mem8ers and others can onl" read and e'ecute=
9f "ou use chmod /ith the r/' notation? u means the o/ner? g means the group? o
means others? and a means all. 9n addition? : means add permissions? and 1 means
remo$e permissions? /hile r? /? and ' still represent read? /rite? and e'ecute?
respecti$el". .hen setting permissions? "ou can see the translation 8et/een the
t/o notations 8" e'ecuting the chmod command /ith the 1$ <$er8ose= option. -or
e'ampleK
#chmod -v 755 afile
mode of `afile changed to 0755 (rwxr-xr-x)
#chmod -v 200 afile
mode of `afile changed to 0200 (-w-------)
(ounting and Unmounting $ilesystems
2ounting a files"stem is /hat "ou need to do to make the files it contains a$aila8le?
and the mount command is /hat "ou use to do that. 9n Linu'? e$er"thing that can
8e seen is part of one 8ig tree of files and directories. Those that are on ph"sicall"
different partitions? disks? or remote machines are RgraftedS onto the s"stem at a
particular placePa mount point? /hich is usuall" an empt" director".
To find out /hat is currentl" mounted? simpl" t"pe the command mount on its o/n.
.e discuss the mount command further in Chapters )7 and ,,.
SUS& Linu' no/ uses su8fs to mount remo$a8le de$ices such as C1R42s and
flopp" disks. This means that "ou no longer ha$e to mount them e'plicitl"\ for
e'ample? if "ou simpl" change to the director" OmediaOcdrom? the contents of
the C /ill 8e $isi8le.
mount )(,.)6%.).)KOhomeO8i8leO OmntP2ounts the remote net/ork
files"stem OhomeO8i8leO from the machine )(,.)6%.).) on the mount point
Omnt
mount Ode$Ohda0 OusrOlocalP2ounts the disk partition Ode$Ohda0 on the
mount point OusrOlocal
umount OmntPUnmounts /hate$er is mounted on the mount point Omnt
TipK -or more interesting information see the manual for the OetcOfsta8.
#sk for details if something is Rfogg"S \=
,5
System information related commands
Here are some commands that helps you find some information about the system status.
(emory Reporting &ith the free Command
The free command sho/s 8reakdo/ns of the amounts and totals of free and used
memor"? including "our s/apfile usage. This command has se$eral command1line
options? 8ut is eas" to run and understand? for e'ampleK
; ree
*o*al used 8ree shared bu88ers cached
!em/ -014. .1004 .111 141-. -104 10444
6/L bu88ers/ 14455 154-5
;2ap/ -4.51 7454 .5-04
This sho/s a 0,2+ s"stem /ith 072+ s/ap space. 6otice that nearl" all the s"stem
memor" is 8eing used? and nearl" %2+ of s/ap space has 8een used.
+" default? the free command displa"s memor" in kilo8"tes? or )*,718"te notation. You
can use the 18 option to displa" "our memor" in 8"tes? or the 1m option to displa"
memor" in mega8"tes. You can also use the free command to constantl" monitor ho/
much memor" is 8eing used through the 1s command. This is hand" as a real1time
monitor if "ou specif" a .*)1second update and run the free command in a terminal
/indo/ under ;)).
Virtual (emory Reporting &ith the <mstat
Command
The $mstat is a general1purpose monitoring program? /hich offers real1time displa" of
not onl" memor" usage? $irtual memor" statistics? 8ut disk acti$it"? s"stem usage? and
central processing unit <CPU= acti$it". 9f "ou call $mstat /ithout an" command1line
options? "ouJll get a one1time snapshot? for e'ampleK
# <mstat
procs memor+ s2ap io s+s*em cpu
r b 2 s2pd 8ree bu88 cache si so bi bo in cs us s+ id
0 0 0 7451 1050 4.11 1055. 1 1 10 1 1-4 51 - . 45
9f "ou specif" a time inter$al in seconds on the $mstat command line? "ouJll get a
continuousl" scrolling report. 5a$ing a constant displa" of /hat is going on /ith "our
computer can help "ou if "ouJre tr"ing to find out /h" "our computer suddenl" slo/s
do/n? or /h" thereJs a lot of disk acti$it".
,6
Reclaiming (emory &ith the =ill Command
#s a desperate measure if "ou need to Euickl" reclaim memor"? "ou can stop running
programs 8" using the kill command. 9n order to kill a specific program? "ou should use
the ps command to list current running processes? and then stop an" or all of them /ith
the kill command. +" default? the ps command lists processes "ou o/n and /hich "ou
can kill? for e'ampleK
# ps
'$& %%@ ;%% %$!" ()!!N&
-57 p0 ; 0/00 bash
511 p0 ; 0/01 r3H*
51. p1 ; 0/00 (bash)
747 p0 ; 0/00 (appli3)
104 p0 ; 0/11 ne*scape inde30h*ml
110 p0 ; 0/00 (dns helper)
445 p0 < 0/00 ps
The ps command /ill list the currentl" running programs and the programJs process
num8er? or P9. You can use this information to kill a process /ith
# =ill -> $?>
#lso "ou should tr" out the top command and see /hat it sho/s.
Determining +o& Long Linu" +as #een Running
with the uptime and w :ommands
The uptime command sho/s "ou ho/ long Linu' has 8een running? ho/ man" users are
on? and three s"stem load a$erages? for e'ampleK
M uptime
),K77am up %K)6? 0 users? load a$erageK *.))? *.)*? *.*7
9f this is too little information for "ou? tr" the / command? /hich first sho/s the same
information as the uptime command? and then lists /hat currentl" logged1in users are
doingK
# w
1./41am up 1/.0, - users, load aHerage/ 0014, 0004, 0005
:;"< %%@ M<)! #)N$NO $&#" ,(': '(': PQ%
bball **+p0 localhos*0locald 4/47pm 15000s 00-1s 0015s bash
bball **+p. localhos*0locald 1./41am 0000s 0015s 0001s 2
The / command gi$es a little more information? and it is especiall" helpful if "ou /ould
like to monitor a 8us" s"stem /ith a num8er of users.
,!
Dernel $ersion and other related information <like hostname? ne/ mail? date? architecture=
can 8e found easil" /ithK
0uname a
Lots of hard/are information "ou can get /ithK
0dmidecode
4r
0lspci
1or the pci installed on the system or maybe for devices:
0lsdev
2is$ usae can be print with:
0df h
1ile dis$ usae can be also chec$ed with:
0du -h
Runleels
Linu' s"stems t"picall" use se$en different runle$els? /hich define /hat ser$ices should
8e running on the s"stem. The init process uses these runle$els to start and stop the
computer.
Runle$el * signifies that the computer has completel" shut do/n? and runle$el ) <or S=
represents single1user mode. Runle$els , through 5 are multiuser modes? and runle$el 6
is the _re8oot_ le$el. ifferent Linu' $ariations ma" not use all runle$els? 8ut t"picall"?
runle$el , is multiuser te't /ithout 6-S? runle$el 0 is multiuser te't? and runle$el 5 is
multiuser 3U9.
&ach runle$el has its o/n director" that defines /hich ser$ices start and in /hat order.
You@ll t"picall" find these directories at OetcOrc.dOrcB.d? /here B is a num8er from *
through 6 that corresponds to the runle$el. 9nside each director" are s"mlinks that point
to master initscripts found in OetcOinit.d or OetcOrc.dOinit.d.
These s"mlinks ha$e a special format. -or instance? S),s"slog is a s"mlink that points
to OetcOinit.dOs"slog? the initscript that handles the s"slog ser$ice. The S in the name tells
init to e'ecute the script /ith the _start_ parameter /hen starting that runle$el. Like/ise?
there ma" 8e another s"mlink pointing to the same initscript /ith the name D%%s"slog\
init /ould e'ecute this script /ith the _stop_ parameter /hen e'iting the runle$el.
,%
The num8er follo/ing the S or D determines the order in /hich init should start or stop
the ser$ice in relation to other ser$ices. You can see 8" the num8ers associated /ith the
s"slog ser$ice that s"slog starts fairl" earl" in the 8oot process? 8ut it stops late in the
shutdo/n process. This is so s"slog can log as much information a8out other ser$ices
starting and stopping as possi8le.
+ecause these are all s"mlinks? it@s eas" to manipulate the order in /hich init starts
ser$ices 8" naming s"mlinks accordingl". 9t@s also eas" to add in ne/ ser$ices 8"
s"mlinking to the master initscript.
To find out in /hich runle$el "ou are Nust t"peK
Mruneve
4r
Mwho /r
The configuration file for the runle$els is OetcOinitta8. See the manual page for this file.
Using the i Te"t !ditor
9tJs almost impossi8le to use Linu' for an" period of time and not need to use a te't
editor. This is 8ecause most Linu' configuration files are plain te't files that "ou
/ill almost certainl" need to change manuall" at some point.
9f "ou are using a 3U9? "ou can run gedit? /hich is fairl" intuiti$e for editing te't.
ThereJs also a simple te't editor "ou can run from the shell called nano. 5o/e$er?
most Linu' shell users /ill use either the $i or emacs command to edit te't files.
The ad$antage of $i or emacs o$er a graphical editor is that "ou can use it from an"
shell? a character terminal? or a character18ased connection o$er a net/ork <using
telnet or ssh? for e'ample=Pno 3U9 is reEuired. The" also each contain tons of
features? so "ou can continue to gro/ /ith them.
This section pro$ides a 8rief tutorial on the $i te't editor? /hich "ou can use to
manuall" edit a configuration file from an" shell. <9f $i doesnJt suit "ou? see the
R&'ploring 4ther Te't &ditorsS side8ar for other options.=
2ost often? "ou start $i to open a particular file. -or e'ample? to open a file called
OtmpOtest? t"pe the follo/ing commandK
$ <i /tmp/test
The 8o' at the top represents /here "our cursor is. The 8ottom line keeps "ou
informed a8out /hat is going on /ith "our editing <here "ou Nust opened a ne/
file=. 9n 8et/een? there are tildes <>= as filler 8ecause there is no te't in the file "et.
6o/ hereJs the intimidating partK There are no hints? menus? or icons to tell "ou
/hat to do. 4n top of that? "ou canJt Nust start t"ping. 9f "ou do? the computer is
,(
likel" to 8eep at "ou. #nd some people complain that Linu' isnJt friendl".
The first things "ou need to kno/ are the different operating modesK command and
input. The $i editor al/a"s starts in command mode. +efore "ou can add or change
te't in the file? "ou ha$e to t"pe a command <one or t/o letters and an optional
num8er= to tell $i /hat "ou /ant to do. Case is important? so use uppercase and
lo/ercase e'actl" as sho/n in the e'amplesY To get into input mode? t"pe an input
command. To start out? t"pe either of the follo/ingK
aPThe add command. #fter it? "ou can input te't that starts to the rigt of
the cursor.
iPThe insert command. #fter it? "ou can input te't that starts to the le!t of
the cursor.
%rrow ke0sP2o$e the cursor up? do/n? left? or right in the file one character
at a time. To mo$e left and right "ou can also use +ackspace and the space
8ar? respecti$el". 9f "ou prefer to keep "our fingers on the ke"8oard? mo$e the
cursor /ith h <left=? l <right=? N <do/n=? or k <up=.
wP2o$es the cursor to the 8eginning of the ne't /ord.
'P2o$es the cursor to the 8eginning of the pre$ious /ord.
1 "#ero$P2o$es the cursor to the 8eginning of the current line.
$P2o$es the cursor to the end of the current line.
HP2o$es the cursor to the upper1left corner of the screen <first line on the
screen=.
(P2o$es the cursor to the first character of the middle line on the screen.
LP2o$es the cursor to the lo/er1left corner of the screen <last line on the
screen=.
The onl" other editing "ou need to kno/ is ho/ to delete te't. 5ere are a fe/ $i
commands for deleting te'tK
'Peletes the character under the cursor.
;Peletes the character directl" 8efore the cursor.
d/Peletes from the current character to the end of the current /ord.
dLPeletes from the current character to the end of the current line.
d*Peletes from the pre$ious character to the 8eginning of the current line.
To /rap things up? use the follo/ing ke"strokes for sa$ing and Euitting the fileK
22PSa$e the current changes to the file and e'it from $i.
3wPSa$e the current file 8ut continue editing.
3w4PSame as UU.
34PGuit the current file. This /orks onl" if "ou donJt ha$e an" unsa$ed
changes.
345PGuit the current file and don%t sa$e the changes "ou Nust made to the file.
9f "ouJ$e reall" trashed the file 8" mistake? the KEY command is the 8est /a" to
e'it and a8andon "our changes. The file re$erts to the most recentl" changed $ersion.
So? if "ou Nust did a K/? "ou are stuck /ith the changes up to that point. 9f "ou
Nust /ant to undo a fe/ 8ad edits? press u to 8ack out of changes.
0*
You ha$e learned a fe/ $i editing commands. 9 descri8e more commands in the
follo/ing
sections. -irst? ho/e$er? here are a fe/ tips to smooth out "our first trials
/ith $iK
EscPRemem8er that &sc gets "ou 8ack to command mode. <9J$e /atched
people press e$er" ke" on the ke"8oard tr"ing to get out of a file.= &sc follo/ed
8" UU gets "ou out of command mode? sa$es the file? and e'its.
uPPress U to undo the pre$ious change "ou made. Continue to press u to
undo the change 8efore that? and the one 8efore that.
Ctr!6P9f "ou decide "ou didnJt /ant to undo the pre$ious command? use
Ctrl:R for Redo. &ssentiall"? this command undoes "our undo.
Caps LockP+e/are of hitting Caps Lock 8" mistake. &$er"thing "ou t"pe in
$i has a different meaning /hen the letters are capitaliFed. You donJt get a
/arning that "ou are t"ping capitalsPthings Nust start acting /eird.
Ctr!7PPage ahead? one page at a time.
Ctr!8PPage 8ack? one page at a time.
Ctr!"PPage ahead one1half page at a time.
Ctr!9PPage 8ack one1half page at a time.
:P3o to the last line of the file.
;:P3o to the first line of the file. <Use an" num8er to go to that line in the
file.=
To search for the ne't occurrence of te't in the file? use either the slash <O= or the
Euestion mark <B= character. -ollo/ the slash or Euestion mark /ith a pattern
<string of te't= to search for/ard or 8ack/ard? respecti$el"? for that pattern. .ithin
the search? "ou can also use metacharacters. 5ere are some e'amplesK
OhelloPSearches for/ard for the /ord hello.
Bgood8"ePSearches 8ack/ard for the /ord good8"e.
OThe.AfootPSearches for/ard for a line that has the /ord The in it and
also? after that at some point? the /ord foot.
BHpPIrintPSearches 8ack/ard for either print or Print. Remem8er that
case matters in Linu'? so make use of 8rackets to search for /ords that could
ha$e different capitaliFation.
You can precede most $i commands /ith num8ers to ha$e the command repeated
that num8er of times. This is a hand" /a" to deal /ith se$eral lines? /ords? or characters
at a time. 5ere are some e'amplesK
0d/Peletes the ne't three /ords.
5clPChanges the ne't fi$e letters <that is? remo$es the letters and enters
input mode=.
),NP2o$es do/n ), lines.
Putting a num8er in front of most commands Nust repeats those commands. #t this
point? "ou should 8e fairl" proficient at using the $i command.
0)
0,
Automated Tasks
9n Linu'? tasks can 8e configured to run automaticall" /ithin a specified period of time?
on a specified date? or /hen the s"stem load a$erage is 8elo/ a specified num8er. Red
5at &nterprise Linu' is pre1configured to run important s"stem tasks to keep the s"stem
updated. -or e'ample? the slocate data8ase used 8" the locate command is updated dail".
# s"stem administrator can use automated tasks to perform periodic 8ackups? monitor the
s"stem? run custom scripts? and more.
Red 5at &nterprise Linu' comes /ith se$eral automated tasks utilitiesK cron? at? and
8atch.
Cron
Cron is a daemon that can 8e used to schedule the e'ecution of recurring tasks according
to a com8ination of the time? da" of the month? month? da" of the /eek? and /eek.
Cron assumes that the s"stem is on continuousl". 9f the s"stem is not on /hen a task is
scheduled? it is not e'ecuted.
To use the cron ser$ice? the $i'ie1cron RP2 package must 8e installed and the crond
ser$ice must 8e running. To determine if the package is installed? use the rpm 1E $i'ie1
cron command. To determine if the ser$ice is running? use the command Os8inOser$ice
crond status.
Confi&urin& Cron *asks
The main configuration file for cron? OetcOcronta8? contains the follo/ing linesK
S5&LL`O8inO8ash
P#T5`Os8inKO8inKOusrOs8inKOusrO8in
2#9LT4`root
542&`O
M run1parts
*) A A A A root run1parts OetcOcron.hourl"
*, 7 A A A root run1parts OetcOcron.dail"
,, 7 A A * root run1parts OetcOcron./eekl"
7, 7 ) A A root run1parts OetcOcron.monthl"
The first four lines are $aria8les used to configure the en$ironment in /hich the cron
tasks are run. The S5&LL $aria8le tells the s"stem /hich shell en$ironment to use <in
this e'ample the 8ash shell=? /hile the P#T5 $aria8le defines the path used to e'ecute
00
commands. The output of the cron tasks are emailed to the username defined /ith the
2#9LT4 $aria8le. 9f the 2#9LT4 $aria8le is defined as an empt" string <2#9LT4`__=?
email is not sent. The 542& $aria8le can 8e used to set the home director" to use /hen
e'ecuting commands or scripts.
&ach line in the OetcOcronta8 file represents a task and has the follo/ing formatK minute
hour da" of month month da" of /eek command
-ields
M :1111111111111111 minute <* 1 5(=
M W :1111111111111 hour <* 1 ,0=
M W W :1111111111 da" of month <) 1 0)=
M W W W :1111111 month <) 1 ),=
M W W W W :1111 da" of /eek <* 1 6= <Sunda"`* or !=
M W W W W W
A A A A A command to 8e e'ecuted
minute P an" integer from * to 5(
hour P an" integer from * to ,0
da" of month P an" integer from ) to 0) <must 8e a $alid da" if a month is specified=
month P an" integer from ) to ), <or the short name of the month such as Nan or fe8=
da" of /eek P an" integer from * to !? /here * or ! represents Sunda" <or the short
name of the /eek such as sun or mon=
command P the command to e'ecute <the command can either 8e a command such as
ls Oproc VV OtmpOproc or the command to e'ecute a custom script=
-or an" of the a8o$e $alues? an asterisk <A= can 8e used to specif" all $alid $alues. -or
e'ample? an asterisk for the month $alue means e'ecute the command e$er" month
/ithin the constraints of the other $alues.
# h"phen <1= 8et/een integers specifies a range of integers. -or e'ample? )17 means the
integers )? ,? 0? and 7.
# list of $alues separated 8" commas <?= specifies a list. -or e'ample? 0? 7? 6? % indicates
those four specific integers.
The for/ard slash <O= can 8e used to specif" step $alues. The $alue of an integer can 8e
skipped /ithin a range 8" follo/ing the range /ith OZintegerV. -or e'ample? *15(O, can
8e used to define e$er" other minute in the minute field. Step $alues can also 8e used
07
/ith an asterisk. -or instance? the $alue AO0 can 8e used in the month field to run the task
e$er" third month.
#n" lines that 8egin /ith a hash mark <M= are comments and are not processed.
#s sho/n in the OetcOcronta8 file? the run1parts script e'ecutes the scripts in the
OetcOcron.hourl"O? OetcOcron.dail"O? OetcOcron./eekl"O? and OetcOcron.monthl"O directories on
an hourl"? dail"? /eekl"? or monthl" 8asis respecti$el". The files in these directories
should 8e shell scripts.
9f a cron task is reEuired to 8e e'ecuted on a schedule other than hourl"? dail"? /eekl"? or
monthl"? it can 8e added to the OetcOcron.dO director". #ll files in this director" use the
same s"nta' as OetcOcronta8.
M record the memor" usage of the s"stem e$er" monda"
M at 0K0*#2 in the file OtmpOmeminfo
0* 0 A A mon cat OprocOmeminfo VV OtmpOmeminfo
M run custom script the first da" of e$er" month at 7K)*#2
)* 7 ) A A OrootOscriptsO8ackup.sh
Cronta' Exampes
Users other than root can configure cron tasks 8" using the cronta8 utilit". #ll user1
defined cronta8s are stored in the O$arOspoolOcronO director" and are e'ecuted using the
usernames of the users that created them. To create a cronta8 as a user? login as that user
and t"pe the command cronta8 1e to edit the user@s cronta8 using the editor specified 8"
the V9SU#L or &9T4R en$ironment $aria8le. The file uses the same format as
OetcOcronta8. .hen the changes to the cronta8 are sa$ed? the cronta8 is stored according
to username and /ritten to the file O$arOspoolOcronOusername.
The cron daemon checks the OetcOcronta8 file? the OetcOcron.dO director"? and the
O$arOspoolOcronO director" e$er" minute for an" changes. 9f an" changes are found? the"
are loaded into memor". Thus? the daemon does not need to 8e restarted if a cronta8 file
is changed.
Controin& %ccess to Cron
The OetcOcron.allo/ and OetcOcron.den" files are used to restrict access to cron. The format
of 8oth access control files is one username on each line. .hitespace is not permitted in
either file. The cron daemon <crond= does not ha$e to 8e restarted if the access control
files are modified. The access control files are read each time a user tries to add or delete
a cron task.
The root user can al/a"s use cron? regardless of the usernames listed in the access control
files.
05
9f the file cron.allo/ e'ists? onl" users listed in it are allo/ed to use cron? and the
cron.den" file is ignored.
9f cron.allo/ does not e'ist? users listed in cron.den" are not allo/ed to use cron.
Startin& and Stoppin& the Service
To start the cron ser$ice? use the command Os8inOser$ice crond start. To stop the ser$ice?
use the command Os8inOser$ice crond stop. 9t is recommended that "ou start the ser$ice at
8oot time.
,$S
.hat is 6-SB
The 6et/ork -ile S"stem <6-S= /as de$eloped to allo/ machines to mount a disk
partition on a remote machine as if it /ere a local disk. 9t allo/s for fast? seamless
sharing of files across a net/ork.
9t also gi$es the potential for un/anted people to access "our hard dri$e o$er the net/ork
<and there8" possi8l" read "our email and delete all "our files as /ell as 8reak into "our
s"stem= if "ou set it up incorrectl".
There are other s"stems that pro$ide similar functionalit" to 6-S. Sam8a
<httpKOO///.sam8a.org= pro$ides file ser$ices to .indo/s clients. The #ndre/ -ile
S"stem? originall" de$eloped 8" 9+2 <httpKOO///.openafs.org= and no/ open1source?
pro$ides a file sharing mechanism /ith some additional securit" and performance
features. The Coda -ile S"stem <httpKOO///.coda.cs.cmu.eduO= com8ines file sharing
/ith a specific focus on disconnected clients. 2an" of the features of the #ndre/ and
Coda file s"stems are slated for inclusion in the ne't $ersion of 6-S <Version 7=
<httpKOO///.nfs$7.org=. The ad$antage of 6-S toda" is that it is mature? standard? /ell
understood? and supported ro8ustl" across a $ariet" of platforms.
Setting Up an ,$S Serer
<ntroduction to Server Setup
9t is assumed that "ou /ill 8e setting up 8oth a ser$er and a client. Setting up the ser$er
/ill 8e done in t/o stepsK Setting up the configuration files for 6-S? and then starting the
6-S ser$ices.
06
Settin& up the Confi&uration 7ies
There are three main configuration files "ou /ill need to edit to set up an 6-S ser$erK
OetcOe'ports? OetcOhosts.allo/? and OetcOhosts.den" . Strictl" speaking? "ou onl" need to
edit OetcOe'ports to get 6-S to /ork? 8ut "ou /ould 8e left /ith an e'tremel" insecure
setup. You ma" also need to edit "our startup scripts\
,etc,exports
This file contains a list of entries\ each entr" indicates a $olume that is shared and ho/ it
is shared. Check the man pages <man e'ports= for a complete description of all the setup
options for the file? although the description here /ill pro8a8l" satisf" most people@s
needs.
#n entr" in OetcOe'ports /ill t"picall" look like thisK
director" machine)<option))?option),= machine,<option,)?option,,=
/here
director"
the director" that "ou /ant to share. 9t ma" 8e an entire $olume though it need not 8e. 9f
"ou share a director"? then all directories under it /ithin the same file s"stem /ill 8e
shared as /ell.
machine) and machine,
client machines that /ill ha$e access to the director". The machines ma" 8e listed 8"
their 6S address or their 9P address <e.g.? machine.compan".com or )(,.)6%.*.% =.
Using 9P addresses is more relia8le and more secure. 6S names ma" not al/a"s resol$e
the ip address
option''
the option listing for each machine /ill descri8e /hat kind of access that machine /ill
ha$e. 9mportant options areK
roK The director" is shared read onl"\ the client machine /ill not 8e a8le to /rite it. This
is the default.
r/K The client machine /ill ha$e read and /rite access to the director".
no^root^sEuashK +" default? an" file reEuest made 8" user root on the client machine is
treated as if it is made 8" user no8od" on the ser$er. <&'actl" /hich U9 the reEuest is
mapped to depends on the U9 of user _no8od"_ on the ser$er? not the client.= 9f
no^root^sEuash is selected? then root on the client machine /ill ha$e the same le$el of
access to the files on the s"stem as root on the ser$er. This can ha$e serious securit"
implications? although it ma" 8e necessar" if "ou /ant to perform an" administrati$e
0!
/ork on the client machine that in$ol$es the e'ported directories. You should not specif"
this option /ithout a good reason.
no^su8tree^checkK 9f onl" part of a $olume is e'ported? a routine called su8tree checking
$erifies that a file that is reEuested from the client is in the appropriate part of the $olume.
9f the entire $olume is e'ported? disa8ling this check /ill speed up transfers.
s"ncK +" default? all 8ut the most recent $ersion <$ersion ).))= of the e'portfs command
/ill use as"nc 8eha$ior? telling a client machine that a file /rite is complete 1 that is? has
8een /ritten to sta8le storage 1 /hen 6-S has finished handing the /rite o$er to the
files"stem. This 8eha$ior ma" cause data corruption if the ser$er re8oots? and the s"nc
option pre$ents this.
Suppose /e ha$e t/o client machines? sla$e) and sla$e,? that ha$e 9P addresses
)(,.)6%.*.) and )(,.)6%.*.,? respecti$el". .e /ish to share our soft/are 8inaries and
home directories /ith these machines. # t"pical setup for OetcOe'ports might look like
thisK
OusrOlocal )(,.)6%.*.)<ro= )(,.)6%.*.,<ro=
Ohome )(,.)6%.*.)<r/= )(,.)6%.*.,<r/=
5ere /e are sharing OusrOlocal read1onl" to sla$e) and sla$e,? 8ecause it pro8a8l"
contains our soft/are and there ma" not 8e 8enefits to allo/ing sla$e) and sla$e, to
/rite to it that out/eigh securit" concerns. 4n the other hand? home directories need to
8e e'ported read1/rite if users are to sa$e their /ork on them.
9f "ou ha$e a large installation? "ou ma" find that "ou ha$e a 8unch of computers all on
the same local net/ork that reEuire access to "our ser$er. There are a fe/ /a"s of
simplif"ing references to large num8ers of machines. -irst? "ou can gi$e access to a range
of machines at once 8" specif"ing a net/ork and a netmask. -or e'ample? if "ou /anted
to allo/ access to all the machines /ith 9P addresses 8et/een )(,.)6%.*.* and
)(,.)6%.*.,55 then "ou could ha$e the entriesK
OusrOlocal )(,.)6%.*.*O,55.,55.,55.*<ro=
Ohome )(,.)6%.*.*O,55.,55.,55.*<r/=
See the 6et/orking14$er$ie/ 54.T4 for further information on ho/ netmasks? and
"ou ma" also /ish to look at the man pages for init and hosts.allo/.
Second? "ou can use 69S netgroups in "our entr". To specif" a netgroup in "our e'ports
file? simpl" prepend the name of the netgroup /ith an _a_. See the 69S 54.T4 for
details on ho/ netgroups /ork.
Third? "ou can use /ildcards such as A.foo.com or )(,.)6%. instead of hostnames. There
/ere pro8lems /ith /ildcard implementation in the ,., kernel series that /ere fi'ed in
kernel ,.,.)(.
0%
5o/e$er? "ou should keep in mind that an" of these simplifications could cause a
securit" risk if there are machines in "our netgroup or local net/ork that "ou do not trust
completel".
# fe/ cautions are in order a8out /hat cannot <or should not= 8e e'ported. -irst? if a
director" is e'ported? its parent and child directories cannot 8e e'ported if the" are in the
same files"stem. 5o/e$er? e'porting 8oth should not 8e necessar" 8ecause listing the
parent director" in the OetcOe'ports file /ill cause all underl"ing directories /ithin that
file s"stem to 8e e'ported.
Second? it is a poor idea to e'port a -#T or V-#T <i.e.? 2S14S or .indo/s (5O(%=
files"stem /ith 6-S. -#T is not designed for use on a multi1user machine? and as a
result? operations that depend on permissions /ill not /ork /ell. 2oreo$er? some of the
underl"ing files"stem design is reported to /ork poorl" /ith 6-S@s e'pectations.
Third? de$ice or other special files ma" not e'port correctl" to non1Linu' clients.
,etc,hosts#aow and ,etc,hosts#den0
These t/o files specif" /hich computers on the net/ork can use ser$ices on "our
machine. &ach line of the file contains a single entr" listing a ser$ice and a set of
machines. .hen the ser$er gets a reEuest from a machine? it does the follo/ingK
9t first checks hosts.allo/ to see if the machine matches a rule listed here. 9f it does? then
the machine is allo/ed access.
9f the machine does not match an entr" in hosts.allo/ the ser$er then checks hosts.den"
to see if the client matches a rule listed there. 9f it does then the machine is denied access.
9f the client matches no listings in either file? then it is allo/ed access.
9n addition to controlling access to ser$ices handled 8" inetd <such as telnet and -TP=?
this file can also control access to 6-S 8" restricting connections to the daemons that
pro$ide 6-S ser$ices. Restrictions are done on a per1ser$ice 8asis.
The first daemon to restrict access to is the portmapper. This daemon essentiall" Nust tells
reEuesting clients ho/ to find all the 6-S ser$ices on the s"stem. Restricting access to
the portmapper is the 8est defense against someone 8reaking into "our s"stem through
6-S 8ecause completel" unauthoriFed clients /on@t kno/ /here to find the 6-S
daemons. 5o/e$er? there are t/o things to /atch out for. -irst? restricting portmapper
isn@t enough if the intruder alread" kno/s for some reason ho/ to find those daemons.
#nd second? if "ou are running 69S? restricting portmapper /ill also restrict reEuests to
69S. That should usuall" 8e harmless since "ou usuall" /ant to restrict 6-S and 69S in a
similar /a"? 8ut Nust 8e cautioned. <Running 69S is generall" a good idea if "ou are
running 6-S? 8ecause the client machines need a /a" of kno/ing /ho o/ns /hat files
on the e'ported $olumes. 4f course there are other /a"s of doing this such as s"ncing
pass/ord files. See the 69S 54.T4 for information on setting up 69S.=
0(
9n general it is a good idea /ith 6-S <as /ith most internet ser$ices= to e'plicitl" den"
access to 9P addresses that "ou don@t need to allo/ access to.
The first step in doing this is to add the follo/ng entr" to OetcOhosts.den"K
portmapK#LL
Starting /ith nfs1utils *.,.*? "ou can 8e a 8it more careful 8" controlling access to
indi$idual daemons. 9t@s a good precaution since an intruder /ill often 8e a8le to /easel
around the portmapper. 9f "ou ha$e a ne/er $ersion of nfs1utils? add entries for each of
the 6-S daemonsK
lockdK#LL
mountdK#LL
rEuotadK#LL
statdK#LL
&$en if "ou ha$e an older $ersion of nfs1utils? adding these entries is at /orst harmless
<since the" /ill Nust 8e ignored= and at 8est /ill sa$e "ou some trou8le /hen "ou
upgrade. Some s"s admins choose to put the entr" #LLK#LL in the file OetcOhosts.den"?
/hich causes an" ser$ice that looks at these files to den" access to all hosts unless it is
e'plicitl" allo/ed. .hile this is more secure 8eha$ior? it ma" also get "ou in trou8le
/hen "ou are installing ne/ ser$ices? "ou forget "ou put it there? and "ou can@t figure out
for the life of "ou /h" the" /on@t /ork.
6e't? /e need to add an entr" to hosts.allo/ to gi$e an" hosts access that /e /ant to
ha$e access. <9f /e Nust lea$e the a8o$e lines in hosts.den" then no8od" /ill ha$e access
to 6-S.= &ntries in hosts.allo/ follo/ the formatK
ser$iceK host Hor net/orkOnetmaskI ? host Hor net/orkOnetmaskI
5ere? host is 9P address of a potential client\ it ma" 8e possi8le in some $ersions to use
the 6S name of the host? 8ut it is strongl" discouraged.
Suppose /e ha$e the setup a8o$e and /e Nust /ant to allo/ access to sla$e).foo.com and
sla$e,.foo.com? and suppose that the 9P addresses of these machines are )(,.)6%.*.) and
)(,.)6%.*.,? respecti$el". .e could add the follo/ing entr" to OetcOhosts.allo/K
portmapK )(,.)6%.*.) ? )(,.)6%.*.,
-or recent nfs1utils $ersions? /e /ould also add the follo/ing <again? these entries are
harmless e$en if the" are not supported=K
lockdK )(,.)6%.*.) ? )(,.)6%.*.,
rEuotadK )(,.)6%.*.) ? )(,.)6%.*.,
mountdK )(,.)6%.*.) ? )(,.)6%.*.,
statdK )(,.)6%.*.) ? )(,.)6%.*.,
7*
9f "ou intend to run 6-S on a large num8er of machines in a local net/ork?
OetcOhosts.allo/ also allo/s for net/orkOnetmask st"le entries in the same manner as
OetcOe'ports a8o$e.
)etting the serices Started
)re-re4uisites
The 6-S ser$er should no/ 8e configured and /e can start it running. -irst? "ou /ill
need to ha$e the appropriate packages installed. This consists mainl" of a ne/ enough
kernel and a ne/ enough $ersion of the nfs1utils package.
6e't? 8efore "ou can start 6-S? "ou /ill need to ha$e TCPO9P net/orking functioning
correctl" on "our machine. 9f "ou can use telnet? -TP? and so on? then chances are "our
TCP net/orking is fine.
That said? /ith most recent Linu' distri8utions "ou ma" 8e a8le to get 6-S up and
running simpl" 8" re8ooting "our machine? and the startup scripts should detect that "ou
ha$e set up "our OetcOe'ports file and /ill start up 6-S correctl". 9f this does not /ork? or
if "ou are not in a position to re8oot "our machine? then the follo/ing section /ill tell
"ou /hich daemons need to 8e started in order to run 6-S ser$ices. 9f for some reason
nfsd /as alread" running /hen "ou edited "our configuration files a8o$e? "ou /ill ha$e
to flush "our configuration\
Startin& the )ortmapper
6-S depends on the portmapper daemon? either called portmap or rpc.portmap. 9t /ill
need to 8e started first. 9t should 8e located in Os8in 8ut is sometimes in OusrOs8in. 2ost
recent Linu' distri8utions start this daemon in the 8oot scripts? 8ut it is /orth making
sure that it is running 8efore "ou 8egin /orking /ith 6-S <Nust t"pe ps au' W grep
portmap=.
The Daemons
6-S ser$ing is taken care of 8" fi$e daemonsK rpc.nfsd? /hich does most of the /ork\
rpc.lockd and rpc.statd? /hich handle file locking\ rpc.mountd? /hich handles the initial
mount reEuests? and rpc.rEuotad? /hich handles user file Euotas on e'ported $olumes.
Starting /ith ,.,.)%? lockd is called 8" nfsd upon demand? so "ou do not need to /orr"
a8out starting it "ourself. statd /ill need to 8e started separatel". 2ost recent Linu'
distri8utions /ill ha$e startup scripts for these daemons.
7)
The daemons are all part of the nfs1utils package? and ma" 8e either in the Os8in director"
or the OusrOs8in director".
9f "our distri8ution does not include them in the startup scripts? then "ou should add
them? configured to start in the follo/ing orderK
rpc.portmap
rpc.mountd? rpc.nfsd
rpc.statd? rpc.lockd <if necessar"=? and
rpc.rEuotad
The nfs1utils package has sample startup scripts for Red5at and e8ian. 9f "ou are using
a different distri8ution? in general "ou can Nust cop" the Red5at script? 8ut "ou /ill
pro8a8l" ha$e to take out the line that sa"sK
. ..Oinit.dOfunctions
to a$oid getting error messages.
Verifying that ,$S is running
To do this? Euer" the portmapper /ith the command
rpcinfo /p ocahost
to find out /hat ser$ices it is pro$iding. You should get something like thisK
program $ers proto port
)***** , tcp ))) portmapper
)***** , udp ))) portmapper
)***)) ) udp !7( rEuotad
)***)) , udp !7( rEuotad
)****5 ) udp !5( mountd
)****5 ) tcp !6) mountd
)****5 , udp !67 mountd
)****5 , tcp !66 mountd
)****5 0 udp !6( mountd
)****5 0 tcp !!) mountd
)****0 , udp ,*7( nfs
)****0 0 udp ,*7( nfs
0***)( ) tcp %0* amd
0***)( ) udp %0) amd
)***,7 ) udp (77 status
)***,7 ) tcp (76 status
)***,) ) udp )*7, nlockmgr
)***,) 0 udp )*7, nlockmgr
7,
)***,) 7 udp )*7, nlockmgr
)***,) ) tcp )6,( nlockmgr
)***,) 0 tcp )6,( nlockmgr
)***,) 7 tcp )6,( nlockmgr
This sa"s that /e ha$e 6-S $ersions , and 0? rpc.statd $ersion )? net/ork lock manager
<the ser$ice name for rpc.lockd= $ersions )? 0? and 7. There are also different ser$ice
listings depending on /hether 6-S is tra$elling o$er TCP or UP. Linu' s"stems use
UP 8" default unless TCP is e'plicitl" reEuested\ ho/e$er other 4Ses such as Solaris
default to TCP.
9f "ou do not at least see a line that sa"s portmapper? a line that sa"s nfs? and a line that
sa"s mountd then "ou /ill need to 8acktrack and tr" again to start up the daemons.
9f "ou do see these ser$ices listed? then "ou should 8e read" to set up 6-S clients to
access files from "our ser$er.
(akin& Chan&es to ,etc,exports ater on
9f "ou come 8ack and change "our OetcOe'ports file? the changes "ou make ma" not take
effect immediatel". You should run the command e'portfs 1ra to force nfsd to re1read
the OetcOe'ports file. 9f "ou can@t find the e'portfs command? then "ou can kill nfsd /ith
the 15UP flag <see the man pages for kill for details=.
9f that still doesn@t /ork? don@t forget to check hosts.allo/ to make sure "ou ha$en@t
forgotten to list an" ne/ client machines there. #lso check the host listings on an"
fire/alls "ou ma" ha$e set up.
Setting up an ,$S Client
(ounting Remote Directories
+efore 8eginning? "ou should dou8le1check to make sure "our mount program is ne/
enough <$ersion ,.)*m if "ou /ant to use Version 0 6-S=? and that the client machine
supports 6-S mounting? though most standard distri8utions do. 9f "ou are using a ,., or
later kernel /ith the Oproc files"stem "ou can check the latter 8" reading the file
OprocOfiles"stems and making sure there is a line containing nfs. 9f not? t"ping insmod nfs
ma" make it magicall" appear if 6-S has 8een compiled as a module\ other/ise? "ou /ill
need to 8uild <or do/nload= a kernel that has 6-S support 8uilt in. 9n general? kernels
that do not ha$e 6-S compiled in /ill gi$e a $er" specific error /hen the mount
command 8elo/ is run.
70
To 8egin using machine as an 6-S client? "ou /ill need the portmapper running on that
machine? and to use 6-S file locking? "ou /ill also need rpc.statd and rpc.lockd running
on 8oth the client and the ser$er. 2ost recent distri8utions start those ser$ices 8" default
at 8oot time.
.ith portmap? lockd? and statd running? "ou should no/ 8e a8le to mount the remote
director" from "our ser$er Nust the /a" "ou mount a local hard dri$e? /ith the mount
command. Continuing our e'ample from the pre$ious section? suppose our ser$er a8o$e
is called master.foo.com?and /e /ant to mount the Ohome director" on sla$e).foo.com.
Then? all /e ha$e to do? from the root prompt on sla$e).foo.com? is t"peK
= mount master#foo#com3,home ,mnt,home
and the director" Ohome on master /ill appear as the director" OmntOhome on sla$e).
<6ote that this assumes /e ha$e created the director" OmntOhome as an empt" mount
point 8eforehand.=
You can unmount the file s"stem 8" t"pingK
= umount ,mnt,home
Cust like "ou /ould for a local file s"stem.
)etting ,$S $ile Systems to -e (ounted at #oot Time
6-S file s"stems can 8e added to "our OetcOfsta8 file the same /a" local file s"stems can?
so that the" mount /hen "our s"stem starts up. The onl" difference is that the file s"stem
t"pe /ill 8e set to nfs and the dump and fsck order <the last t/o entries= /ill ha$e to 8e
set to Fero. So for our e'ample a8o$e? the entr" in OetcOfsta8 /ould look likeK
M de$ice mountpoint fs1t"pe options dump fsckorder
...
master.foo.comKOhome Omnt nfs r/ * *
...
See the man pages for fsta8 if "ou are unfamiliar /ith the s"nta' of this file. 9f "ou are
using an automounter such as amd or autofs? the options in the corresponding fields of
"our mount listings should look $er" similar if not identical.
#t this point "ou should ha$e 6-S /orking? though a fe/ t/eaks ma" still 8e necessar"
to get it to /ork /ell.
77
(ount *ptions
Soft versus Hard (ountin&
There are some options "ou should consider adding at once. The" go$ern the /a" the
6-S client handles a ser$er crash or net/ork outage. 4ne of the cool things a8out 6-S is
that it can handle this gracefull"? if "ou set up the clients right. There are t/o distinct
failure modesK
soft
9f a file reEuest fails? the 6-S client /ill report an error to the process on the client
machine reEuesting the file access. Some programs can handle this /ith composure? most
/on@t. .e do not recommend using this setting\ it is a recipe for corrupted files and lost
data. You should especiall" not use this for mail disks 111 if "ou $alue "our mail? that is.
hard
The program accessing a file on a 6-S mounted file s"stem /ill hang /hen the ser$er
crashes. The process cannot 8e interrupted or killed <e'cept 8" a _sure kill_= unless "ou
also specif" intr. .hen the 6-S ser$er is 8ack online the program /ill continue
undistur8ed from /here it /as. .e recommend using hard?intr on all 6-S mounted file
s"stems.
Picking up from the pre$ious e'ample? the fsta8 /ould no/ look likeK
M de$ice mountpoint fs1t"pe options dump fsckord
...
master.foo.comKOhome OmntOhome nfs r/?hard?intr * *
...
The rsiFe and /siFe mount options specif" the siFe of the chunks of data that the client
and ser$er pass 8ack and forth to each other.
The defaults ma" 8e too 8ig or to small\ there is no siFe that /orks /ell on all or most
setups. 4n the one hand? some com8inations of Linu' kernels and net/ork cards <largel"
on older machines= cannot handle 8locks that large. 4n the other hand? if the" can handle
larger 8locks? a 8igger siFe might 8e faster.
3etting the 8lock siFe right is an important factor in performance and is a must if "ou are
planning to use the 6-S ser$er in a production en$ironment.
75
,.S
6et/ork 9nformation Ser$ice? a ser$ice that pro$ides information? that has to 8e kno/n
throughout the net/ork? to all machines on the net/ork. There is support for 69S in
Linu'@s standard li8c li8rar"? /hich in the follo/ing te't is referred to as _traditional
69S_
The ne't four lines are Euoted from the Sun<tm= S"stem X 6et/ork #dministration
2anualK
_69S /as formerl" kno/n as Sun Yello/ Pages <YP= 8ut
the name Yello/ Pages<tm= is a registered trademark
in the United Dingdom of +ritish Telecom plc and ma"
not 8e used /ithout permission._
69S stands for 6et/ork 9nformation Ser$ice. 9ts purpose is to pro$ide information? that
has to 8e kno/n throughout the net/ork? to all machines on the net/ork. 9nformation
likel" to 8e distri8uted 8" 69S isK
login namesOpass/ordsOhome directories <OetcOpass/d=
group information <OetcOgroup=
9f? for e'ample? "our pass/ord entr" is recorded in the 69S pass/d data8ase? "ou /ill 8e
a8le to login on all machines on the net/ork /hich ha$e the 69S client programs
running.
Sun is a trademark of Sun 2icros"stems? 9nc. licensed to SunSoft? 9nc.
69S:
6et/ork 9nformation Ser$ice <Plus K1=? essentiall" 69S on steroids. 69S: is designed 8"
Sun 2icros"stems 9nc. as a replacement for 69S /ith 8etter securit" and 8etter handling
of ^large^ installations.
How ><S works
.ithin a net/ork there must 8e at least one machine acting as a 69S ser$er. You can
ha$e multiple 69S ser$ers? each ser$ing different 69S _domains_ 1 or "ou can ha$e
cooperating 69S ser$ers? /here one is the master 69S ser$er? and all the other are so1
called sla$e 69S ser$ers <for a certain 69S _domain_? that isY= 1 or "ou can ha$e a mi' of
them...
Sla$e ser$ers onl" ha$e copies of the 69S data8ases and recei$e these copies from the
master 69S ser$er /hene$er changes are made to the master@s data8ases. epending on
the num8er of machines in "our net/ork and the relia8ilit" of "our net/ork? "ou might
decide to install one or more sla$e ser$ers. .hene$er a 69S ser$er goes do/n or is too
76
slo/ in responding to reEuests? a 69S client connected to that ser$er /ill tr" to find one
that is up or faster.
69S data8ases are in so1called +2 format? deri$ed from #SC99 data8ases. -or e'ample?
the files OetcOpass/d and OetcOgroup can 8e directl" con$erted to +2 format using #SC991
to1+2 translation soft/are <maked'm? included /ith the ser$er soft/are=. The master
69S ser$er should ha$e 8oth? the #SC99 data8ases and the +2 data8ases.
Sla$e ser$ers /ill 8e notified of an" change to the 69S maps? <$ia the 0ppush program=?
and automaticall" retrie$e the necessar" changes in order to s"nchroniFe their data8ases.
69S clients do not need to do this since the" al/a"s talk to the 69S ser$er to read the
information stored in it@s +2 data8ases.
4ld "p8ind $ersions do a 8roadcast to find a running 69S ser$er. This is insecure? due the
fact that an"one ma" install a 69S ser$er and ans/er the 8roadcast Eueries. 6e/er
Versions of "p8ind <"p8ind10.0 or "p8ind1mt= are a8le to get the ser$er from a
configuration file 1 thus no need to 8roadcast.
How ><S! works
69S: is a ne/ $ersion of the net/ork information nameser$ice from Sun. The 8iggest
difference 8et/een 69S and 69S: is that 69S: has support for data encr"ption and
authentication o$er secure RPC.
The naming model of 69S: is 8ased upon a tree structure. &ach node in the tree
corresponds to an 69S: o8Nect? from /hich /e ha$e si' t"pesK director"? entr"? group?
link? ta8le and pri$ate.
The 69S: director" that forms the root of the 69S: namespace is called the root
director". There are t/o special 69S: directoriesK org^dir and groups^dir. The org^dir
director" consists of all administration ta8les? such as pass/d? hosts? and mail^aliases.
The groups^dir director" consists of 69S: group o8Nects /hich are used for access
control. The collection of org^dir? groups^dir and their parent director" is referred to as
an 69S: domain.
(anaging System Logs
The s"slogd utilit" logs $arious kinds of s"stem acti$it"? such as de8ugging output from
sendmail and /arnings printed 8" the kernel. s"slogd runs as a daemon and is usuall"
started in one of the rc files at 8oot time.
The file OetcOs"slog.conf is used to control /here s"slogd records information. Such a file
might look like the follo/ingK
A.info\A.notice O$arOlogOmessages
7!
mail.de8ug O$arOlogOmaillog
A./arn O$arOlogOs"slog
kern.emerg Ode$Oconsole
The first field of each line lists the kinds of messages that should 8e logged? and the
second field lists the location /here the" should 8e logged. The first field is of the
formatK
facilit".le$el H\ facilit".le$el b I
/here facilit" is the s"stem application or facilit" generating the message? and le$el is the
se$erit" of the message.
-or e'ample? facilit" can 8e mail <for the mail daemon=? kern <for the kernel=? user <for
user programs=? or auth <for authentication programs such as login or su=. #n asterisk in
this field specifies all facilities.
le$el can 8e <in increasing se$erit"=K de8ug? info? notice? /arning? err? crit? alert? or
emerg.
9n the pre$ious OetcOs"slog.conf? /e see that all messages of se$erit" info and notice are
logged to O$arOlogOmessages? all de8ug messages from the mail daemon are logged to
O$arOlogOmaillog? and all /arn messages are logged to O$arOlogOs"slog. #lso? an" emerg
/arnings from the kernel are sent to the console </hich is the current $irtual console? or
an 'term started /ith the 1C option=.
The messages logged 8" s"slogd usuall" include the date? an indication of /hat process
or facilit" deli$ered the message? and the message itself11all on one line. -or e'ample? a
kernel error message indicating a pro8lem /ith data on an e't,fs files"stem might appear
in the log files asK
ec ) ,)K*0K05 loomer kernelK &;T,1fs error <de$ice 0O,=K
e't,^check^8locks^8it mapK .rong free 8locks count in super 8lock?
stored ` ,!,*,? counted ` ,!%50
Similarl"? if an su to the root account succeeds? "ou might see a log message such asK
ec )) )5K0)K5) loomer suK md/ on Ode$Ott"p0
Log files can 8e important in tracking do/n s"stem pro8lems. 9f a log file gro/s too
large? "ou can delete it using rm\ it /ill 8e recreated /hen s"slogd starts up again.
Your s"stem pro8a8l" comes eEuipped /ith a running s"slogd and an OetcOs"slog.conf
that does the right thing. 5o/e$er? it@s important to kno/ /here "our log files are and
/hat programs the" represent. 9f "ou need to log man" messages <sa"? de8ugging
messages from the kernel? /hich can 8e $er" $er8ose= "ou can edit s"slog.conf and tell
s"slogd to reread its configuration file /ith the commandK
kill 15UP ccat O$arOrunOs"slog.pidc
6ote the use of 8ackEuotes to o8tain the process 9 of s"slogd? contained in
O$arOrunOs"slog.pid.
7%
4ther s"stem logs might 8e a$aila8le as /ell. These includeK
O$arOlogO/tmp
This file contains 8inar" data indicating the login times and duration for each user on the
s"stem\ it is used 8" the last command to generate a listing of user logins. The output of
last might look likeK
md/ tt"0 Sun ec )) )5K,5 still logged in
md/ tt"0 Sun ec )) )5K,7 1 )5K,5 <**K**=
md/ tt") Sun ec )) ))K76 still logged in
re8oot > Sun ec )) *6K76
# record is also logged in O$arOlogO/tmp /hen the s"stem is re8ooted.
O$arOrunOutmp
This is another 8inar" file that contains information on users currentl" logged into the
s"stem. Commands? such as /ho? /? and finger? use this file to produce information on
/ho is logged in. -or e'ample? the / command might printK
0K5%pm up 7K),? 5 users? load a$erageK *.*)? *.*,? *.**
User tt" logina idle CCPU PCPU /hat
md/ tt"p0 ))K76am )7 1
md/ tt"p, ))K76am ) /
md/ tt"p7 ))K76am kermit
md/ tt"p* ))K76am )7 8ash
.e see the login times for each user <in this case? one user logged in man" times=? as /ell
as the command currentl" 8eing used. The / manual page descri8es all of the fields
displa"ed.
O$arOlogOlastlog
This file is similar to /tmp? 8ut is used 8" different programs <such as finger? to
determine /hen a user /as last logged in.=
6ote that the format of the /tmp and utmp files differs from s"stem to s"stem. Some
programs ma" 8e compiled to e'pect one format and others another format. -or this
reason? commands that use the files ma" produce confusing or inaccurate information11
especiall" if the files 8ecome corrupted 8" a program that /rites information to them in
the /rong format.
7(
Logfiles can get Euite large? and if "ou do not ha$e the necessar" hard disk space? "ou
ha$e to do something a8out "our partitions 8eing filled too fast. 4f course? "ou can delete
the log files from time to time? 8ut "ou ma" not /ant to do this? since the log files also
contain information that can 8e $alua8le in crisis situations.
4ne option is to cop" the log files from time to time to another file and compress this file.
The log file itself starts at * again. 5ere is a short shell script that does this for the log file
O$arOlogOmessagesK
mv ,var,o&,messa&es ,var,o&,messa&es-'ackup
cp ,dev,nu ,var,o&,messa&es
C96"%*E?@date !ABmBdB0A@
mv ,var,o&,messa&es-'ackup ,var,o&,messa&es-$C96"%*E
&Cip ,var,o&,messa&es-$C96"%*E
-irst? /e mo$e the log file to a different name and then truncate the original file to *
8"tes 8" cop"ing to it from Ode$Onull. .e do this so that further logging can 8e done
/ithout pro8lems /hile the ne't steps are done. Then? /e compute a date string for the
current date that is used as a suffi' for the filename? rename the 8ackup file? and finall"
compress it /ith gFip.
You might /ant to run this small script from cron? 8ut as it is presented here? it should
not 8e run more than once a da"11other/ise the compressed 8ackup cop" /ill 8e
o$er/ritten? 8ecause the filename reflects the date 8ut not the time of da". 9f "ou /ant to
run this script more often? "ou must use additional num8ers to distinguish 8et/een the
$arious copies.
There are man" more impro$ements that could 8e made here. -or e'ample? "ou might
/ant to check the siFe of the log file first and onl" cop" and compress it if this siFe
e'ceeds a certain limit.
&$en though this is alread" an impro$ement? "our partition containing the log files /ill
e$entuall" get filled. You can sol$e this pro8lem 8" keeping onl" a certain num8er of
compressed log files <sa"? )*= around. .hen "ou ha$e created as man" log files as "ou
/ant to ha$e? "ou delete the oldest? and o$er/rite it /ith the ne't one to 8e copied. This
principle is also called log rotation. Some distri8utions ha$e scripts like sa$elog or
logrotate that can do this automaticall".
To finish this discussion? it should 8e noted that most recent distri8utions like SuS&?
e8ian? and Red 5at alread" ha$e 8uilt1in cron scripts that manage "our log files and are
much more sophisticated than the small one presented here.
Logrotate
5*
4ne of the most useful tools for log management in U69; is logrotate? /hich is part of
Nust a8out an" U69; distri8ution. 9n short? it lets "ou automaticall" split? compress and
delete log files according to se$eral policies ? and is usuall" emplo"ed to rotate common
files like O$arOlogOmessages? O$arOlogOsecure and O$arOlogOs"stem.log.
This 54.T4 sho/s "ou ho/ to set up log rotation not at a s"stem le$el? 8ut for a gi$en
user
7ies0stem La0out
Let@s assume "ou@re user? and that "ou@$e set up a daemon to run under "our username
and spit out the files to >userO$arOlogOdaemon.log. Your files"stem tree looks like thisK
OhomeOuser 11:11 etc Z1 /e@re going to put logrotate.conf here
W
:11 2ail
...
:11 $ar 11:11 li8 Z1 the logrotate status file goes here
W
:11 log Z1 the actual log files go here
Confi&urin& o&rotate
The first step is to create a configuration file. 5ere is a sample that rotates the log file on
a /eekl" 8asis? compresses the old log? creates a ne/ Fero18"te file and mails us a short
reportK
$ cat D,etc,o&rotate#conf
M see _man logrotate_ for details
M rotate log files /eekl"
/eekl"
M keep 7 /eeks /orth of 8acklogs
rotate 7
M create ne/ <empt"= log files after rotating old ones
M <this is the default? and can 8e o$erriden for each log file=
create
M uncomment this if "ou /ant "our log files compressed
compress
OhomeOuserO$arOlogOdaemon.log d
create
mail useralocalhost
e
You can? of course? check out man logrotate and add more options <or more files /ith
different options=.
5)
:ettin& it to 6un
2aking logrotate actuall" /ork? ho/e$er? reEuires in$oking it from cron. To do that? add
it to "our cronta8 specif"ing the status file /ith 1s and the configuration file "ou createdK
$ cronta' /
* * A A A OusrOs8inOlogrotate 1s OhomeOuserO$arOli8Ologrotate.status f
OhomeOuserOetcOlogrotate.conf V Ode$Onull ,VX)
<Take care 1 some s"stems do not allo/ _f_ to skip to the ne't line? /hich means "ou
must enter the logrotate in$ocation in a single line=
The a8o$e in$okes logrotate at midnight e$er" da"? dumping 8oth standard output and
standard error to Ode$Onull. 9t /ill then look at its status file and decide /hether or not it is
time to actuall" rotate the log files.
The difference -et&een hard and soft links
The data part is associated /ith something called an @inode@. The inode carries the map of
/here the data is? the file permissions? etc. for the data.
.111111111111111V Y data Y Y data Y etc
O :111111: Y111111:
Y perm8its? etc Y data addresses Y
:111111111111inode111111111111111:
The filename part carries a name and an associated inode num8er.
.11111111111111V Y perm8its? etc Y addresses Y
O :111111111inode1111111111111:
Y filename Y inode M Y
:11111111111111111111:
2ore than one filename can reference the same inode num8er\ these files are said to 8e
@hard linked@ together.
Y filename Y inode M Y
:11111111111111111111:
f
V11111111111111V Y perm8its? etc Y addresses Y
O :111111111inode1111111111111:
Y othername Y inode M Y
5,
:111111111111111111111:
4n the other hand? there@s a special file t"pe /hose data part carries a path to another file.
Since it is a special file? the 4S recogniFes the data as a path? and redirects opens? reads?
and /rites so that? instead of accessing the data /ithin the special file? the" access the
data in the file named 8" the data in the special file. This special file is called a @soft link@
or a @s"m8olic link@ <aka a @s"mlink@=.
Y filename Y inode M Y
:11111111111111111111:
f
.1111111V Y perm8its? etc Y addresses Y
:111111111inode1111111111111:
O
O
O
.1111111111111111111111111111111111111111111111@
<
@11V Y_OpathOtoOsomeOotherOfile_Y
:111111111data1111111111111:
O e
.> > > > > > > e11 <redirected at open<= time=
< e
@>>V Y filename Y inode M Y
:11111111111111111111:
f
@111111111111V Y perm8its? etc Y addresses Y
:111111111inode1111111111111:
O
O
.1111111111111111111111111111111111111111111111111111@
<
@1V Y data Y Y data Y etc.
:111111: :111111:
6o/? the filename part of the file is stored in a special file of its o/n along /ith the
filename parts of other files\ this special file is called a director". The director"? as a file?
is Nust an arra" of filename parts of other files.
.hen a director" is 8uilt? it is initiall" populated /ith the filename parts of t/o special
filesK the @.@ and @..@ files. The filename part for the @.@ file is populated /ith the inodeM of
the director" file in /hich the entr" has 8een made\ @.@ is a hardlink to the file that
implements the current director".
50
The filename part for the @..@ file is populated /ith the inodeM of the director" file that
contains the filename part of the current director" file. @..@ is a hardlink to the file that
implements the immediate parent of the current director".
The @ln@ command kno/s ho/ to 8uild hardlinks and softlinks\ the @mkdir@ command
kno/s ho/ to 8uild directories <the 4S takes care of the a8o$e hardlinks=.
There are restrictions on /hat can 8e hardlinked <8oth links must reside on the same
files"stem? the source file must e'ist? etc.= that are not applica8le to softlinks <source and
target can 8e on seperate file s"stems? source does not ha$e to e'ist? etc.=. 4T45?
softlinks ha$e other restrictions not shared 8" hardlinks <additional 9O4 necessar" to
complete file access? additional storage taken up 8" softlink file@s data? etc.=
9n other /ords? there@s tradeoffs /ith each.
6o/? let@s demonstrate some of this...
ln in action
Let@s start off /ith an empt" director"? and create a file in it
D,director0 $ s -ia
total 0
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K)6 .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)6 ..
D,director0 $ echo A*his is a fieA -'asic#fie
D,director0 $ s -ia
total 7
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K)! .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)6 ..
!07!% 1r/1r11r11 ) lpitcher users )5 2ar )) ,*K)! 8asic.file
D,director0 $ cat 'asic#fie
This is a file
6o/? let@s make a hardlink to the file
D,director0 $ n 'asic#fie hardink#fie
D,director0 $ s -ia
total 5
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K,* .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)% ..
!07!% 1r/1r11r11 , lpitcher users )5 2ar )) ,*K)! 8asic.file
!07!% 1r/1r11r11 , lpitcher users )5 2ar )) ,*K)! hardlink.file
D,director0 $ cat hardink#fie
57
This is a file
.e see thatK
hardlink.file shares the same inode <!07!%= as 8asic.file
hardlink.file shares the same data as 8asic.file
9f /e change the permissions on 8asic.fileK
D,director0 $ chmod a!w 'asic#fie
D,director0 $ s -ia
total 5
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K,* .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)% ..
!07!% 1r/1r/1r/1 , lpitcher users )5 2ar )) ,*K)! 8asic.file
!07!% 1r/1r/1r/1 , lpitcher users )5 2ar )) ,*K)! hardlink.file
then the same permissions change on hardlink.file.
The t/o files <8asic.file and hardlink.file= share the same inode and data? 8ut ha$e
different file names.
Let@s no/ make a softlink to the original fileK
D,director0 $ n -s 'asic#fie softink#fie
D,director0 $ s -ia
total 5
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K,7 .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)% ..
!07!% 1r/1r/1r/1 , lpitcher users )5 2ar )) ,*K)! 8asic.file
!07!% 1r/1r/1r/1 , lpitcher users )5 2ar )) ,*K)! hardlink.file
!07!( lr/'r/'r/' ) lpitcher users )* 2ar )) ,*K,7 softlink.file 1V 8asic.file
D,director0 $ cat softink#fie
This is a file
5ere? /e see that although softlink.file accesses the same data as 8asic.file and
hardlink.file? it does not share the same inode <!07!( $s !07!%=? nor does it e'hi8it the
same file permissions. 9t does sho/ a ne/ permission 8itK the @l@ <softlink= 8it.
9f /e delete 8asic.fileK
D,director0 $ rm 'asic#fie
D,director0 $ s -ia
total 7
!07!! dr/'r1'r1' , lpitcher users )*,7 2ar )) ,*K,! .
()%*7 dr/'r1'r1' ,( lpitcher users ,*7% 2ar )) ,*K)% ..
55
!07!% 1r/1r/1r/1 ) lpitcher users )5 2ar )) ,*K)! hardlink.file
!07!( lr/'r/'r/' ) lpitcher users )* 2ar )) ,*K,7 softlink.file 1V 8asic.file
then /e lose the a8ilit" to access the linked data through the softlinkK
D,director0 $ cat softink#fie
catK softlink.fileK 6o such file or director"
5o/e$er? /e still ha$e access to the original data through the hardlinkK
D,director0 $ cat hardink#fie
This is a file
You /ill notice that /hen /e deleted the original file? the hardlink didn@t $anish.
Similarl"? if /e had deleted the softlink? the original file /ouldn@t ha$e $anished.
# further note /ith respect to hardlink files
.hen deleting files? the data part isn@t disposed of until all the filename parts ha$e 8een
deleted. There@s a count in the inode that indicates ho/ man" filenames point to this file?
and that count is decremented 8" ) each time one of those filenames is deleted. .hen the
count makes it to Fero? the inode and its associated data are deleted.
+" the /a"? the count also reflects ho/ man" times the file has 8een opened /ithout
8eing closed <in other /ords? ho/ man" references to the file are still acti$e=. This has
some ramifications /hich aren@t o8$ious at firstK "ou can delete a file so that no
_filename_ part points to the inode? /ithout releasing the space for the data part of the
file? 8ecause the file is still open.
5a$e "ou e$er found "ourself in this positionK "ou notice that O$arOlogOmessages <or some
other s"slog1o/ned file= has gro/n too 8ig? and "ou
rm ,var,o&,messa&es
touch ,var,o&,messa&es
to reclaim the space? 8ut the used space doesn@t reappearB This is 8ecause? although
"ou@$e deleted the filename part? there@s a process that@s got the data part open still
<s"slogd=? and the 4S /on@t release the space for the data until the process closes it. 9n
order to complete "our space reclamation? "ou ha$e to
ki -S<:H9) @cat ,var,run,s0so&d#pid@
to get s"slogd to close and reopen the file.
56
$ile Compression and Archiing
Sometimes it is useful to store a group of files in one file so that the" can 8e 8acked up?
easil" transferred to another director"? or e$en transferred to a different computer. 9t is
also sometimes useful to compress files into one file so that the" use less disk space and
do/nload faster $ia the 9nternet.
9t is important to understand the distinction 8et/een an archi$e file and a compressed
file. #n archi$e file is a collection of files and directories that are stored in one file. The
archi$e file is not compressed P it uses the same amount of disk space as all the
indi$idual files and directories com8ined. # compressed file is a collection of files and
directories that are stored in one file and stored in a /a" that uses less disk space than all
the indi$idual files and directories com8ined. 9f "ou do not ha$e enough disk space on
"our computer? "ou can compress files that "ou do not use $er" often or files that "ou
/ant to sa$e 8ut do not use an"more. You can e$en create an archi$e file and then
compress it to sa$e disk space.
Compressin& 7ies at the She )rompt
Compressed files use less disk space and do/nload faster than large? uncompressed files.
9n Red 5at Linu' "ou can compress files /ith the compression tools gFip? 8Fip,? or Fip.
The 8Fip, compression tool is recommended 8ecause it pro$ides the most compression
and is found on most U69;1like operating s"stems. The gFip compression tool can also
8e found on most U69;1like operating s"stems. 9f "ou need to transfer files 8et/een
Linu' and other operating s"stem such as 2S .indo/s? "ou should use Fip 8ecause it is
more compati8le /ith the compression utilities on .indo/s.
Compression *oo 7ie Extension 9ncompression *oo
3Fip .gF gunFip
8Fip, .8F, 8unFip,
Fip .Fip unFip
+" con$ention? files compressed /ith gFip are gi$en the e'tension .gF? files compressed
/ith 8Fip, are gi$en the e'tension .8F,? and files compressed /ith Fip are gi$en the
e'tension .Fip.
-iles compressed /ith gFip are uncompressed /ith gunFip? files compressed /ith 8Fip,
are uncompressed /ith 8unFip,? and files compressed /ith Fip are uncompressed /ith
unFip.
5!
8CipE and 8unCipE
To use 8Fip, to compress a file? t"pe the follo/ing command at a shell promptK 8Fip,
filename
The file /ill 8e compressed and sa$ed as filename.8F,.
To e'pand the compressed file? t"pe the follo/ing commandK8unFip, filename.8F, The
filename.8F, is deleted and replaced /ith filename.
You can use 8Fip, to compress multiple files and directories at the same time 8" listing
them /ith a space 8et/een each oneK8Fip, filename.8F, file) file, file0 OusrO/orkOschool
The a8o$e command compresses file)? file,? file0? and the contents of the
OusrO/orkOschool director" <assuming this director" e'ists= and places them in a file
named filename.8F,.
-or more information? t"pe man 8Fip, and man 8unFip, at a shell prompt to read the man
pages for 8Fip, and 8unFip,.
:Cip and :unCip
To use gFip to compress a file? t"pe the follo/ing command at a shell promptK gFip
filename. The file /ill 8e compressed and sa$ed as filename.gF.
To e'pand the compressed file? t"pe the follo/ing commandKgunFip filename.gF
The filename.gF is deleted and replaced /ith filename.
You can use gFip to compress multiple files and directories at the same time 8" listing
them /ith a space 8et/een each oneKgFip 1r filename.gF file) file, file0 OusrO/orkOschool
The a8o$e command compresses file)? file,? file0? and the contents of the
OusrO/orkOschool director" <assuming this director" e'ists= and places them in a file
named filename.gF.
-or more information? t"pe man gFip and man gunFip at a shell prompt to read the man
pages for gFip and gunFip.
2ip and 9nCip
To compress a file /ith Fip? t"pe the follo/ing commandKFip 1r filename.Fip filesdir
9n this e'ample? filename.Fip represents the file "ou are creating and filesdir represents
the director" "ou /ant to put in the ne/ Fip file. The 1r option specifies that "ou /ant to
include all files contained in the filesdir director" recursi$el".
To e'tract the contents of a Fip file? t"pe the follo/ing commandKunFip filename.Fip
You can use Fip to compress multiple files and directories at the same time 8" listing
them /ith a space 8et/een each oneK Fip 1r filename.Fip file) file, file0 OusrO/orkOschool
5%
The a8o$e command compresses file)? file,? file0? and the contents of the
OusrO/orkOschool director" <assuming this director" e'ists= and places them in a file
named filename.Fip.
-or more information? t"pe man Fip and man unFip at a shell prompt to read the man
pages for Fip and unFip.
%rchivin& 7ies at the She )rompt
# tar file is a collection of se$eral files andOor directories in one file. This is a good /a"
to create 8ackups and archi$es.
Some of the options used /ith the tar areK
1c P create a ne/ archi$e.
1f P /hen used /ith the 1c option? use the filename specified for the creation of the tar
file\ /hen used /ith the 1' option? unarchi$e the specified file.
1t P sho/ the list of files in the tar file.
1$ P sho/ the progress of the files 8eing archi$ed.
1' P e'tract files from an archi$e.
1F P compress the tar file /ith gFip.
1N P compress the tar file /ith 8Fip,.
To create a tar file? t"peKtar 1c$f filename.tar director"Ofile
9n this e'ample? filename.tar represents the file "ou are creating and director"Ofile
represents the director" and file "ou /ant to put in the archi$ed file.
You can tar multiple files and directories at the same time 8" listing them /ith a space
8et/een each oneK tar 1c$f filename.tar OhomeOmineO/ork OhomeOmineOschool
The a8o$e command places all the files in the /ork and the school su8directories of
OhomeOmine in a ne/ file called filename.tar in the current director".
To list the contents of a tar file? t"peKtar 1t$f filename.tar
To e'tract the contents of a tar file? t"peK tar 1'$f filename.tar
This command does not remo$e the tar file? 8ut it places copies of its unarchi$ed contents
in the current /orking director"? preser$ing an" director" structure that the archi$e file
used. -or e'ample? if the tarfile contains a file called 8ar.t't /ithin a director" called
fooO? then e'tracting the archi$e file /ill result in the creation of the director" fooO in "our
current /orking director" /ith the file 8ar.t't inside of it.
5(
Remem8er? the tar command does not compress the files 8" default. To create a tarred
and 8Fipped compressed file? use the 1N optionK tar 1cN$f filename.t8F file
tar files compressed /ith 8Fip, are con$entionall" gi$en the e'tension .t8F\ ho/e$er?
sometimes users archi$e their files using the tar.8F, e'tension.
The a8o$e command creates an archi$e file and then compresses it as the file
filename.t8F. 9f "ou uncompress the filename.t8F file /ith the 8unFip, command? the
filename.t8F file is remo$ed and replaced /ith filename.tar.
You can also e'pand and unarchi$e a 8Fip tar file in one commandKtar 1'N$f filename.t8F
To create a tarred and gFipped compressed file? use the 1F optionK tar 1cF$f filename.tgF
file
tar files compressed /ith gFip are con$entionall" gi$en the e'tension .tgF.
This command creates the archi$e file filename.tar and then compresses it as the file
filename.tgF. <The file filename.tar is not sa$ed.= 9f "ou uncompress the filename.tgF file
/ith the gunFip command? the filename.tgF file is remo$ed and replaced /ith
filename.tar.
You can e'pand a gFip tar file in one commandKtar 1'F$f filename.tgF
T"pe the command man tar for more information a8out the tar command.
Package (anagement &ith RP(
The Red 5at Package 2anager <RP2= is an open packaging s"stem? a$aila8le for an"one
to use? /hich runs on Red 5at Linu' as /ell as other Linu' and U69; s"stems. Red 5at?
9nc. encourages other $endors to use RP2 for their o/n products. RP2 is distri8uta8le
under the terms of the 3PL.
-or the end user? RP2 makes s"stem updates eas". 9nstalling? uninstalling? and upgrading
RP2 packages can 8e accomplished /ith short commands. RP2 maintains a data8ase of
installed packages and their files? so "ou can in$oke po/erful Eueries and $erifications on
"our s"stem. 9f "ou prefer a graphical interface? "ou can use 3nome1RP2 to perform
man" RP2 commands.
6*
uring upgrades? RP2 handles configuration files carefull"? so that "ou ne$er lose "our
customiFations P something that "ou /ill not accomplish /ith regular .tar.gF files.
-or the de$eloper? RP2 allo/s "ou to take soft/are source code and package it into
source and 8inar" packages for end users. This process is Euite simple and is dri$en from
a single file and optional patches that "ou create. This clear delineation of _pristine_
sources and "our patches and 8uild instructions eases the maintenance of the package as
ne/ $ersions of the soft/are are released.
Run RP2 Commands as Root
+ecause RP2 makes changes to "our s"stem? "ou must 8e root in order to install?
remo$e? or upgrade an RP2 package.
6)( "esi&n :oas
9n order to understand ho/ to use RP2? it can 8e helpful to understand RP2@s design
goalsK
Upgrada8ilit"
Using RP2? "ou can upgrade indi$idual components of "our s"stem /ithout completel"
reinstalling. .hen "ou get a ne/ release of an operating s"stem 8ased on RP2 <such as
Red 5at Linu'=? "ou don@t need to reinstall on "our machine <as "ou do /ith operating
s"stems 8ased on other packaging s"stems=. RP2 allo/s intelligent? full"1automated? in1
place upgrades of "our s"stem. Configuration files in packages are preser$ed across
upgrades? so "ou /on@t lose "our customiFations. There are no special upgrade files need
to upgrade a package 8ecause the same RP2 file is used to install and upgrade the
package on "our s"stem.
Po/erful Guer"ing
RP2 is designed to pro$ide po/erful Euer"ing options. You can do searches through
"our entire data8ase for packages or Nust for certain files. You can also easil" find out
/hat package a file 8elongs to and from /here the package came. The files an RP2
package contains are in a compressed archi$e? /ith a custom 8inar" header containing
useful information a8out the package and its contents? allo/ing "ou to Euer" indi$idual
packages Euickl" and easil".
S"stem Verification
#nother po/erful feature is the a8ilit" to $erif" packages. 9f "ou are /orried that "ou
deleted an important file for some package? simpl" $erif" the package. You /ill 8e
notified of an" anomalies. #t that point? "ou can reinstall the package if necessar". #n"
configuration files that "ou modified are preser$ed during reinstallation.
6)
Pristine Sources
# crucial design goal /as to allo/ the use of _pristine_ soft/are sources? as distri8uted
8" the original authors of the soft/are. .ith RP2? "ou ha$e the pristine sources along
/ith an" patches that /ere used? plus complete 8uild instructions. This is an important
ad$antage for se$eral reasons. -or instance? if a ne/ $ersion of a program comes out? "ou
do not necessaril" ha$e to start from scratch to get it to compile. You can look at the
patch to see /hat "ou might need to do. #ll the compiled1in defaults? and all of the
changes that /ere made to get the soft/are to 8uild properl" are easil" $isi8le using this
techniEue.
The goal of keeping sources pristine ma" onl" seem important for de$elopers? 8ut it
results in higher Eualit" soft/are for end users? too. .e /ould like to thank the folks
from the +43US distri8ution for originating the pristine source concept.
9sin& 6)(
RP2 has fi$e 8asic modes of operation <not counting package 8uilding=K installing?
uninstalling? upgrading? Euer"ing? and $erif"ing. This section contains an o$er$ie/ of
each mode. -or complete details and options tr" rpm 11help? or turn to the section called
#dditional Resources for more information on RP2.
-inding RP2s
+efore using an RP2? "ou must kno/ /here to find them. #n 9nternet search /ill return
man" RP2 repositories? 8ut if "ou are looking for RP2 packages 8uilt 8" Red 5at? the"
can 8e found at the follo/ing locationsK
The official Red 5at Linu' C1R42s
The Red 5at &rrata Page a$aila8le at httpKOO///.redhat.comOsupportOerrata
# Red 5at -TP 2irror Site a$aila8le at httpKOO///.redhat.comOmirrors.html
Red 5at 6et/ork
RP2 packages t"picall" ha$e file names like foo1).*1).i0%6.rpm. The file name includes
the package name <foo=? $ersion <).*=? release <)=? and architecture <i0%6=. 9nstalling a
package is as simple as t"ping the follo/ing command at a shell promptK
6,
= rpm -ivh foo-;#1-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
#s "ou can see? RP2 prints out the name of the package and then prints a succession of
hash marks as the package is installed as a progress meter.
6ote
#lthough a command like rpm 1i$h foo1).*1).i0%6.rpm is commonl" used to install an
RP2 package? "ou ma" /ant to consider using rpm 1U$h foo1).*1).i0%6.rpm instead. 1U
is commonl" used for upgrading a package? 8ut it /ill also install ne/ packages.
9nstalling packages is designed to 8e simple? 8ut "ou ma" sometimes see errorsK
Package #lread" 9nstalled
9f the package of the same $ersion is alread" installed? "ou /ill seeK
= rpm -ivh foo-;#1-;#iFGH#rpm
foo package foo1).*1) is alread" installed
M
9f "ou /ant to install the package an"/a" and the same $ersion "ou are tr"ing to install is
alread" installed? "ou can use the 11replacepkgs option? /hich tells RP2 to ignore the
errorK
= rpm -ivh --repacepk&s foo-;#1-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
This option is helpful if files installed from the RP2 /ere deleted or if "ou /ant the
original configuration files from the RP2 to 8e installed.
Conflicting -iles
9f "ou attempt to install a package that contains a file /hich has alread" 8een installed 8"
another package or an earlier $ersion of the same package? "ou@ll seeK
= rpm -ivh foo-;#1-;#iFGH#rpm
foo OusrO8inOfoo conflicts /ith file from 8ar1).*1)
M
60
To make RP2 ignore this error? use the 11replacefiles optionK
= rpm -ivh --repacefies foo-;#1-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
Unresol$ed ependenc"
RP2 packages can _depend_ on other packages? /hich means that the" reEuire other
packages to 8e installed in order to run properl". 9f "ou tr" to install a package /hich has
an unresol$ed dependenc"? "ou@ll seeK
= rpm -ivh foo-;#1-;#iFGH#rpm
failed dependenciesK 8ar is needed 8" foo1).*1)
M
To handle this error "ou should install the reEuested packages. 9f "ou /ant to force the
installation an"/a" <a 8ad idea since the package pro8a8l" /ill not run correctl"=? use the
11nodeps option.
Uninstalling
Uninstalling a package is Nust as simple as installing one. T"pe the follo/ing command at
a shell promptK
= rpm -e foo
M
6ote
6otice that /e used the package name foo? not the name of the original package file foo1
).*1).i0%6.rpm. To uninstall a package? "ou /ill need to replace foo /ith the actual
package name of the original package.
You can encounter a dependenc" error /hen uninstalling a package if another installed
package depends on the one "ou are tr"ing to remo$e. -or e'ampleK
= rpm -e foo
remo$ing these packages /ould 8reak dependenciesK foo is needed 8" 8ar1).*1)
M
67
To cause RP2 to ignore this error and uninstall the package an"/a" </hich is also a 8ad
idea since the package that depends on it /ill pro8a8l" fail to /ork properl"=? use the
11nodeps option.
Upgrading
Upgrading a package is similar to installing one. T"pe the follo/ing command at a shell
promptK
= rpm -9vh foo-E#1-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
.hat "ou do not see a8o$e is that RP2 automaticall" uninstalled an" old $ersions of the
foo package. 9n fact? "ou ma" /ant to al/a"s use 1U to install packages? since it /ill
/ork e$en /hen there are no pre$ious $ersions of the package installed.
Since RP2 performs intelligent upgrading of packages /ith configuration files? "ou ma"
see a message like the follo/ingK sa$ing OetcOfoo.conf as OetcOfoo.conf.rpmsa$e
This message means that "our changes to the configuration file ma" not 8e _for/ard
compati8le_ /ith the ne/ configuration file in the package? so RP2 sa$ed "our original
file? and installed a ne/ one. You should in$estigate the differences 8et/een the t/o
configuration files and resol$e them as soon as possi8le? to ensure that "our s"stem
continues to function properl".
Upgrading is reall" a com8ination of uninstalling and installing? so during an RP2
upgrade "ou can encounter uninstalling and installing errors? plus one more. 9f RP2
thinks "ou are tr"ing to upgrade to a package /ith an older $ersion num8er? "ou /ill seeK
= rpm -9vh foo-;#1-;#iFGH#rpm
foo package foo1,.*1) </hich is ne/er= is alread" installed
M
To cause RP2 to _upgrade_ an"/a"? use the 11oldpackage optionK
= rpm -9vh --odpacka&e foo-;#1-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
-reshening
65
-reshening a package is similar to upgrading one. T"pe the follo/ing command at a shell
promptK
= rpm -7vh foo-;#E-;#iFGH#rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
RP2@s freshen option checks the $ersions of the packages specified on the command line
against the $ersions of packages that ha$e alread" 8een installed on "our s"stem. .hen a
ne/er $ersion of an alread"1installed package is processed 8" RP2@s freshen option? it
/ill 8e upgraded to the ne/er $ersion. 5o/e$er? RP2@s freshen option /ill not install a
package if no pre$iousl"1installed package of the same name e'ists. This differs from
RP2@s upgrade option? as an upgrade /ill install packages? /hether or not an older
$ersion of the package /as alread" installed.
RP2@s freshen option /orks for single packages or a group of packages. 9f "ou ha$e Nust
do/nloaded a large num8er of different packages? and "ou onl" /ant to upgrade those
packages that are alread" installed on "our s"stem? freshening /ill do the No8. 9f "ou use
freshening? "ou /ill not ha$e to deleting an" un/anted packages from the group that "ou
do/nloaded 8efore using RP2.
9n this case? "ou can simpl" issue the follo/ing commandK
= rpm -7vh I#rpm
RP2 /ill automaticall" upgrade onl" those packages that are alread" installed.
Guer"ing
Use the rpm 1E command to Euer" the data8ase of installed packages. The rpm 1E foo
command /ill print the package name? $ersion? and release num8er of the installed
package fooK
= rpm -4 foo
foo1,.*1)
M
6ote
6otice that /e used the package name foo. To Euer" a package? "ou /ill need to replace
foo /ith the actual package name.
66
9nstead of specif"ing the package name? "ou can use the follo/ing options /ith 1E to
specif" the package<s= "ou /ant to Euer". These are called Package Specification
4ptions.
1a Eueries all currentl" installed packages.
1f ZfileV /ill Euer" the package /hich o/ns ZfileV. .hen specif"ing a file? "ou must
specif" the full path of the file <for e'ample? OusrO8inOls=.
1p ZpackagefileV Eueries the package ZpackagefileV.
There are a num8er of /a"s to specif" /hat information to displa" a8out Eueried
packages. The follo/ing options are used to select the t"pe of information for /hich "ou
are searching. These are called 9nformation Selection 4ptions.
1i displa"s package information including name? description? release? siFe? 8uild date?
install date? $endor? and other miscellaneous information.
1l displa"s the list of files that the package contains.
1s displa"s the state of all the files in the package.
1d displa"s a list of files marked as documentation <man pages? info pages? R&s?
etc.=.
1c displa"s a list of files marked as configuration files. These are the files "ou change
after installation to adapt the package to "our s"stem <for e'ample? sendmail.cf? pass/d?
initta8? etc.=.
-or the options that displa" lists of files? "ou can add 1$ to the command to displa" the
lists in a familiar ls 1l format.
Verif"ing
Verif"ing a package compares information a8out files installed from a package /ith the
same information from the original package. #mong other things? $erif"ing compares the
siFe? 25 sum? permissions? t"pe? o/ner? and group of each file.
The command rpm 1V $erifies a package. You can use an" of the Package Selection
4ptions listed for Euer"ing to specif" the packages "ou /ish to $erif". # simple use of
$erif"ing is rpm 1V foo? /hich $erifies that all the files in the foo package are as the"
/ere /hen the" /ere originall" installed. -or e'ampleK
6!
To $erif" a package containing a particular fileK rpm 1Vf O8inO$i
To $erif" #LL installed packagesK rpm 1Va
To $erif" an installed package against an RP2 package fileK rpm 1Vp foo1).*1).i0%6.rpm
This command can 8e useful if "ou suspect that "our RP2 data8ases are corrupt.
9f e$er"thing $erified properl"? there /ill 8e no output. 9f there are an" discrepancies the"
/ill 8e displa"ed. The format of the output is a string of eight characters <a c denotes a
configuration file= and then the file name. &ach of the eight characters denotes the result
of a comparison of one attri8ute of the file to the $alue of that attri8ute recorded in the
RP2 data8ase. # single . <a period= means the test passed. The follo/ing characters
denote failure of certain testsK
5 P 25 checksum
S P file siFe
L P s"m8olic link
T P file modification time
P de$ice
U P user
3 P group
2 P mode <includes permissions and file t"pe=
B P unreada8le file
9f "ou see an" output? use "our 8est Nudgment to determine if "ou should remo$e or
reinstall the package? or fi' the pro8lem in another /a".
6%
Compiling from the original source
6ead documentation
Look for files calledK 96ST#LL? R&? S&TUP? or similar.
Read /ith less docfile? or Fless docfile.gF for .gF files.
*he procedure
The installation procedure for soft/are that comes in tar.gF and tar.8F, packages isn@t
al/a"s the same? 8ut usuall" it@s like thisK
M tar '$Ff package.tar.gF <or tar '$Nf package.tar.8F,=
M cd package
M .Oconfigure
M make
M make install
9f "ou@re luck"? 8" issuing these simple commands "ou unpack? configure? compile? and
install the soft/are package and "ou don@t e$en ha$e to kno/ /hat "ou@re doing.
5o/e$er? it@s health" to take a closer look at the installation procedure and see /hat these
steps mean.
9npackin&
2a"8e "ou@$e alread" noticed that the package containing the source code of the program
has a tar.gF or a tar.8F, e'tension. This means that the package is a compressed tar
archi$e? also kno/n as a tar8all. .hen making the package? the source code and the other
needed files /ere piled together in a single tar archi$e? hence the tar e'tension. #fter
piling them all together in the tar archi$e? the archi$e /as compressed /ith gFip? hence
the gF e'tension.
Some people /ant to compress the tar archi$e /ith 8Fip, instead of gFip. 9n these cases
the package has a tar.8F, e'tension. You install these packages e'actl" the same /a" as
tar.gF packages? 8ut "ou use a 8it different command /hen unpacking.
9t doesn@t matter /here "ou put the tar8alls "ou do/nload from the internet 8ut 9 suggest
creating a special director" for do/nloaded tar8alls. 9n this tutorial 9 assume "ou keep
tar8alls in a director" called dls that "ou@$e created under "our home director". 5o/e$er?
the dls director" is Nust an e'ample. You can put "our do/nloaded tar.gF or tar.8F,
soft/are packages into an" director" "ou /ant. 9n this e'ample 9 assume "our username
is me and "ou@$e do/nloaded a package called pkg.tar.gF into the dls director" "ou@$e
created <OhomeOmeOdls=.
6(
4k? finall" on to unpacking the tar8all. #fter do/nloading the package? "ou unpack it
/ith this commandK
me@puter3 D,ds$ tar xvCf pk&#tar#&C
#s "ou can see? "ou use the tar command /ith the appropriate options <'$Ff= for
unpacking the tar8all. 9f "ou ha$e a package /ith tar.8F, e'tension instead? "ou must tell
tar that this isn@t a gFipped tar archi$e. You do so 8" using the N option instead of F? like
thisK
me@puter3 D,ds$ tar xvjf pk&#tar#'CE
.hat happens after unpacking? depends on the package? 8ut in most cases a director"
/ith the package@s name is created. The ne/l" created director" goes under the director"
/here "ou are right no/. To 8e sure? "ou can gi$e the ls commandK
me@puter3 D,ds$ s
pkg pkg.tar.gF
meaputerK >OdlsL
9n our e'ample unpacking our package pkg.tar.gF did /hat e'pected and created a
director" /ith the package@s name. 6o/ "ou must cd into that ne/l" created director"K
me@puter3 D,ds$ cd pk&
me@puter3 D,ds,pk&$
Read an" documentation "ou find in this director"? like R& or 96ST#LL files?
8efore continuingY
Confi&urin&
6o/? after /e@$e changed into the package@s director" <and done a little RT-2@ing=? it@s
time to configure the package. Usuall"? 8ut not al/a"s <that@s /h" "ou need to check out
the R& and 96ST#LL files= it@s done 8" running the configure script.
You run the script /ith this commandK
me@puter3 D,ds,pk&$ #,confi&ure
.hen "ou run the configure script? "ou don@t actuall" compile an"thing "et. configure
Nust checks "our s"stem and assigns $alues for s"stem1dependent $aria8les. These $alues
are used for generating a 2akefile. The 2akefile in turn is used for generating the actual
8inar".
!*
.hen "ou run the configure script? "ou@ll see a 8unch of /eird messages scrolling on
"our screen. This is normal and "ou shouldn@t /orr" a8out it. 9f configure finds an error?
it complains a8out it and e'its. 5o/e$er? if e$er"thing /orks like it should? configure
doesn@t complain a8out an"thing? e'its? and shuts up.
9f configure e'ited /ithout errors? it@s time to mo$e on to the ne't step.
8uidin&
9t@s finall" time to actuall" 8uild the 8inar"? the e'ecuta8le program? from the source
code. This is done 8" running the make commandK
me@puter3 D,ds,pk&$ make
6ote that make needs the 2akefile for 8uilding the program. 4ther/ise it doesn@t kno/
/hat to do. This is /h" it@s so important to run the configure script successfull"? or
generate the 2akefile some other /a".
.hen "ou run make? "ou@ll see again a 8unch of strange messages filling "our screen.
This is also perfectl" normal and nothing "ou should /orr" a8out. This step ma" take
some time? depending on ho/ 8ig the program is and ho/ fast "our computer is. 9f "ou@re
doing this on an old dementic rig /ith a snail processor? go gra8 "ourself some coffee. #t
this point 9 usuall" lose m" patience completel".
9f all goes as it should? "our e'ecuta8le is finished and read" to run after make has done
its No8. 6o/? the final step is to install the program.
<nstain&
6o/ it@s finall" time to install the program. .hen doing this "ou must 8e root. 9f "ou@$e
done things as a normal user? "ou can 8ecome root /ith the su command. 9t@ll ask "ou the
root pass/ord and then "ou@re read" for the final stepY
me@puter3 D,ds,pk&$ su
Pass/ordK
rootaputerK OhomeOmeOdlsOpkgM
6o/ /hen "ou@re root? "ou can install the program /ith the make install commandK
root@puter3 ,home,me,ds,pk&= make insta
#gain? "ou@ll get some /eird messages scrolling on the screen. #fter it@s stopped?
congratsK "ou@$e installed the soft/are and "ou@re read" to run itY
+ecause in this e'ample /e didn@t change the 8eha$ior of the configure script? the
program /as installed in the default place. 9n man" cases it@s OusrOlocalO8in. 9f
!)
OusrOlocalO8in <or /hate$er place "our program /as installed in= is alread" in "our P#T5?
"ou can Nust run the program 8" t"ping its name.
#nd one more thingK if "ou 8ecame root /ith su? "ou@d 8etter get 8ack "our normal user
pri$ileges 8efore "ou do something stupid. T"pe e'it to 8ecome a normal user againK
root@puter3 ,home,me,ds,pk&= exit
e'it
meaputerK >OdlsOpkgL
Ceanin& up the mess
9 8et "ou /ant to sa$e some disk space. 9f this is the case? "ou@ll /ant to get rid of some
files "ou don@t need. .hen "ou ran make it created all sorts of files that /ere needed
during the 8uild process 8ut are useless no/ and are Nust taking up disk space. This is
/h" "ou@ll /ant to make cleanK
me@puter3 D,ds,pk&$ make cean
5o/e$er? make sure "ou keep "our 2akefile. 9t@s needed if "ou later decide to uninstall
the program and /ant to do it as painlessl" as possi8leY
9ninstain&
So? "ou decided "ou didn@t like the program after allB Uninstalling the programs "ou@$e
compiled "ourself isn@t as eas" as uninstalling programs "ou@$e installed /ith a package
manager? like rpm.
9f "ou /ant to uninstall the soft/are "ou@$e compiled "ourself? do the o8$iousK do some
old1fashioned RT-2@ig. Read the documentation that came /ith "our soft/are package
and see if it sa"s an"thing a8out uninstalling. 9f it doesn@t? "ou can start pulling "our hair
out.
9f "ou didn@t delete "our 2akefile? "ou ma" 8e a8le to remo$e the program 8" doing a
make uninstallK
root@puter3 ,home,me,ds,pk&= make uninsta
9f "ou see /eird te't scrolling on "our screen <8ut at this point "ou@$e pro8a8l" got used
to /eird te't filling the screenB K1= that@s a good sign. 9f make starts complaining at "ou?
that@s a 8ad sign. Then "ou@ll ha$e to remo$e the program files manuall".
9f "ou kno/ /here the program /as installed? "ou@ll ha$e to manuall" delete the installed
files or the director" /here "our program is. 9f "ou ha$e no idea /here all the files are?
"ou@ll ha$e to read the 2akefile and see /here all the files got installed? and then delete
them.
!,
yum
%'out 6epositories
# repositor" is a prepared director" or .e8 site that contains soft/are packages and
inde' files. Soft/are management utilities such as "um automaticall" locate and o8tain
the correct RP2 packages from these repositories. This method frees "ou from ha$ing to
manuall" find and install ne/ applications or updates. You ma" use a single command to
update all s"stem soft/are? or search for ne/ soft/are 8" specif"ing criteria.
# net/ork of ser$ers pro$ide se$eral repositories for each $ersion of Red 5at. The
package management utilities in Red 5at are alread" configured to use three of these
repositoriesK
+ase
The packages that make up a Red 5at release? as it is on disc
Updates
Updated $ersions of packages that are pro$ided in +ase
&'tras
Packages for a large selection of additional soft/are
6ed Hat "eveopment 6epositories
Red 5at also includes settings for se$eral alternati$e repositories. These pro$ide
packages for $arious t"pes of test s"stem? and replace one or more of the standard
repositories.
Third1part" soft/are de$elopers also pro$ide repositories for their Red 5at compati8le
packages.
You ma" also use the package groups pro$ided 8" the Red 5at repositories to manage
related packages as sets. Some third1part" repositories add packages to these groups? or
pro$ide their packages as additional groups.
!0
%vaia'e )acka&e :roups
To $ie/ a list of all of the a$aila8le package groups for "our Red 5at s"stem? run the
command su 1c @"um grouplist@.
Use repositories to ensure that "ou al/a"s recei$e current $ersions of soft/are. 9f se$eral
$ersions of the same package are a$aila8le? "our management utilit" automaticall" selects
the latest $ersion.
%'out "ependencies
Some of the files installed on a Red 5at distri8ution are li8raries /hich ma" pro$ide
functions to multiple applications. .hen an application reEuires a specific li8rar"? the
package /hich contains that li8rar" is a dependenc". To properl" install a package? Red
5at must first satisf" its dependencies. The dependenc" information for a RP2 package
is stored /ithin the RP2 file.
The "um utilit" uses package dependenc" data to ensure that all of reEuirements for an
application are met during installation. 9t automaticall" installs the packages for an"
dependencies not alread" present on "our s"stem. 9f a ne/ application has reEuirements
that conflict /ith e'isting soft/are? "um a8orts /ithout making an" changes to "our
s"stem.
9nderstandin& )acka&e >ames
&ach package file has a long name that indicates se$eral ke" pieces of information. -or
e'ample? this is the full name of a tsclient packageK
tsclient1*.)0,16.i0%6.rpm
2anagement utilities commonl" refer to packages /ith one of three formatsK
Package nameK tsclient
Package name /ith $ersion and release num8ersK tsclient1*.)0,16
Package name /ith hard/are architectureK tsclient.i0%6
-or clarit"? "um lists packages in the format name.architecture. Repositories also
commonl" store packages in separate directories 8" architecture. 9n each case? the
hard/are architecture specified for the package is the minimum t"pe of machine reEuired
to use the package.
i0%6
Suita8le for an" current 9ntel1compati8le computer
noarch
!7
Compati8le /ith all computer architectures
ppc
Suita8le for Po/erPC s"stems? such as #pple Po/er 2acintosh
'%6^67
Suita8le for 6718it 9ntel1compati8le processors? such as 4pterons
Some soft/are ma" 8e optimiFed for particular t"pes of 9ntel1compati8le machine.
Separate packages ma" 8e pro$ided for i0%6? i5%6? i6%6 and '%6^67 computers. #
machine /ith at least an 9ntel Pentium? V9# C0 or compati8le CPU ma" use i5%6
packages. Computers /ith an 9ntel Pentium Pro and a8o$e? or a current model of #2
chip? ma" use i6%6 packages.
Use the short name of the package for "um commands. This causes "um to automaticall"
select the most recent package in the repositories that matches the hard/are architecture
of "our computer.
Specif" a package /ith other name formats to o$erride the default 8eha$ior and force
"um to use the package that matches that $ersion or architecture. 4nl" o$erride "um
/hen "ou kno/ that the default package selection has a 8ug or other fault that makes it
unsuita8le for installation.
(ana&in& Software with 0um
Use the "um utilit" to modif" the soft/are on "our s"stem in four /a"sK
To install ne/ soft/are from package repositories
To install ne/ soft/are from an indi$idual package file
To update e'isting soft/are on "our s"stem
To remo$e un/anted soft/are from "our s"stem
To use "um? specif" a function and one or more packages or package groups. &ach
section 8elo/ gi$es some e'amples.
-or each operation? "um do/nloads the latest package information from the configured
repositories. 9f "our s"stem uses a slo/ net/ork connection "um ma" reEuire se$eral
seconds to do/nload the repositor" inde'es and the header files for each package.
The "um utilit" searches these data files to determine the 8est set of actions to produce
the reEuired result? and displa"s the transaction for "ou to appro$e. The transaction ma"
!5
include the installation? update? or remo$al of additional packages? in order to resol$e
soft/are dependencies.
This is an e'ample of the transaction for installing tsclientK
```````````````````````````````````````````````````````````````
``````````````
Package #rch Version Repositor" SiFe
```````````````````````````````````````````````````````````````
``````````````
9nstallingK
tsclient i0%6 *.)0,16 8ase ,7! k
9nstalling for dependenciesK
rdesktop i0%6 ).7.*1, 8ase )*! k
Transaction Summar"
```````````````````````````````````````````````````````````````
``````````````
9nstall , Package<s=
Update * Package<s=
Remo$e * Package<s=
Total do/nload siFeK 055 k
9s this ok H"O6IK
&'ample ). -ormat of "um Transaction Reports
Re$ie/ the list of changes? and then press " to accept and 8egin the process. 9f "ou press
6 or &nter? "um does not do/nload or change an" packages.
)acka&e Jersions
The "um utilit" onl" displa"s and uses the ne/est $ersion of each package? unless "ou
specif" an older $ersion.
The "um utilit" also imports the repositor" pu8lic ke" if it is not alread" installed on the
rpm ke"ring.
This is an e'ample of the pu8lic ke" importK
/arningK rpmts^5dr-rom-dnoK 5eader V0 S# signatureK 64D&Y? ke" 9 7f,a6fd,
pu8lic ke" not a$aila8le for tsclient1*.)0,16.i0%6.rpm
Retrie$ing 3P3 ke" from fileKOOOetcOpkiOrpm1gpgORP213P31D&Y1fedora
9mporting 3P3 ke" *'7-,#6-, _-edora ProNect Zfedoraaredhat.comV_
9s this ok H"O6IK
&'ample ,. -ormat of "um Pu8lic De" 9mport
!6
Check the pu8lic ke"? and then press " to import the ke" and authoriFe the ke" for use. 9f
"ou press 6 or &nter? "um stops /ithout installing an" packages.
To ensure that do/nloaded packages are genuine? "um $erifies the digital signature of
each package against the pu8lic ke" of the pro$ider. 4nce all of the packages reEuired for
the transaction are successfull" do/nloaded and $erified? "um applies them to "our
s"stem.
*ransaction Lo&
&$er" completed transaction records the affected packages in the log file
O$arOlogO"um.log. You ma" onl" read this file /ith root access.
<nstain& >ew Software with 0um
To install the package tsclient? enter the commandK
su -c K0um insta tscientK
&nter the pass/ord for the root account /hen prompted.
To install the package group 2"SGL ata8ase? enter the commandK
su -c K0um &roupinsta A(0SLL "ata'aseAK
&nter the pass/ord for the root account /hen prompted.
>ew Services 6e4uire %ctivation
.hen "ou install a ser$ice? Red 5at does not acti$ate or start it. To configure a ne/
ser$ice to run on 8ootup? choose esktop g S"stem Settings g Ser$er Settings g
Ser$ices? or use the chkconfig and ser$ice command1line utilities.
9pdatin& Software with 0um
To update the tsclient package to the latest $ersion? t"peK
su -c K0um update tscientK
&nter the pass/ord for the root account /hen prompted.
>ew Software Jersions 6e4uire 6eoadin&
9f a piece of soft/are is in use /hen "ou update it? the old $ersion remains acti$e until the
application or ser$ice is restarted. Dernel updates take effect /hen "ou re8oot the s"stem.
Merne )acka&es
!!
Dernel packages remain on the s"stem after the" ha$e 8een superseded 8" ne/er
$ersions. This ena8les "ou to 8oot "our s"stem /ith an older kernel if an error occurs
/ith the current kernel. To minimiFe maintenance? "um automaticall" remo$es o8solete
kernel packages from "our s"stem? retaining onl" the current kernel and the pre$ious
$ersion.
To update all of the packages in the package group 2"SGL ata8ase? enter the
commandK
su -c K0um &roupupdate A(0SLL "ata'aseAK
&nter the pass/ord for the root account /hen prompted. Updating the &ntire S"stem
6emovin& Software with 0um
To remo$e soft/are? "um e'amines "our s"stem for 8oth the specified soft/are? and an"
soft/are /hich claims it as a dependenc". The transaction to remo$e the soft/are deletes
8oth the soft/are and the dependencies.
To remo$e the tsclient package from "our s"stem? use the commandK
su -c K0um remove tscientK
&nter the pass/ord for the root account /hen prompted.
To remo$e all of the packages in the package group 2"SGL ata8ase? enter the
commandK
su -c K0um &roupremove A(0SLL "ata'aseAK
&nter the pass/ord for the root account /hen prompted.
sysctl
S"sctl is an interface for e'amining and d"namicall" changing parameters in a +S Uni'
<or Linu'= operating s"stem kernel. 3enerall"? these parameters <identified as o8Nects in a
2anagement 9nformation +ase= descri8e tuna8le limits such as the siFe of a shared
memor" segment? the num8er of threads the operating s"stem /ill use as an 6-S client?
or the ma'imum num8er of processes on the s"stem\ or descri8e? ena8le or disa8le
8eha$iors such as 9P for/arding? securit" restrictions on the superuser <the _securele$el_=?
or de8ugging output.
3enerall"? a s"stem call or s"stem call /rapper is pro$ided for use 8" programs? as /ell
as an administrati$e program and a configuration file <for setting the tuna8le parameters
/hen the s"stem 8oots=.
!%
This feature appeared in the _7.7+S_ $ersion of Uni'? and is also used in the Linu'
kernel. 9t has the ad$antage o$er hardcoded constants that changes to the parameters can
8e made d"namicall" /ithout recompiling the kernel.
&'amples
.hen 9P for/arding is ena8led? the operating s"stem kernel /ill act as a router. -or the
Linu' kernel? the parameter net.ip$7.ip^for/ard can 8e set to ) to ena8le this 8eha$ior. 9n
-ree+S? 6et+S and 4pen+S the parameter is net.inet.ip.for/arding.
9n most s"stems? the command s"sctl 1/ parameter`) /ill ena8le the desired 8eha$ior.
This /ill persist until the ne't re8oot. 9f the 8eha$ior should 8e ena8led /hene$er the
s"stem 8oots? the line parameter`) can 8e added to the file OetcOs"sctl.conf. #dditionall"?
some s"sctl $aria8les cannot 8e modified after the s"stem is 8ooted? these $aria8les
<depending on the $aria8le and the $ersion and fla$or of +S= need to either 8e set
staticall" in the kernel at compile time or set in O8ootOloader.conf.
The proc files"stem
Under the Linu' kernel? the proc files"stem also pro$ides an interface to the s"sctl
parameters. -or e'ample? the parameter net.ip$7.ip^for/ard corresponds /ith the file
OprocOs"sOnetOip$7Oip^for/ard. Reading or changing this file is eEui$alent to changing the
parameter using the s"sctl command.
4racle parameters
kernel.shmma'`,0)06%,(70
kernel.msgmni`)*,7
kernel.sem`),5* ,56*** )** )*,7
$m.ma'^map^count`0*****
net.ip$7.ip^local^port^range ` )*,7 65***
Linu" Partitions
"evices
There is a special nomenclature that linu' uses to refer to hard dri$e partitions that must
8e understood in order to follo/ the discussion on the follo/ing pages.
9n Linu'? partitions are represented 8" de$ice files. These are phone" files located in
Ode$. 5ere are a fe/ entriesK
8r/1r/1111 ) root disk 0? * 2a" 5 )((% hda
8r/1r/1111 ) root disk %? * 2a" 5 )((% sda
cr/1111111 ) root tt" 7? 67 2a" 5 )((% tt"S*
!(
# de$ice file is a file /ith t"pe c < for _character_ de$ices? de$ices that do not use the
8uffer cache= or 8 <for _8lock_ de$ices? /hich go through the 8uffer cache=. 9n Linu'? all
disks are represented as 8lock de$ices onl".
"evice names
>amin& Convention
+" con$ention? 9& dri$es /ill 8e gi$en de$ice names Ode$Ohda to Ode$Ohdd. 5ard ri$e
# <Ode$Ohda= is the first dri$e and 5ard ri$e C <Ode$Ohdc= is the third.
Ta8le ,. 9& controller naming con$ention
dri$e name dri$e controller dri$e num8er
Ode$Ohda ) )
Ode$Ohd8 ) ,
Ode$Ohdc , )
Ode$Ohdd , ,
# t"pical PC has t/o 9& controllers? each of /hich can ha$e t/o dri$es connected to it.
-or e'ample? Ode$Ohda is the first dri$e <master= on the first 9& controller and Ode$Ohdd
is the second <sla$e= dri$e on the second controller <the fourth 9& dri$e in the
computer=.
You can /rite to these de$ices directl" <using cat or dd=. 5o/e$er? since these de$ices
represent the entire disk? starting at the first 8lock? "ou can mistakenl" o$er/rite the
master 8oot record and the partition ta8le? /hich /ill render the dri$e unusa8le.
Ta8le 0. partition names
dri$e name dri$e controller dri$e num8er partition t"pe partition
num8er
Ode$Ohda) ) ) primar" )
Ode$Ohda, ) ) primar" ,
Ode$Ohda0 ) ) primar" 0
Ode$Ohda7 ) ) s/ap 6#
Ode$Ohd8) ) , primar" )
Ode$Ohd8, ) , primar" ,
Ode$Ohd80 ) , primar" 0
Ode$Ohd87 ) , primar" 7
%*
4nce a dri$e has 8een partitioned? the partitions /ill represented as num8ers on the end
of the names. -or e'ample? the second partition on the second dri$e /ill 8e Ode$Ohd8,.
The partition t"pe <primar"= is listed in the ta8le a8o$e for clarit"?
Ta8le 7. SCS9 ri$es
dri$e name dri$e controller dri$e num8er partition t"pe partition
num8er
Ode$Osda) ) 6 primar" )
Ode$Osda, ) 6 primar" ,
Ode$Osda0 ) 6 primar" 0
SCS9 dri$es follo/ a similar pattern\ The" are represented 8" @sd@ instead of @hd@. The first
partition of the second SCS9 dri$e /ould therefore 8e Ode$Osd8). 9n the ta8le a8o$e? the
dri$e num8er is ar8itrail" chosen to 8e 6 to introduce the idea that SCS9 9 num8ers do
not map onto de$ice names under linu'.
>ame %ssi&nment
Under <Sun= Solaris and <S39= 9R9;? the de$ice name gi$en to a SCS9 dri$e has some
relationship to /here "ou plug it in. Under linu'? there is onl" /ailing and gnashing of
teeth.
+efore
SCS9 9 M, SCS9 9 M5 SCS9 9 M! SCS9 9 M%
Ode$Osda Ode$Osd8 Ode$Osdc Ode$Osdd
#fter
SCS9 9 M, SCS9 9 M! SCS9 9 M%
Ode$Osda Ode$Osd8 Ode$Osdc
SCS9 dri$es ha$e 9 num8ers /hich go from ) through )5. Lo/er SCS9 9 num8ers are
assigned lo/er1order letters. -or e'ample? if "ou ha$e t/o dri$es num8ered , and 5? then
M, /ill 8e Ode$Osda and M5 /ill 8e Ode$Osd8. 9f "ou remo$e either? all the higher num8ered
dri$es /ill 8e renamed the ne't time "ou 8oot up.
9f "ou ha$e t/o SCS9 controllers in "our linu' 8o'? "ou /ill need to e'amine the output
of O8inOdmesg in order to see /hat name each dri$e /as assigned. 9f "ou remo$e one of
t/o controllers? the remaining controller might ha$e all its dri$es renamed. 3rrr...
There are t/o /ork1arounds\ 8oth in$ol$e using a program to put a la8el on each
partition. The la8el is persistent e$en /hen the de$ice is ph"sicall" mo$ed. You then
refer to the partition directl" or indirectl" 8" la8el.
%)
Lo&ica )artitions
Ta8le 5. Logical Partitions
dri$e name dri$e controller dri$e num8er partition t"pe partition
num8er
Ode$Ohd8) ) , primar" )
Ode$Ohd8, ) , e'tended 6#
Ode$Ohda5 ) , logical ,
Ode$Ohd86 ) , logical 0
The ta8le a8o$e illustrates a m"sterious Nump in the name assignments. This is due to the
use of logical partitions. This is all "ou ha$e to kno/ to deal /ith linu' disk de$ices. -or
the sake of completeness? see Dristian@s discussion of de$ice num8ers 8elo/.
"evice num'ers
The onl" important thing /ith a de$ice file are its maNor and minor de$ice num8ers?
/hich are sho/n instead of the file siFeK
$ s - ,dev,hda
Ta8le 6. e$ice file attri8utes
8r/1r/1111 ) root disk 0? * Cul )%
)((7
Ode$Ohda
permissions o/ner group maNor
de$ice
num8er
minor
de$ice
num8er
date de$ice
name
.hen accessing a de$ice file? the maNor num8er selects /hich de$ice dri$er is 8eing
called to perform the inputOoutput operation. This call is 8eing done /ith the minor
num8er as a parameter and it is entirel" up to the dri$er ho/ the minor num8er is 8eing
interpreted. The dri$er documentation usuall" descri8es ho/ the dri$er uses minor
num8ers. -or 9& disks? this documentation is in OusrOsrcOlinu'OocumentationOide.t't.
-or SCS9 disks? one /ould e'pect such documentation in
OusrOsrcOlinu'OocumentationOscsi.t't? 8ut it isn@t there. 4ne has to look at the dri$er
source to 8e sure < OusrOsrcOlinu'Odri$erOscsiOsd.cK)%71)(6=. -ortunatel"? there is Peter
#n$in@s list of de$ice num8ers and names in OusrOsrcOlinu'OocumentationOde$ices.t't\
see the entries for 8lock de$ices? maNor 0? ,,? 00? 07 for 9& and maNor % for SCS9 disks.
%,
The maNor and minor num8ers are a 8"te each and that is /h" the num8er of partitions
per disk is limited.
Partition Types
# partition is la8eled to host a certain kind of file s"stem <not to 8e confused /ith a
$olume la8el. Such a file s"stem could 8e the linu' standard e't, file s"stem or linu'
s/ap space? or e$en foreign file s"stems like <2icrosoft= 6T-S or <Sun= U-S. There is a
numerical code associated /ith each partition t"pe. -or e'ample? the code for e't, is
*'%0 and linu' s/ap is *'%,. To see a list of partition t"pes and their codes? e'ecute
Os8inOsfdisk 1T
7orei&n )artition *0pes
The partition t"pe codes ha$e 8een ar8itraril" chosen <"ou can@t figure out /hat the"
should 8e= and the" are particular to a gi$en operating s"stem. Therefore? it is
theoreticall" possi8le that if "ou use t/o operating s"stems /ith the same hard dri$e? the
same code might 8e used to designate t/o different partition t"pes. 4SO, marks its
partitions /ith a *'*! t"pe and so does .indo/s 6T@s 6T-S. 2S14S allocates se$eral
t"pe codes for its $arious fla$ors of -#T file s"stemsK *'*)? *'*7 and *'*6 are kno/n.
R14S used *'%) to indicate protected -#T partitions? creating a t"pe clash /ith
Linu'O2ini' at that time? 8ut neither Linu'O2ini' nor R14S are /idel" used an"
more.
4SO, marks its partitions /ith a *'*! t"pe and so does .indo/s 6T@s 6T-S. 2S14S
allocates se$eral t"pe codes for its $arious fla$ors of -#T file s"stemsK *'*)? *'*7 and
*'*6 are kno/n. R14S used *'%) to indicate protected -#T partitions? creating a t"pe
clash /ith Linu'O2ini' at that time? 8ut neither Linu'O2ini' nor R14S are /idel"
used an" more.
)rimar0 )artitions
The num8er of partitions on an 9ntel18ased s"stem /as limited from the $er" 8eginningK
The original partition ta8le /as installed as part of the 8oot sector and held space for onl"
four partition entries. These partitions are no/ called primar" partitions.
Lo&ica )artitions
4ne primar" partition of a hard dri$e ma" 8e su8partitioned. These are logical partitions.
This effecti$el" allo/s us to skirt the historical four partition limitation.
The primar" partition used to house the logical partitions is called an e'tended partition
and it has its o/n file s"stem t"pe <*'*5=. Unlike primar" partitions? logical partitions
must 8e contiguous. &ach logical partition contains a pointer to the ne't logical partition?
/hich implies that the num8er of logical partitions is unlimited. 5o/e$er? linu' imposes
%0
limits on the total num8er of an" t"pe of partition on a dri$e? so this effecti$el" limits the
num8er of logical partitions. This is at most )5 partitions total on an SCS9 disk and 60
total on an 9& disk.
Swap )artitions
&$er" process running on "our computer is allocated a num8er of 8locks of R#2. These
8locks are called pages. The set of in1memor" pages /hich /ill 8e referenced 8" the
processor in the $er" near future is called a _/orking set._ Linu' tries to predict these
memor" accesses <assuming that recentl" used pages /ill 8e used again in the near
future= and keeps these pages in R#2 if possi8le.
9f "ou ha$e too man" processes running on a machine? the kernel /ill tr" to free up R#2
8" /riting pages to disk. This is /hat s/ap space is for. 9t effecti$el" increases the
amount of memor" "ou ha$e a$aila8le. 5o/e$er? disk 9O4 is a8out a hundred times
slo/er than reading from and /riting to R#2. Consider this emergenc" memor" and not
e'tra memor".
9f memor" 8ecomes so scarce that the kernel pages out from the /orking set of one
process in order to page in for another? the machine is said to 8e thrashing. Some readers
might ha$e inad$ertenl" e'perienced thisK the hard dri$e is grinding a/a" like craF"? 8ut
the computer is slo/ to the point of 8eing unusa8le. S/ap space is something "ou need to
ha$e? 8ut it is no su8stitute for sufficient R#2.
)artitionin& with fdisk
This section sho/s "ou ho/ to actuall" partition "our hard dri$e /ith the fdisk utilit".
Linu' allo/s onl" 7 primar" partitions. You can ha$e a much larger num8er of logical
partitions 8" su81di$iding one of the primar" partitions. 4nl" one of the primar"
partitions can 8e su81di$ided.
&'amplesK
-our primar" partitions
2i'ed primar" and logical partitions
fdisk usa&e
fdisk is started 8" t"ping <as root= fdisk de$ice at the command prompt. de$ice might 8e
something like Ode$Ohda or Ode$Osda. The 8asic fdisk commands "ou need areK
p print the partition ta8le
n create a ne/ partition
%7
d delete a partition
E Euit /ithout sa$ing changes
/ /rite the ne/ partition ta8le and e'it
Changes "ou make to the partition ta8le do not take effect until "ou issue the /rite </=
command. 5ere is a sample partition ta8leK
isk Ode$Ohd8K 67 heads? 60 sectors? 6,) c"linders
Units ` c"linders of 7*0, A 5), 8"tes
e$ice +oot Start &nd +locks 9d S"stem
Ode$Ohd8) A ) )%7 0!*(),: %0 Linu'
Ode$Ohd8, )%5 06% 0!*(77 %0 Linu'
Ode$Ohd80 06( 55, 0!*(77 %0 Linu'
Ode$Ohd87 550 6,) )0()*7 %, Linu' s/ap
The first line sho/s the geometr" of "our hard dri$e. 9t ma" not 8e ph"sicall" accurate?
8ut "ou can accept it as though it /ere. The hard dri$e in this e'ample is made of 0,
dou8le1sided platters /ith one head on each side <pro8a8l" not true=. &ach platter has 6,)
concentric tracks. # 01dimensional track <the same track on all disks= is called a c"linder.
&ach track is di$ided into 60 sectors. &ach sector contains 5), 8"tes of data. Therefore
the 8lock siFe in the partition ta8le is 67 heads A 60 sectors A 5), 8"tes er...di$ided 8"
)*,7. <See 7 for discussion on pro8lems /ith this calculation.= The start and end $alues
are c"linders.
7our primar0 partitions
The o$er$ie/K
ecide on the siFe of "our s/ap space and /here it ought to go. i$ide up the remaining
space for the three other partitions.
&'ampleK
9 start fdisk from the shell promptK
= fdisk ,dev,hd'
/hich indicates that 9 am using the second dri$e on m" 9& controller. .hen 9 print the
<empt"= partition ta8le? 9 Nust get configuration information.
Command <m for help=K p
isk Ode$Ohd8K 67 heads? 60 sectors? 6,) c"linders
Units ` c"linders of 7*0, A 5), 8"tes
%5
9 kne/ that 9 had a ).,38 dri$e? 8ut no/ 9 reall" kno/K 67 A 60 A 5), A 6,) `
),%)(%,767 8"tes. 9 decide to reser$e ),%28 of that space for s/ap? lea$ing
))50(%,767. 9f 9 use one of m" primar" partitions for s/ap? that means 9 ha$e three left
for e't, partitions. i$ided eEuall"? that makes for 0%728 per partition. 6o/ 9 get to
/ork.
Command <m for help=K n
Command action
e e'tended
p primar" partition <)17=
p
Partition num8er <)17=K )
-irst c"linder <)16,)? default )=KZR&TUR6V
Using default $alue )
Last c"linder or :siFe or :siFe2 or :siFeD <)16,)? default 6,)=K :0%72
6e't? 9 set up the partition 9 /ant to use for s/apK
Command <m for help=K n
Command action
e e'tended
p primar" partition <)17=
p
Partition num8er <)17=K ,
-irst c"linder <)(!16,)? default )(!=KZR&TUR6V
Using default $alue )(!
Last c"linder or :siFe or :siFe2 or :siFeD <)(!16,)? default 6,)=K :),%2
6o/ the partition ta8le looks like thisK
e$ice +oot Start &nd +locks 9d S"stem
Ode$Ohd8) ) )(6 0(5)*7 %0 Linu'
Ode$Ohd8, )(! ,6, )00*56 %0 Linu'
9 set up the remaining t/o partitions the same /a" 9 did the first. -inall"? 9 make the first
partition 8oota8leK
Command <m for help=K a
Partition num8er <)17=K )
#nd 9 make the second partition of t"pe s/apK
Command <m for help=K t
Partition num8er <)17=K ,
5e' code <t"pe L to list codes=K %,
Changed s"stem t"pe of partition , to %, <Linu' s/ap=
Command <m for help=K p
The end resultK
isk Ode$Ohd8K 67 heads? 60 sectors? 6,) c"linders
Units ` c"linders of 7*0, A 5), 8"tes
%6
e$ice +oot Start &nd +locks 9d S"stem
Ode$Ohd8) A ) )(6 0(5)*7: %0 Linu'
Ode$Ohd8, )(! ,6, )00*56 %, Linu' s/ap
Ode$Ohd80 ,60 75% 0(5)06 %0 Linu'
Ode$Ohd87 75( 6,) 0,%6*% %0 Linu'
-inall"? 9 issue the /rite command </= to /rite the ta8le on the disk.
(ixed primar0 and o&ica partitions
The o$er$ie/K create one use one of the primar" partitions to house all the e'tra
partitions. Then create logical partitions /ithin it. Create the other primar" partitions
8efore or after creating the logical partitions.
&'ampleK
9 start fdisk from the shell promptK
= fdisk ,dev,sda
/hich indicates that 9 am using the first dri$e on m" SCS9 chain.
-irst 9 figure out ho/ man" partitions 9 /ant. 9 kno/ m" dri$e has a )%038 capacit" and
9 /ant ,638 partitions <8ecause 9 happen to ha$e 8ack1up tapes that are a8out that siFe=.
)%038 O ,638 ` >!
so 9 /ill need ! partitions. &$en though fdisk accepts partition siFes e'pressed in 28 and
D8? 9 decide to calculate the num8er of c"linders that /ill end up in each partition
8ecause fdisk reports start and stop points in c"linders. 9 see /hen 9 enter fdisk that 9 ha$e
,,%** c"linders.
V The num8er of c"linders for this disk is set to ,,%**. There is
V nothing /rong /ith that? 8ut this is larger than )*,7? and could in
V certain setups cause pro8lems /ithK )= soft/are that runs at 8oot
V time <e.g.? L9L4= ,= 8ooting and partitioning soft/are from other
V 4Ss <e.g.? 4S -9SD? 4SO, -9SD=
So? ,,%** total c"linders di$ided 8" se$en partitions is 0,5% c"linders. &ach partition
/ill 8e a8out 0,5% c"linders long. 9 ignore the /arning msg 8ecause this is not m" 8oot
dri$e.
Since 9 ha$e 7 primar" partitions? 0 of them can 8e 0,5% long. The e'tended partition /ill
ha$e to 8e <7 A 0,5%=? or )0*0,? c"linders long in order to contain the 7 logical partitions.
%!
9 enter the follo/ing commands to set up the first of the 0 primar" partitions <stuff 9 t"pe
is 8old =K
Command <m for help=K n
Command action
e e'tended
p primar" partition <)17=
p
Partition num8er <)17=K )
-irst c"linder <)1,,%**? default )=K ZR&TUR6V
Using default $alue )
Last c"linder or :siFe or :siFe2 or :siFeD <)1,,%**? default ,,%**=K 0,5%
The last partition is the e'tended partitionK
Partition num8er <)17=K 7
-irst c"linder <(!!51,,%**? default (!!5=K ZR&TUR6V
Using default $alue (!!5
Last c"linder or :siFe or :siFe2 or :siFeD <(!!51,,%**? default ,,%**=K ZR&TUR6V
Using default $alue ,,%**
The result? /hen 9 issue the print ta8le command isK
Ode$Osda) ) 0,5% ,6)6(%50: %0 Linu'
Ode$Osda, 0,5( 65)6 ,6)6(%%5 %0 Linu'
Ode$Osda0 65)! (!!7 ,6)6(%%5 %0 Linu'
Ode$Osda7 (!!5 ,,%** )*760)075 5 &'tended
6e't 9 segment the e'tended partition into 7 logical partitions? starting /ith the first
logical partition? into 0,5%1c"linder segments. The logical partitions automaticall" start
from Ode$Osda5.
Command <m for help=K n
-irst c"linder <(!!51,,%**? default (!!5=K ZR&TUR6V
Using default $alue (!!5
Last c"linder or :siFe or :siFe2 or :siFeD <(!!51,,%**? default ,,%**=K )0*0,
The end result isK
e$ice +oot Start &nd +locks 9d S"stem
Ode$Osda) ) 0,5% ,6)6(%50: %0 Linu'
Ode$Osda, 0,5( 65)6 ,6)6(%%5 %0 Linu'
Ode$Osda0 65)! (!!7 ,6)6(%%5 %0 Linu'
Ode$Osda7 (!!5 ,,%** )*760)075 5 &'tended
Ode$Osda5 (!!5 )0*0, ,6)6(%50: %0 Linu'
Ode$Osda6 )0*00 )6,(* ,6)6(%50: %0 Linu'
Ode$Osda! )6,() )(5%7 ,675(*,0: %0 Linu'
Ode$Osda% )(5%5 ,,%** ,5%0,7%%: %0 Linu'
%%
-inall"? 9 issue the /rite command </= to /rite the ta8le on the disk. To make the
partitions usa8le? 9 /ill ha$e to format each partition and then mount it.
Su'mitted Exampes
9@d like to su8mit m" partition la"out? 8ecause it /orks /ell /ith an" distri8ution of
Linu' <e$en 8ig RP2 8ased ones=. 9 ha$e one hard dri$e that ... is )* gigs? e'actl".
.indo/s can@t see a8o$e (.0 gigs of it? 8ut Linu' can see it all? and use it all. 9t also has
much more than )*,7 c"lenders.
Ta8le !. Partition la"out e'ample
Partition 2ount point SiFe
Ode$Ohda) O8oot <)5 megs=
Ode$Ohda, /indo/s (% partition <, gigs=
Ode$Ohda0 e'tended <6O#=
Ode$Ohda5 s/ap space <67 megs=
Ode$Ohda6 Otmp <5* megs=
Ode$Ohda! O <)5* megs=
Ode$Ohda% Ousr <).5 gigs=
Ode$Ohda( Ohome <rest of dri$e=
LV(
LV2 is a logical $olume manager for the Linu' kernel. 9t /as originall" /ritten in )((%
8" 5einF 2auelshagen? /ho 8ased its design on that of the LV2 in 5P1U;.
The installers for the Red 5at? 2ontaVista Linu'? SL&? e8ian 36UOLinu'? and
U8untu distri8utions are LV21a/are and can install a 8oota8le s"stem /ith a root
files"stem on a logical $olume.
7eatures
The LV2 canK
ResiFe $olume groups online 8" a8sor8ing ne/ ph"sical $olumes <PV= or eNecting
e'isting ones.
ResiFe logical $olumes online 8" concatenating e'tents onto them or truncating e'tents
from them.
Create read1onl" snapshots of logical $olumes <LV2)=.
Create read1/rite snapshots of logical $olumes <LV2,=.
Stripe /hole or parts of logical $olumes across multiple PVs? in a fashion similar to
R#9*.
2irror /hole or parts of logical $olumes? in a fashion similar to R#9)
2o$e online logical $olumes 8et/een PVs.
%(
Split or merge $olume groups in situ <as long as no logical $olumes span the split=. This
can 8e useful /hen migrating /hole logical $olumes to or from offline storage.
(issin& features
LV2 cannot pro$ide parit" 8ased redundanc" similar to R#97? R#95? or R#96.
<mpementation
LV2 keeps a metadata header at the start of e$er" PV? each of /hich is uniEuel"
identified 8" a UU9. &ach PV@s header is a complete cop" of the entire $olume group@s
la"out? including the UU9s of all other PV? the UU9s of all logical $olumes and an
allocation map of P&s to L&s.
9n the ,.61series Linu' kernels? the LV2 is implemented in terms of the de$ice mapper? a
8lock1le$el scheme for creating $irtual 8lock de$ices and mapping their contents onto
other 8lock de$ices. This minimiFes the amount of the relati$el" hard1to1de8ug kernel
code needed to implement the LV2 and also allo/s its 9O4 redirection ser$ices to 8e
shared /ith other $olume managers <such as &V2S=.
#n" LV21specific code is pushed out into its user1space tools. To 8ring a $olume group
online? for e'ample? the _$gchange_ toolK
Searches for PVs in all a$aila8le 8lock de$ices.
Parses the metadata header in each PV found.
Computes the la"outs of all $isi8le $olume groups.
Loops o$er each logical $olume in the $olume group to 8e 8rought online andK
Checks if the logical $olume to 8e 8rought online has all its PVs $isi8le.
Creates a ne/? empt" de$ice mapping.
2aps it </ith the _linear_ target= onto the data areas of the PVs the logical $olume
8elongs to.
To mo$e an online logical $olume 8et/een PVs? the _p$mo$e_ toolK
Creates a ne/? empt" de$ice mapping for the destination.
#pplies the _mirror_ target to the original and destination maps. The kernel /ill start the
mirror in _degraded_ mode and 8egin cop"ing data from the original to the destination to
8ring it into s"nc.
Replaces the original mapping /ith the destination /hen the mirror comes into s"nc? then
destro"s the original.
These de$ice mapper operations take place transparentl"? /ithout applications or
files"stems 8eing a/are that their underl"ing storage is mo$ing.
Exampe3 % 8asic 7ie Server
# simple? practical e'ample of LV2 use is a traditional file ser$er? /hich pro$ides
centraliFed 8ackup? storage space for media files? and shared file space for se$eral famil"
(*
mem8ers@ computers. -le'i8ilit" is a ke" reEuirement\ /ho kno/s /hat storage
challenges ne't "ear@s technolog" /ill 8ringB
-or e'ample? suppose "our reEuirements areK
7**3 1 Large media file storage
5*3 1 4nline 8ackups of t/o laptops and three desktops <)*3 each=
)*3 1 Shared files
Ultimatel"? these reEuirements ma" increase a great deal o$er the ne't "ear or t/o? 8ut
e'actl" ho/ much and /hich partition /ill gro/ the most are still unkno/n.
"isk Hardware
Traditionall"? a file ser$er uses SCS9 disks? 8ut toda" S#T# disks offer an attracti$e
com8ination of speed and lo/ cost. #t the time of this /riting? ,5* 3+ S#T# dri$es are
commonl" a$aila8le for around L)**\ for a tera8"te? the cost is around L7**.
S#T# dri$es are not named like #T# dri$es <hda? hd8=? 8ut like SCS9 <sda? sd8=. 4nce
the s"stem has 8ooted /ith S#T# support? it has four ph"sical de$ices to /ork /ithK
Ode$Osda ,5).* 3+
Ode$Osd8 ,5).* 3+
Ode$Osdc ,5).* 3+
Ode$Osdd ,5).* 3+
6e't? partition these for use /ith LV2. You can do this /ith fdisk 8" specif"ing the
_Linu' LV2_ partition t"pe %e. The finished product looks like thisK
= fdisk - ,dev,sdd
isk Ode$OsddK ,5).* 3+? ,5)***)(0*,7 8"tes
,55 heads? 60 sectorsOtrack? 0*5)5 c"linders
Units ` c"linders of )6*65 A 5), ` %,,5,%* 8"tes
e$ice Start &nd +locks 9d S"stem
Ode$Osdd) ) 0*5)5 ,75)))!*6 %e Linu' LV2
6otice the partition t"pe is %e? or _Linu' LV2._
Creatin& a Jirtua Joume
9nitialiFe each of the disks using the p$create commandK
= pvcreate ,dev,sda ,dev,sd' ,dev,sdc ,dev,sdd
()
This sets up all the partitions on these dri$es for use under LV2? allo/ing creation of
$olume groups. To e'amine a$aila8le PVs? use the p$displa" command. This s"stem /ill
use a single1$olume group named data$gK
= v&create datav& ,dev,sda; ,dev,sd'; ,dev,sdc; ,dev,sdd;
Use $gdispla" to see the ne/l" created data$g V3 /ith the four dri$es stitched together.
6o/ create the logical $olumes /ithin themK
= vcreate --name mediav --siCe N11:
= vcreate --name 'ackupv --siCe O1:
= vcreate --name sharev --siCe ;1:
.ithout LV2? "ou might allocate all a$aila8le disk space to the partitions "ou@re
creating? 8ut /ith LV2? it is /orth/hile to 8e conser$ati$e? allocating onl" half the
a$aila8le space to the current reEuirements. #s a general rule? it@s easier to gro/ a
files"stem than to shrink it? so it@s a good strateg" to allocate e'actl" /hat "ou need
toda"? and lea$e the remaining space unallocated until "our needs 8ecome clearer. This
method also gi$es "ou the option of creating ne/ $olumes /hen ne/ needs arise <such as
a separate encr"pted file share for sensiti$e data=. To e'amine these $olumes? use the
l$displa" command.
6o/ "ou ha$e se$eral nicel" named logical $olumes at "our disposalK
Ode$Odata$gO8ackupl$ <also Ode$OmapperOdata$g18ackupl$=
Ode$Odata$gOmedial$ <also Ode$OmapperOdata$g1medial$=
Ode$Odata$gOsharel$ <also Ode$OmapperOdata$g1sharel$=
(,
U,./ Sumary
*0po&raphica conventions
9n /hat follo/s? /e shall use the follo/ing t"pographical con$entionsK
Characters /ritten in bold typewriter font are commands to 8e t"ped into the
computer as the" stand.
Characters /ritten in italic typewriter font indicate non1specific file or
director" names.
.ords inserted /ithin sEuare 8rackets [Ctrl] indicate ke"s to 8e pressed.
So? for e'ample?
T ls anydirectory H&nterI
means _at the U69; prompt T? t"pe ls follo/ed 8" the name of some director"? then
press the ke" marked &nter_
on@t forget to press the H&nterI ke"K commands are not sent to the computer until this is
done.
6oteK U69; is case1sensit$e? so LS is not the same as ls.
The same applies to filenames? so m"file.t't? 2"-ile.t't and 2Y-9L&.T;T are three
seperate files. +e/are if cop"ing files to a PC? since 4S and .indo/s do not make this
distinction.
.ntroduction
This session concerns U69;? /hich is a common operating s"stem. +" operating s"stem?
/e mean the suite of programs /hich make the computer /ork. U69; is used 8" the
/orkstations and multi1user ser$ers /ithin the school.
4n ; terminals and the /orkstations? ; .indo/s pro$ide a graphical interface 8et/een
the user and U69;. 5o/e$er? kno/ledge of U69; is reEuired for operations /hich aren@t
co$ered 8" a graphical program? or for /hen there is no ; /indo/s s"stem? for e'ample?
in a telnet session.
(0
*he 9><P operatin& s0stem
The U69; operating s"stem is made up of three parts\ the kernel? the shell and the
programs.
The kernel
The kernel of U69; is the hu8 of the operating s"stemK it allocates time and memor" to
programs and handles the filestore and communications in response to s"stem calls.
#s an illustration of the /a" that the shell and the kernel /ork together? suppose a user
t"pes rm myfile </hich has the effect of remo$ing the file m0fie=. The shell searches
the filestore for the file containing the program rm? and then reEuests the kernel? through
s"stem calls? to e'ecute the program rm on m"file. .hen the process rm myfile has
finished running? the shell then returns the U69; prompt T to the user? indicating that it
is /aiting for further commands.
The shell
The shell acts as an interface 8et/een the user and the kernel. .hen a user logs in? the
login program checks the username and pass/ord? and then starts another program called
the shell. The shell is a command line interpreter <CL9=. 9t interprets the commands the
user t"pes in and arranges for them to 8e carried out. The commands are themsel$es
programsK /hen the" terminate? the shell gi$es the user another prompt <T on our
s"stems=.
The adept user can customise hisOher o/n shell? and users can use different shells on the
same machine. Staff and students in the school ha$e the tcsh shell 8" default.
The tcsh shell has certain features to help the user inputting commands.
-ilename Completion 1 +" t"ping part of the name of a command? filename or director"
and pressing the HTa8I ke"? the tcsh shell /ill complete the rest of the name
automaticall". 9f the shell finds more than one name 8eginning /ith those letters "ou
ha$e t"ped? it /ill 8eep? prompting "ou to t"pe a fe/ more letters 8efore pressing the ta8
ke" again.
5istor" 1 The shell keeps a list of the commands "ou ha$e t"ped in. 9f "ou need to repeat
a command? use the cursor ke"s to scroll up and do/n the list or t"pe histor" for a list of
pre$ious commands.
(7
7ies and processes
&$er"thing in U69; is either a file or a process.
# process is an e'ecuting program identified 8" a uniEue P9 <process identifier=.
# file is a collection of data. The" are created 8" users using te't editors? running
compilers etc.
&'amples of filesK
a document <report? essa" etc.=
the te't of a program /ritten in some high1le$el programming language
instructions comprehensi8le directl" to the machine and incomprehensi8le to a
casual user? for e'ample? a collection of 8inar" digits <an e'ecuta8le or 8inar"
file=\
a director"? containing information a8out its contents? /hich ma" 8e a mi'ture of
other directories <su8directories= and ordinar" files.
*he "irector0 Structure
#ll the files are grouped together in the director" structure. The file1s"stem is arranged in
a hierarchical structure? like an in$erted tree. The top of the hierarch" is traditionall"
called root.
9n the diagram a8o$e? /e see that the director" ee5)a8 contains the su8director" uni'stuff
and a file proN.t't
Startin& an Ptermina session
(5
To start an ;term session? click on the Uni' Terminal icon on "our desktop? or from the
drop1do/n menus
#n ;terminal /indo/ /ill appear /ith a Uni' prompt? /aiting for "ou to start entering
commands.
(6
Part *ne
;#; Listin& fies and directories
ls 0list1
.hen "ou first login? "our current /orking director" is "our home director". Your home
director" has the same name as "our user1name? for e'ample? ee()a8? and it is /here
"our personal files and su8directories are sa$ed.
To find out /hat is in "our home director"? t"pe
T ls <short for list=
The ls command lists the contents of "our current /orking director".
There ma" 8e no files $isi8le in "our home director"? in /hich case? the U69; prompt
/ill 8e returned. #lternati$el"? there ma" alread" 8e some files inserted 8" the S"stem
#dministrator /hen "our account /as created.
ls does not? in fact? cause all the files in "our home director" to 8e listed? 8ut onl" those
ones /hose name does not 8egin /ith a dot <.= -iles 8eginning /ith a dot <.= are kno/n as
hidden files and usuall" contain important program configuration information. The" are
hidden 8ecause "ou should not change them unless "ou are $er" familiar /ith U69;YYY
To list all files in "our home director" including those /hose names 8egin /ith a dot?
t"pe
(!
T ls 1a
ls is an e'ample of a command /hich can take optionsK -a is an e'ample of an option.
The options change the 8eha$iour of the command. There are online manual pages that
tell "ou /hich options a particular command can take? and ho/ each option modifies the
8eha$iour of the command. <See later in this tutorial=
;#E (akin& "irectories
mkdir 0make directory1
.e /ill no/ make a su8director" in "our home director" to hold the files "ou /ill 8e
creating and using in the course of this tutorial. To make a su8director" called
unixstuff in "our current /orking director" t"pe
T mkdir uni'stuff
To see the director" "ou ha$e Nust created? t"pe
T ls
;#F Chan&in& to a different director0
cd 0change directory1
The command cd directory means change the current /orking director" to
@director"@. The current /orking director" ma" 8e thought of as the director" "ou are in?
i.e. "our current position in the file1s"stem tree.
To change to the director" "ou ha$e Nust made? t"pe
T cd uni'stuff
T"pe ls to see the contents </hich should 8e empt"=
!"ercise 2a
2ake another director" inside the unixstuff director" called 'ackups
;#N *he directories # and ##
Still in the unixstuff director"? t"pe
(%
T ls 1a
#s "ou can see? in the unixstuff director" <and in all other directories=? there are t/o
special directories called <#= and <##=
9n U69;? <#= means the current director"? so t"ping
T cd .
64T&K there is a space 8et/een cd and the dot
means sta" /here "ou are <the unixstuff director"=.
This ma" not seem $er" useful at first? 8ut using <#= as the name of the current director"
/ill sa$e a lot of t"ping? as /e shall see later in the tutorial.
<##= means the parent of the current director"? so t"ping
T cd ..
/ill take "ou one director" up the hierarch" <8ack to "our home director"=. Tr" it no/.
6oteK t"ping cd /ith no argument al/a"s returns "ou to "our home director". This is
$er" useful if "ou are lost in the file s"stem.
;#O )athnames
p&d 0print &orking directory1
Pathnames ena8le "ou to /ork out /here "ou are in relation to the /hole file1s"stem. -or
e'ample? to find out the a8solute pathname of "our home1director"? t"pe cd to get 8ack to
"our home1director" and then t"pe
T p/d
The full pathname /ill look something like this 1
,a,fserv',fserv',fserv'EE,ee'en&QQ,eeQ;a'
/hich means that ee()a8 <"our home director"= is in the director" ee8eng(( <the group
director"=?/hich is located on the fser$8 file1ser$er.
6oteK
((
,a,fserv',fserv',fserv'EE,ee'en&QQ,eeQ;a'
can 8e shortened to
,user,ee'en&QQ,eeQ;a'
!"ercise 2-
Use the commands ls? pwd and cd to e'plore the file s"stem.
<Remem8er? if "ou get lost? t"pe cd 8" itself to return to "our home1director"=
;#H (ore a'out home directories and pathnames
Understanding pathnames
-irst t"pe cd to get 8ack to "our home1director"? then t"pe
T ls uni'stuff
to list the conents of "our uni'stuff director".
6o/ t"pe
T ls 8ackups
You /ill get a message like this 1
8ackupsK 6o such file or director"
The reason is? 'ackups is not in "our current /orking director". To use a command on a
file <or director"= not in the current /orking director" <the director" "ou are currentl" in=?
"ou must either cd to the correct director"? or specif" its full pathname. To list the
contents of "our 8ackups director"? "ou must t"pe
T ls uni'stuffO8ackups
)**
3 0your home directory1
5ome directories can also 8e referred to 8" the tilde D character. 9t can 8e used to specif"
paths starting at "our home director". So t"ping
T ls >Ouni'stuff
/ill list the contents of "our uni'stuff director"? no matter /here "ou currentl" are in the
file s"stem.
.hat do "ou think
T ls >
/ould listB
.hat do "ou think
T ls >O..
/ould listB
Summar0
ls
list files and directories
ls -a
list all files and directories
mkdir
make a director"
cd directory
change to named director"
cd
change to home1director"
cd ~
change to home1director"
cd ..
change to parent director"
pwd
displa" the path of the current director"
)*)
Part T&o
E#; Cop0in& 7ies
cp 0copy1
cp file1 file2 is the command /hich makes a cop" of fie; in the current /orking
director" and calls it fieE
.hat /e are going to do no/? is to take a file stored in an open access area of the file
s"stem? and use the cp command to cop" it to "our uni'stuff director".
-irst? cd to "our uni'stuff director".
T cd >Ouni'stuff
Then at the U69; prompt? t"pe?
T cp O$olOe'amplesOtutorialOscience.t't .
<6oteK on@t forget the dot <.= at the end. Remem8er? in U69;? the dot means the current
director".=
The a8o$e command means cop" the file science#txt to the current director"? keeping the
name the same.
<6oteK The director" ,vo,exampes,tutoria, is an area to /hich e$er"one in the
department has read and cop" access. 9f "ou are from outside the Uni$ersit"? "ou can
gra8 a cop" of the file here. Use @-ileOSa$e #s..@ from the menu 8ar to sa$e it into "our
unixstuff director".=
!"ercise 4a
Create a 8ackup of "our science#txt file 8" cop"ing it to a file called science#'ak
E#E (ovin& fies
m 0moe1
mv file1 file2 mo$es <or renames= fie; to fieE
)*,
To mo$e a file from one place to another? use the mv command. This has the effect of
mo$ing rather than cop"ing the file? so "ou end up /ith onl" one file rather than t/o.
9t can also 8e used to rename a file? 8" mo$ing the file to the same director"? 8ut gi$ing it
a different name.
.e are no/ going to mo$e the file science.8ak to "our 8ackup director".
-irst? change directories to "our uni'stuff director" <can "ou remem8er ho/B=. Then?
inside the unixstuff director"? t"pe
T m$ science.8ak 8ackupsO.
T"pe ls and ls backups to see if it has /orked.
E#F 6emovin& fies and directories
rm 0remoe15 rmdir 0remoe directory1
To delete <remo$e= a file? use the rm command. #s an e'ample? /e are going to create a
cop" of the science#txt file then delete it.
9nside "our unixstuff director"? t"pe
T cp science.t't tempfile.t't
T ls <to check if it has created the file=
T rm tempfile.t't
T ls <to check if it has deleted the file=
You can use the rmdir command to remo$e a director" <make sure it is empt" first=. Tr"
to remo$e the 'ackups director". You /ill not 8e a8le to since U69; /ill not let "ou
remo$e a non1empt" director".
!"ercise 4-
Create a director" called tempstuff using mkdir ? then remo$e it using the rmdir
command.
)*0
E#N "ispa0in& the contents of a fie on the screen
clear 0clear screen1
+efore "ou start the ne't section? "ou ma" like to clear the terminal /indo/ of the
pre$ious commands so the output of the follo/ing commands can 8e clearl" understood.
#t the prompt? t"pe
T clear
This /ill clear all te't and lea$e "ou /ith the T prompt at the top of the /indo/.
cat 0concatenate1
The command cat can 8e used to displa" the contents of a file on the screen. T"peK
T cat science.t't
#s "ou can see? the file is longer than than the siFe of the /indo/? so it scrolls past
making it unreada8le.
less
The command less /rites the contents of a file onto the screen a page at a time. T"pe
T less science.t't
Press the [space-bar] if "ou /ant to see another page? t"pe [q] if "ou /ant to Euit
reading. #s "ou can see? less is used in preference to cat for long files.
head
The head command /rites the first ten lines of a file to the screen.
-irst clear the screen then t"pe
T head science.t't
)*7
Then t"pe
T head 15 science.t't
.hat difference did the 15 do to the head commandB
tail
The tail command /rites the last ten lines of a file to the screen.
Clear the screen and t"pe
T tail science.t't
5o/ can "ou $ie/ the last )5 lines of the fileB
E#O Searchin& the contents of a fie
Simple searching using less
Using less? "ou can search though a te't file for a ke"/ord <pattern=. -or e'ample? to
search through science#txt for the /ord @science@? t"pe
T less science.t't
then? still in less <i.e. don@t press HEI to Euit=? t"pe a for/ard slash [/] follo/ed 8" the
/ord to search
Oscience
#s "ou can see? less finds and highlights the ke"/ord. T"pe [n] to search for the ne't
occurrence of the /ord.
grep 0don6t ask &hy it is called grep1
rep is one of man" standard U69; utilities. 9t searches files for specified /ords or
patterns. -irst clear the screen? then t"pe
T grep science science.t't
)*5
#s "ou can see? rep has printed out each line containg the /ord science.
4r has itBBBB
Tr" t"ping
T grep Science science.t't
The rep command is case sensiti$e\ it distinguishes 8et/een Science and science.
To ignore upperOlo/er case distinctions? use the 1i option? i.e. t"pe
T grep 1i science science.t't
To search for a phrase or pattern? "ou must enclose it in single Euotes <the apostrophe
s"m8ol=. -or e'ample to search for spinning top? t"pe
T grep 1i @spinning top@ science.t't
Some of the other options of grep areK
1$ displa" those lines that do 64T match
1n precede each maching line /ith the line num8er
1c print onl" the total count of matched lines
Tr" some of them and see the different results. on@t forget? "ou can use more than one
option at a time? for e'ample? the num8er of lines /ithout the /ords science or Science is
T grep 1i$c science science.t't
&c 0&ord count1
# hand" little utilit" is the wc command? short for /ord count. To do a /ord count on
science#txt? t"pe
T /c 1/ science.t't
To find out ho/ man" lines the file has? t"pe
T /c 1l science.t't
)*6
Summar0
cp file1 file2
cop" file) and call it file,
mv file1 file2
mo$e or rename file) to file,
rm file
remo$e a file
rmdir directory
remo$e a director"
cat file
displa" a file
more file
displa" a file a page at a time
head file
displa" the first fe/ lines of a file
tail file
displa" the last fe/ lines of a file
rep 'keyword' file
search a file for ke"/ords
wc file
count num8er of linesO/ordsOcharacters in file
Part Three
F#; 6edirection
2ost processes initiated 8" U69; commands /rite to the standard output <that is? the"
/rite to the terminal screen=? and man" take their input from the standard input <that is?
the" read it from the ke"8oard=. There is also the standard error? /here processes /rite
their error messages? 8" default? to the terminal screen.
.e ha$e alread" seen one use of the cat command to /rite the contents of a file to the
screen.
6o/ t"pe cat /ithout specifing a file to read
T cat
Then t"pe a fe/ /ords on the ke"8oard and press the [!eturn] ke".
-inall" hold the [Ctrl] ke" do/n and press [d] </ritten as ] for short= to end the
input.
)*!
.hat has happenedB
9f "ou run the cat command /ithout specifing a file to read? it reads the standard input
<the ke"8oard=? and on recei$ing the@end of file@ <]=? copies it to the standard output <the
screen=.
9n U69;? /e can redirect 8oth the input and the output of commands.
F#E 6edirectin& the $utput
.e use the V s"m8ol to redirect the output of a command. -or e'ample? to create a file
called ist; containing a list of fruit? t"pe
T cat V list)
Then t"pe in the names of some fruit. Press [!eturn] after each one.
pear
8anana
apple
] <Control to stop=
.hat happens is the cat command reads the standard input <the ke"8oard= and the V
redirects the output? /hich normall" goes to the screen? into a file called ist;
To read the contents of the file? t"pe
T cat list)
!"ercise 7a
Using the a8o$e method? create another file called istE containing the follo/ing fruitK
orange? plum? mango? grapefruit. Read the contents of istE
The form VV appends standard output to a file. So to add more items to the file ist;? t"pe
T cat VV list)
Then t"pe in the names of more fruit
peach
grape
orange
] <Control to stop=
)*%
To read the contents of the file? t"pe
T cat list)
You should no/ ha$e t/o files. 4ne contains si' fruit? the other contains four fruit. .e
/ill no/ use the cat command to Noin <concatenate= ist; and istE into a ne/ file called
'i&ist. T"pe
T cat list) list, V 8iglist
.hat this is doing is reading the contents of ist; and istE in turn? then outputing the te't
to the file 'i&ist
To read the contents of the ne/ file? t"pe
T cat 8iglist
F#F 6edirectin& the <nput
.e use the Z s"m8ol to redirect the input of a command.
The command sort alpha8eticall" or numericall" sorts a list. T"pe
T sort
Then t"pe in the names of some $egeta8les. Press [!eturn] after each one.
carrot
8eetroot
artichoke
] <control d to stop=
The output /ill 8e
artichoke
8eetroot
carrot
Using Z "ou can redirect the input to come from a file rather than the ke"8oard. -or
e'ample? to sort the list of fruit? t"pe
T sort Z 8iglist
and the sorted list /ill 8e output to the screen.
To output the sorted list to a file? t"pe?
)*(
T sort Z 8iglist V slist
Use cat to read the contents of the file sist
F#N )ipes
To see /ho is on the s"stem /ith "ou? t"pe
T /ho
4ne method to get a sorted list of names is to t"pe?
T /ho V names.t't
T sort Z names.t't
This is a 8it slo/ and "ou ha$e to remem8er to remo$e the temporar" file called names
/hen "ou ha$e finished. .hat "ou reall" /ant to do is connect the output of the who
command directl" to the input of the sort command. This is e'actl" /hat pipes do. The
s"m8ol for a pipe is the $ertical 8ar W
-or e'ample? t"ping
T /ho W sort
/ill gi$e the same result as a8o$e? 8ut Euicker and cleaner.
To find out ho/ man" users are logged on? t"pe
T /ho W /c 1l
!"ercise 7-
a"ps -#hockney textfile is the command to print a postscript file to the printer
hockne".
Using pipes? print all lines of ist; and istE containing the letter @p@? sort the result? and
print to the printer hockne".
))*
Summar0
command $ file
redirect standard output to a file
command $$ file
append standard output to a file
command % file
redirect standard input from a file
command1 & command2
pipe the output of command) to the input of command,
cat file1 file2 $ file0
concatenate file) and file, to file*
sort
sort data
who
list users currentl" logged in
a"ps -#printer textfile
print te't file to named printer
lpr -#printer psfile
print postscript file to named printer
Part $our
N#; Ridcards
The characters 8 and 9
The character A is called a /ildcard? and /ill match against none or more character<s= in
a file <or director"= name. -or e'ample? in "our unixstuff director"? t"pe
T ls listA
This /ill list all files in the current director" starting /ith ist####
Tr" t"ping
T ls Alist
This /ill list all files in the current director" ending /ith ####ist
The character B /ill match e'actl" one character.
So ls 'ouse /ill match files like house and mouse? 8ut not &rouse.
Tr" t"ping
T ls Blist
)))
N#E 7iename conventions
.e should note here that a director" is merel" a special t"pe of file. So the rules and
con$entions for naming files appl" also to directories.
9n naming files? characters /ith special meanings such as , I . B ? should 8e a$oided.
#lso? a$oid using spaces /ithin names. The safest /a" to name a file is to use onl"
alphanumeric characters? that is? letters and num8ers? together /ith ^ <underscore= and .
<dot=.
-ile names con$entionall" start /ith a lo/er1case letter? and ma" end /ith a dot follo/ed
8" a group of letters indicating the contents of the file. -or e'ample? all files consisting of
C code ma" 8e named /ith the ending .c? for e'ample? prog).c . Then in order to list all
files containing C code in "our home director"? "ou need onl" t"pe ls (.c in that
director".
8ewareK some applications gi$e the same name to all the output files the" generate.
-or e'ample? some compilers? unless gi$en the appropriate option? produce compiled
files named a#out. Should "ou forget to use that option? "ou are ad$ised to rename the
compiled file immediatel"? other/ise the ne't such file /ill o$er/rite it and it /ill 8e
lost.
N#F :ettin& Hep
*n'line (anuals
There are on1line manuals /hich gi$es information a8out most commands. The manual
pages tell "ou /hich options a particular command can take? and ho/ each option
modifies the 8eha$iour of the command. T"pe man command to read the manual page for
a particular command.
-or e'ample? to find out more a8out the wc </ord count= command? t"pe
T man /c
#lternati$el"
T /hatis /c
gi$es a one1line description of the command? 8ut omits an" information a8out options
etc.
)),
Apropos
.hen "ou are not sure of the e'act name of a command?
T apropos ke"/ord
/ill gi$e "ou the commands /ith ke"/ord in their manual page header. -or e'ample? tr"
t"ping
T apropos cop"
Summar0
(
match an" num8er of characters
'
match one character
man command
read the online manual page for a command
whatis command
8rief description of a command
apropos keyword
match commands /ith ke"/ord in their man pages
Part $ie
O#; 7ie s0stem securit0 Saccess ri&htsT
9n "our uni'stuff director"? t"pe
T ls 1l <l for long listingY=
You /ill see that "ou no/ get lots of details a8out the contents of "our director"? similar
to the e'ample 8elo/.
))0
&ach file <and director"= has associated access rights? /hich ma" 8e found 8" t"ping ls
-l. #lso? ls -l gi$es additional information as to /hich group o/ns the file <8eng(5
in the follo/ing e'ample=K
1r/'r/1r11 ) ee5)a8 8eng(5 ,75* Sept,( ))K5, file)
9n the left1hand column is a )* s"m8ol string consisting of the s"m8ols d? r? /? '? 1? and?
occasionall"? s or S. 9f d is present? it /ill 8e at the left hand end of the string? and
indicates a director"K other/ise 1 /ill 8e the starting s"m8ol of the string.
The ( remaining s"m8ols indicate the permissions? or access rights? and are taken as three
groups of 0.
The left group of 0 gi$es the file permissions for the user that o/ns the file <or
director"= <ee5)a8 in the a8o$e e'ample=\
the middle group gi$es the permissions for the group of people to /hom the file
<or director"= 8elongs <ee8eng(5 in the a8o$e e'ample=\
the rightmost group gi$es the permissions for all others.
The s"m8ols r? /? etc.? ha$e slightl" different meanings depending on /hether the" refer
to a simple file or to a director".
Access rights on files:
r <or 1=? indicates read permission <or other/ise=? that is? the presence or a8sence
of permission to read and cop" the file
/ <or 1=? indicates /rite permission <or other/ise=? that is? the permission <or
other/ise= to change a file
' <or 1=? indicates e'ecution permission <or other/ise=? that is? the permission to
e'ecute a file? /here appropriate
))7
Access rights on directories:
r allo/s users to list files in the director"\
/ means that users ma" delete files from the director" or mo$e files into it\
' means the right to access files in the director". This implies that "ou ma" read
files in the director" pro$ided "ou ha$e read permission on the indi$idual files.
So? in order to read a file? "ou must ha$e e'ecute permission on the director" containing
that file? and hence on an" director" containing that director" as a su8director"? and so
on? up the tree.
))5
Some e"amples
-rwxrwxrwx
a file that e$er"one can read? /rite and e'ecute <and delete=.
-rw-------
a file that onl" the o/ner can read and /rite 1 no1one else
can read or /rite and no1one has e'ecution rights <e.g. "our
mail8o' file=.
O#E Chan&in& access ri&hts
chmod 0changing a file mode1
4nl" the o/ner of a file can use chmod to change the permissions of a file. The options
of chmod are as follo/s
S0m'o (eanin&
u user
g group
o other
a all
r read
/ /rite <and delete=
' e'ecute <and access director"=
: add permission
1 take a/a" permission
-or e'ample? to remo$e read /rite and e'ecute permissions on the file 'i&ist for the
group and others? t"pe
T chmod go1r/' 8iglist
This /ill lea$e the other permissions unaffected.
To gi$e read and /rite permissions on the file 'i&ist to all?
T chmod a:r/ 8iglist
))6
!"ercise ;a
Tr" changing access permissions on the file science#txt and on the director" 'ackups
Use ls -l to check that the permissions ha$e changed.
O#F )rocesses and Uo's
# process is an e'ecuting program identified 8" a uniEue P9 <process identifier=. To see
information a8out "our processes? /ith their associated P9 and status? t"pe
T ps
# process ma" 8e in the foreground? in the 8ackground? or 8e suspended. 9n general the
shell does not return the U69; prompt until the current process has finished e'ecuting.
Some processes take a long time to run and hold up the terminal. +ackgrounding a long
process has the effect that the U69; prompt is returned immediatel"? and other tasks can
8e carried out /hile the original process continues e'ecuting.
Running -ackground processes
To 8ackground a process? t"pe an . at the end of the command line. -or e'ample? the
command sleep /aits a gi$en num8er of seconds 8efore continuing. T"pe
T sleep )*
This /ill /ait )* seconds 8efore returning the command prompt T. Until the command
prompt is returned? "ou can do nothing e'cept /ait.
To run sleep in the 8ackground? t"pe
T sleep )* X
H)I 6,5(
The . runs the No8 in the 8ackground and returns the prompt straight a/a"? allo/ing "ou
do run other programs /hile /aiting for that one to finish.
The first line in the a8o$e e'ample is t"ped in 8" the user\ the ne't line? indicating No8
num8er and P9? is returned 8" the machine. The user is 8e notified of a No8 num8er
<num8ered from )= enclosed in sEuare 8rackets? together /ith a P9 and is notified /hen
a 8ackground process is finished. +ackgrounding is useful for No8s /hich /ill take a long
time to complete.
))!
#ackgrounding a current foreground process
#t the prompt? t"pe
T sleep )**
You can suspend the process running in the foreground 8" holding do/n the [control]
ke" and t"ping [)] </ritten as V2= Then to put it in the 8ackground? t"pe
T 8g
6oteK do not 8ackground programs that reEuire user interaction e.g. pine
O#N Listin& suspended and 'ack&round processes
.hen a process is running? 8ackgrounded or suspended? it /ill 8e entered onto a list
along /ith a No8 num8er. To e'amine this list? t"pe
T No8s
#n e'ample of a No8 list could 8e
H)I Suspended sleep )**
H,I Running netscape
H0I Running nedit
To restart <foreground= a suspended processes? t"pe
T fg TNo8num8er
-or e'ample? to restart sleep *++? t"pe
T fg T)
T"ping f /ith no No8 num8er foregrounds the last suspended process.
O#O Miin& a process
kill 0terminate or signal a process1
9t is sometimes necessar" to kill a process <for e'ample? /hen an e'ecuting program is in
an infinite loop=
To kill a No8 running in the foreground? t"pe ,C <control c=. -or e'ample? run
))%
T sleep )**
]C
To kill a suspended or 8ackground process? t"pe
T kill TNo8num8er
-or e'ample? run
T sleep )** X
T No8s
9f it is No8 num8er 7? t"pe
T kill T7
To check /hether this has /orked? e'amine the No8 list again to see if the process has
8een remo$ed.
ps 0process status1
#lternati$el"? processes can 8e killed 8" finding their process num8ers <P9s= and using
kill PID_number
T sleep )** X
T ps
P9 TT S T92& C422#6
,**!! ptsO5 S *K*5 sleep )**
,)560 ptsO5 T *K** netscape
,)%!0 ptsO5 S *K,5 nedit
To kill off the process sleep *++? t"pe
T kill ,**!!
and then t"pe ps again to see if it has 8een remo$ed from the list.
9f a process refuses to 8e killed? uses the -Q option? i.e. t"pe
T kill 1( ,**!!
6oteK 9t is not possi8le to kill off other users@ processes YYY
Summar0
))(
ls -la
list access rights for all files
chmod [options] file
change access rights for named file
command -
run command in 8ackground
,C
kill the No8 running in the foreground
,.
suspend the No8 running in the foreground
b
8ackground the suspended No8
/obs
list current No8s
f 0*
foreground No8 num8er )
kill 0*
kill No8 num8er )
ps
list current processes
kill "1*2"
kill process num8er ,6)5,
Part Si"
$ther usefu 9><P commands
<uota
#ll students are allocated a certain amount of disk space on the file s"stem for their
personal files? usuall" a8out )**28. 9f "ou go o$er "our Euota? "ou are gi$en ! da"s to
remo$e e'cess files.
To check "our current Euota and ho/ much of it "ou ha$e used? t"pe
T Euota 1$
df
The df command reports on the space left on the file s"stem. -or e'ample? to find out
ho/ much space is left on the fileser$er? t"pe
T df .
),*
du
The du command outputs the num8er of kilo8"es used 8" each su8director". Useful if
"ou ha$e gone o$er Euota and "ou /ant to find out /hich director" has the most files. 9n
"our home1director"? t"pe
T du
compress
This reduces the siFe of a file? thus freeing $alua8le disk space. -or e'ample? t"pe
T ls 1l science.t't
and note the siFe of the file. Then to compress science.t't? t"pe
T compress science.t't
This /ill compress the file and place it in a file called science.t't.U
To see the change in siFe? t"pe ls 1l again.
To uncomress the file? use the uncompress command.
T uncompress science.t't.U
g=ip
This also compresses a file? and is more efficient than compress. -or e'ample? to Fip
science.t't? t"pe
T gFip science.t't
This /ill Fip the file and place it in a file called science.t't.gF
To unFip the file? use the gunFip command.
T gunFip science.t't.gF
file
file classifies the named files according to the t"pe of data the" contain? for e'ample ascii
<te't=? pictures? compressed data? etc.. To report on all files in "our home director"? t"pe
T file A
),)
history
The C shell keeps an ordered list of all the commands that "ou ha$e entered. &ach
command is gi$en a num8er according to the order it /as entered.
T histor" <sho/ command histor" list=
9f "ou are using the C shell? "ou can use the e'clamation character <Y= to recall commands
easil".
T YY <recall last command=
T Y10 <recall third most recent command=
T Y5 <recall 5th command in list=
T Ygrep <recall last command starting /ith grep=
You can increase the siFe of the histor" 8uffer 8" t"ping
T set histor"`)**
Part Seen
W#; Compiin& 9><P software packa&es
.e ha$e man" pu8lic domain and commercial soft/are packages installed on our
s"stems? /hich are a$aila8le to all users. 5o/e$er? students are allo/ed to do/nload and
install small soft/are packages in their o/n home director"? soft/are usuall" onl" useful
to them personall".
There are a num8er of steps needed to install the soft/are.
Locate and do/nload the source code </hich is usuall" compressed=
Unpack the source code
Compile the code
9nstall the resulting e'ecuta8le
Set paths to the installation director"
4f the a8o$e steps? pro8a8l" the most difficult is the compilation stage.
),,
Compiling Source Code
#ll high1le$el language code must 8e con$erted into a form the computer understands.
-or e'ample? C language source code is con$erted into a lo/er1le$el language called
assem8l" language. The assem8l" language code made 8" the pre$ious stage is then
con$erted into o8Nect code /hich are fragments of code /hich the computer understands
directl". The final stage in compiling a program in$ol$es linking the o8Nect code to code
li8raries /hich contain certain 8uilt1in functions. This final stage produces an e'ecuta8le
program.
To do all these steps 8" hand is complicated and 8e"ond the capa8ilit" of the ordinar"
user. # num8er of utilities and tools ha$e 8een de$eloped for programmers and end1users
to simplif" these steps.
make and the (akefile
The make command allo/s programmers to manage large programs or groups of
programs. 9t aids in de$eloping large programs 8" keeping track of /hich portions of the
entire program ha$e 8een changed? compiling onl" those parts of the program /hich ha$e
changed since the last compile.
The make program gets its set of compile rules from a te't file called (akefie /hich
resides in the same director" as the source files. 9t contains information on ho/ to
compile the soft/are? e.g. the optimisation le$el? /hether to include de8ugging info in
the e'ecuta8le. 9t also contains information on /here to install the finished compiled
8inaries <e'ecuta8les=? manual pages? data files? dependent li8rar" files? configuration
files? etc.
Some packages reEuire "ou to edit the 2akefile 8" hand to set the final installation
director" and an" other parameters. 5o/e$er? man" packages are no/ 8eing distri8uted
/ith the 36U configure utilit".
configure
#s the num8er of U69; $ariants increased? it 8ecame harder to /rite programs /hich
could run on all $ariants. e$elopers freEuentl" did not ha$e access to e$er" s"stem? and
the characteristics of some s"stems changed from $ersion to $ersion. The 36U configure
and 8uild s"stem simplifies the 8uilding of programs distri8uted as source code. #ll
programs are 8uilt using a simple? standardised? t/o step process. The program 8uilder
need not install an" special tools in order to 8uild the program.
The confiure shell script attempts to guess correct $alues for $arious s"stem1
dependent $aria8les used during compilation. 9t uses those $alues to create a (akefie in
each director" of the package.
The simplest /a" to compile a package isK
),0
). cd to the director" containing the package@s source code.
,. T"pe ./confiure to configure the package for "our s"stem.
0. T"pe make to compile the package.
7. 4ptionall"? t"pe make check to run an" self1tests that come /ith the package.
5. T"pe make install to install the programs and an" data files and
documentation.
6. 4ptionall"? t"pe make clean to remo$e the program 8inaries and o8Nect files
from the source code director"
The configure utilit" supports a /ide $ariet" of options. You can usuall" use the
--help option to get a list of interesting options for a particular configure script.
The onl" generic options "ou are likel" to use are the --prefix and --exec-
prefix options. These options are used to specif" the installation directories.
The director" named 8" the --prefix option /ill hold machine independent files
such as documentation? data and configuration files.
The director" named 8" the --exec-prefix option? </hich is normall" a
su8director" of the 11prefi' director"=? /ill hold machine dependent files such as
e'ecuta8les.
W#E "ownoadin& source code
-or this e'ample? /e /ill do/nload a piece of free soft/are that con$erts 8et/een
different units of measurements.
-irst create a do/nload director"
T mkdir do/nload
o/nload the soft/are here and sa$e it to "our ne/ do/nload director".
W#F Extractin& the source code
3o into "our downoad director" and list the contents.
T cd do/nload
T ls 1l
#s "ou can see? the filename ends in tar.gF. The tar command turns se$eral files and
directories into one single tar file. This is then compressed using the )ip command <to
create a tar.gF file=.
),7
-irst unFip the file using the un)ip command. This /ill create a .tar file.
T gunFip units1).!7.tar.gF
Then e'tract the contents of the tar file.
T tar 1'$f units1).!7.tar
#gain? list the contents of the downoad director"? then go to the units-;#WN su81
director".
T cd units1).!7
W#N Confi&urin& and creatin& the (akefie
The first thing to do is carefull" read the 6E%"(E and <>S*%LL te't files <use the
less command=. These contain important information on ho/ to compile and run the
soft/are.
The units package uses the 36U configure s"stem to compile the source code. .e /ill
need to specif" the installation director"? since the default /ill 8e the main s"stem area
/hich "ou /ill not ha$e /rite permissions for. .e need to create an install director" in
"our home director".
T mkdir >Ounits)!7
Then run the configure utilit" setting the installation path to this.
T .Oconfigure 11prefi'`L542&Ounits)!7
64T&K
The $H$(E $aria8le is an e'ample of an en$ironment $aria8le.
The $alue of $H$(E is the path to "our home director". Cust t"pe
0 echo 34567
to sho/ the contents of this $aria8le. .e /ill learn more a8out en$ironment $aria8les in
a later chapter.
9f configure has run correctl"? it /ill ha$e created a 2akefile /ith all necessar" options.
You can $ie/ the 2akefile if "ou /ish <use the less command=? 8ut do not edit the
contents of this.
W#O 8uidin& the packa&e
),5
6o/ "ou can go ahead and 8uild the package 8" running the make command.
T make
#fter a minute or t/o <depending on the speed of the computer=? the e'ecuta8les /ill 8e
created. You can check to see e$er"thing compiled successfull" 8" t"ping
T make check
9f e$er"thing is oka"? "ou can no/ install the package.
T make install
This /ill install the files into the D,units;WN director" "ou created earlier.
W#H 6unnin& the software
You are no/ read" to run the soft/are <assuming e$er"thing /orked=.
T cd >Ounits)!7
9f "ou list the contents of the units director"? "ou /ill see a num8er of su8directories.
8in The 8inar" e'ecuta8les
info 36U info formatted documentation
man 2an pages
share Shared data files
To run the program? change to the 'in director" and t"pe
T .Ounits
#s an e'ample? con$ert 6 feet to metres.
You ha$eK 6 feet
You /antK metres
A ).%,%%
9f "ou get the ans/er ).%,%%? congratulations? it /orked.
),6
To $ie/ /hat units it can con$ert 8et/een? $ie/ the data file in the share director" <the
list is Euite comprehensi$e=.
To read the full documentation? change into the info director" and t"pe
T info 11file`units.info
W#W Strippin& unnecessar0 code
.hen a piece of soft/are is 8eing de$eloped? it is useful for the programmer to include
de8ugging information into the resulting e'ecuta8le. This /a"? if there are pro8lems
encountered /hen running the e'ecuta8le? the programmer can load the e'ecuta8le into a
de8ugging soft/are package and track do/n an" soft/are 8ugs.
This is useful for the programmer? 8ut unnecessar" for the user. .e can assume that the
package? once finished and a$aila8le for do/nload has alread" 8een tested and de8ugged.
5o/e$er? /hen /e compiled the soft/are a8o$e? de8ugging information /as still
compiled into the final e'ecuta8le. Since it is unlike" that /e are going to need this
de8ugging information? /e can strip it out of the final e'ecuta8le. 4ne of the ad$antages
of this is a much smaller e'ecuta8le? /hich should run slightl" faster.
.hat /e are going to do is look at the 8efore and after siFe of the 8inar" file. -irst change
into the 'in director" of the units installation director".
T cd >Ounits)!7O8in
T ls 1l
#s "ou can see? the file is o$er )** k8"tes in siFe. You can get more information on the
t"pe of file 8" using the file command.
T file units
unitsK &L- 0,18it LS+ e'ecuta8le? 9ntel %*0%6? $ersion )? d"namicall" linked <uses
shared li8s=? not stripped
To strip all the de8ug and line num8ering information out of the 8inar" file? use the
strip command
T strip units
T ls 1l
#s "ou can see? the file is no/ 06 k8"tes 1 a third of its original siFe. T/o thirds of the
8inar" file /as de8ug code YYY
),!
Check the file information again.
T file units
unitsK &L- 0,18it LS+ e'ecuta8le? 9ntel %*0%6? $ersion )? d"namicall" linked <uses
shared li8s=? stripped
596TK You can use the make command to install pre1stripped copies of all the 8inar"
files /hen "ou install the package.
9nstead of t"ping make install? simpl" t"pe make install-strip
Part !ight
G#; 9><P Jaria'es
Varia8les are a /a" of passing information from the shell to programs /hen "ou run
them. Programs look _in the en$ironment_ for particular $aria8les and if the" are found
/ill use the $alues stored. Some are set 8" the s"stem? others 8" "ou? "et others 8" the
shell? or an" program that loads another program.
Standard U69; $aria8les are split into t/o categories? en$ironment $aria8les and shell
$aria8les. 9n 8road terms? shell $aria8les appl" onl" to the current instance of the shell
and are used to set short1term /orking conditions\ en$ironment $aria8les ha$e a farther
reaching significance? and those set at login are $alid for the duration of the session. +"
con$ention? en$ironment $aria8les ha$e UPP&R C#S& and shell $aria8les ha$e lo/er
case names.
G#E Environment Jaria'es
#n e'ample of an en$ironment $aria8le is the 4STYP& $aria8le. The $alue of this is the
current operating s"stem "ou are using. T"pe
T echo L4STYP&
2ore e'amples of en$ironment $aria8les are
US&R <"our login name=
542& <the path name of "our home director"=
54ST <the name of the computer "ou are using=
#RC5 <the architecture of the computers processor=
9SPL#Y <the name of the computer screen to displa" ; /indo/s=
PR96T&R <the default printer to send print No8s=
P#T5 <the directories the shell should search to find a command=
),%
$inding out the current alues of these aria-les:
&6V9R462&6T $aria8les are set using the setenv command? displa"ed using the
printenv or env commands? and unset using the unsetenv command.
To sho/ all $alues of these $aria8les? t"pe
T printen$ W less
G#F She Jaria'es
#n e'ample of a shell $aria8le is the histor" $aria8le. The $alue of this is ho/ man" shell
commands to sa$e? allo/ the user to scroll 8ack through all the commands the" ha$e
pre$iousl" entered. T"pe
T echo Lhistor"
2ore e'amples of shell $aria8les are
c/d <"our current /orking director"=
home <the path name of "our home director"=
path <the directories the shell should search to find a command=
prompt <the te't string used to prompt for interacti$e commands shell "our login
shell=
$inding out the current alues of these aria-les:
S5&LL $aria8les are 8oth set and displa"ed using the set command. The" can 8e unset
8" using the unset command.
To sho/ all $alues of these $aria8les? t"pe
T set W less
So &hat is the difference -et&een PAT+ and path 9
9n general? en$ironment and shell $aria8les that ha$e the same name <apart from the case=
are distinct and independent? e'cept for possi8l" ha$ing the same initial $alues. There
are? ho/e$er? e'ceptions.
&ach time the shell $aria8les home? user and term are changed? the corresponding
en$ironment $aria8les 542&? US&R and T&R2 recei$e the same $alues. 5o/e$er?
altering the en$ironment $aria8les has no effect on the corresponding shell $aria8les.
),(
P#T5 and path specif" directories to search for commands and programs. +oth $aria8les
al/a"s represent the same director" list? and altering either automaticall" causes the other
to 8e changed.
G#N 9sin& and settin& varia'es
&ach time "ou login to a U69; host? the s"stem looks in "our home director" for
initialisation files. 9nformation in these files is used to set up "our /orking en$ironment.
The C and TC shells uses t/o files called .login and .cshrc <note that 8oth file names
8egin /ith a dot=.
#t login the C shell first reads #cshrc follo/ed 8" #o&in
#o&in is to set conditions /hich /ill appl" to the /hole session and to perform actions
that are rele$ant onl" at login.
#cshrc is used to set conditions and perform actions specific to the shell and to each
in$ocation of it.
The guidelines are to set &6V9R462&6T $aria8les in the #o&in file and S5&LL
$aria8les in the #cshrc file.
R%6><>:3 6&V&R put commands that run graphical displa"s <e.g. a /e8 8ro/ser= in
"our .cshrc or .login file.
G#O Settin& she varia'es in the #cshrc fie
-or e'ample? to change the num8er of shell commands sa$ed in the histor" list? "ou need
to set the shell $aria8le histor". 9t is set to )** 8" default? 8ut "ou can increase this if "ou
/ish.
T set histor" ` ,**
Check this has /orked 8" t"ping
T echo Lhistor"
5o/e$er? this has onl" set the $aria8le for the lifetime of the current shell. 9f "ou open a
ne/ 'term /indo/? it /ill onl" ha$e the default histor" $alue set. To P&R2#6&6TLY
set the $alue of histor"? "ou /ill need to add the set command to the .cshrc file.
-irst open the #cshrc file in a te't editor. #n eas"? user1friendl" editor to use is nedit.
T nedit >O.cshrc
)0*
#dd the follo/ing line #-T&R the list of other commands.
set histor" ` ,**
Sa$e the file and force the shell to reread its .cshrc file 8u" using the shell source
command.
T source .cshrc
Check this has /orked 8" t"ping
T echo Lhistor"
G#H Settin& the path
.hen "ou t"pe a command? "our path <or P#T5= $aria8le defines in /hich directories
the shell /ill look to find the command "ou t"ped. 9f the s"stem returns a message sa"ing
_commandK Command not found_? this indicates that either the command doesn@t e'ist at
all on the s"stem or it is simpl" not in "our path.
-or e'ample? to run units? "ou either need to directl" specif" the units path
<D,units;WN,'in,units=? or "ou need to ha$e the director" D,units;WN,'in in "our path.
You can add it to the end of "our e'isting path <the $path represents this= 8" issuing the
commandK
T set path ` <Lpath >Ounits)!7O8in=
Test that this /orked 8" tr"ing to run units in an" director" other that /here units is
actuall" located.
T cd\ units
H<>*3 You can run multiple commands on one line 8" separating them /ith a
semicolon.
To add this path P&R2#6&6TLY? add the follo/ing line to "our .cshrc #-T&R the list
of other commands.
set path ` <Lpath >Ounits)!7O8in=
)0)
Uni" ' $re<uently Asked >uestions 021 ?$re<uent
posting@
These articles are di$ided appro'imatel" as follo/sK
).A= 3eneral Euestions.
,.A= Relati$el" 8asic Euestions? likel" to 8e asked 8" 8eginners.
0.A= 9ntermediate Euestions.
7.A= #d$anced Euestions? likel" to 8e asked 8" people /ho thought
the" alread" kne/ all of the ans/ers.
5.A= Guestions pertaining to the $arious shells? and the differences.
This article includes ans/ers toK
).)= .ho helped "ou put this list togetherB
).,= .hen someone refers to [rn<)=J or [ctime<0=J? /hat does the num8er in parentheses
meanB
).0= .hat does dsome strange uni' command namee stand forB
).7= 5o/ does the gate/a" 8et/een Rcomp.uni'.EuestionsS and the Rinfo1uni'S mailing list
/orkB
).5= .hat are some useful Uni' or C 8ooksB
).6= .hat happened to the pronunciation list that used to 8e part of this documentB
9f "ouJre looking for the ans/er to? sa"? Euestion ).5? and /ant to skip e$er"thing else? "ou can
search ahead for the regular e'pression R]).5=S.
.hile these are all legitimate Euestions? the" seem to crop up in comp.uni'.Euestions or
comp.uni'.shell on an annual 8asis? usuall" follo/ed 8" plent" of replies <onl" some of /hich are
correct= and then a period of griping a8out ho/ the same Euestions keep coming up. You ma"
also like to read the monthl" article R#ns/ers to -reEuentl" #sked GuestionsS in the ne/sgroup
Rne/s.announce.ne/usersS? /hich /ill tell "ou /hat RU69;S stands for.
.ith the $ariet" of Uni' s"stems in the /orld? itJs hard to guarantee that these ans/ers /ill /ork
e$er"/here. Read "our local manual pages 8efore tr"ing an"thing suggested here. 9f "ou ha$e
suggestions or corrections for an" of these ans/ers? please send them to to tmatimaraisgtec.com.
).,= .hen someone refers to [rn<)=J or [ctime<0=J? /hat does
the num8er in parentheses meanB
9t looks like some sort of function call? 8ut it isnJt. These num8ers refer to the section of the
RUni' manualS /here the appropriate documentation can 8e found. You could t"pe Rman 0
ctimeS to look up the manual page for RctimeS in section 0 of the manual.
The traditional manual sections areK
) User1le$el commands
)0,
, S"stem calls
0 Li8rar" functions
7 e$ices and de$ice dri$ers
5 -ile formats
6 3ames
! Various miscellaneous stuff 1 macro packages etc.
% S"stem maintenance and operation commands
Some Uni' $ersions use non1numeric section names. -or instance? ;eni' uses RCS for
commands and RSS for functions. Some ne/er $ersions of Uni' reEuire Rman 1sM titleS
instead of Rman M titleS.
&ach section has an introduction? /hich "ou can read /ith Rman M introS /here M is the
section num8er.
Sometimes the num8er is necessar" to differentiate 8et/een a command and a li8rar" routine
or s"stem call of the same name. -or instance? "our s"stem ma" ha$e Rtime<)=S? a manual
page a8out the [timeJ command for timing programs? and also Rtime<0=S? a manual page
a8out the [timeJ su8routine for determining the current time. You can use Rman ) timeS or
Rman 0 timeS to specif" /hich RtimeS man page "ouJre interested in.
YouJll often find other sections for local programs or e$en su8sections of the sections a8o$e 1
Ultri' has sections 0m? 0n? 0' and 0"p among others.
).0= .hat does dsome strange uni' command namee stand forB
a/k ` R#ho .ein8erger and DernighanS
This language /as named 8" its authors? #l #ho? Peter .ein8erger and +rian Dernighan.
grep ` R3lo8al Regular &'pression PrintS
grep comes from the ed command to print all lines matching a
certain pattern
gOreOp
/here RreS is a Rregular e'pressionS.
fgrep ` R-i'ed 3R&PS.
fgrep searches for fi'ed strings onl". The RfS does not stand for RfastS 1 in fact? Rfgrep foo8ar
A.cS is usuall" slo/er than Regrep foo8ar A.cS <Yes? this is kind of surprising. Tr" it.=
-grep still has its uses though? and ma" 8e useful /hen searching a file for a larger num8er of
strings than egrep can handle.
egrep ` R&'tended 3R&PS
egrep uses fancier regular e'pressions than grep. 2an" people use egrep all the time? since it
has some more sophisticated internal algorithms than grep or fgrep? and is usuall" the fastest
of the three programs.
cat ` RC#TenateS
)00
catenate is an o8scure /ord meaning Rto connect in a seriesS? /hich is /hat the RcatS
command does to one or more files. 6ot to 8e confused /ith CO#OT? the Computer #ided
T"pesetter.
gecos ` R3eneral &lectric Comprehensi$e 4perating Super$isorS
.hen 3&Js large s"stems di$ision /as sold to 5one"/ell? 5one"/ell dropped the R&S from
R3&C4SS.
Uni'Js pass/ord file has a Rp/^gecosS field. The name is a real holdo$er from the earl"
da"s. ennis Ritchie has reportedK
RSometimes /e sent printer output or 8atch No8s to the 3C4S machine. The gcos field in the
pass/ord file /as a place to stash the information for the L9&6T card. 6ot elegant.S
nroff ` R6e/ R4--S
troff ` RT"pesetter ne/ R4--S
These are descendants of RroffS? /hich /as a re1implementation of the 2ultics RrunoffS
program <a program that "ouJd use to Rrun offS a good cop" of a document=.
tee ` T
-rom plum8ing terminolog" for a T1shaped pipe splitter.
8ss ` R+lock Started 8" S"m8olS
ennis Ritchie sa"sK
#ctuall" the acron"m <in the sense /e took it up\ it ma" ha$e other credi8le et"mologies= is
R+lock Started 8" S"m8ol.S 9t /as a pseudo1op in -#P <-ortran #ssem8l" H1erBI Program=?
an assem8ler for the 9+2 !*71!*(1!*(*1!*(7 machines. 9t defined its la8el and set aside
space for a gi$en num8er of /ords. There /as another pseudo1op? +&S? R+lock &nded 8"
S"m8olS that did the same e'cept that the la8el /as defined 8" the last assigned /ord : ).
<4n these machines -ortran arra"s /ere stored 8ack/ards in storage and /ere )1origin.=
The usage is reasona8l" appropriate? 8ecause Nust as /ith standard Uni' loaders? the space
assigned didnJt ha$e to 8e punched literall" into the o8Nect deck 8ut /as represented 8" a
count some/here.
8iff ` R+9--S
This command? /hich turns on as"nchronous mail notification? /as actuall" named after a
dog at +erkele".
9 can confirm the origin of 8iff? if "ouJre interested. +iff /as 5eidi StettnerJs dog? 8ack
/hen 5eidi <and 9? and +ill Co"= /ere all grad students at U.C. +erkele" and the earl"
$ersions of +S /ere 8eing de$eloped. +iff /as popular among the residents of &$ans 5all?
and /as kno/n for 8arking at the mailman? hence the name of the command.
Confirmation courtes" of &ric Cooper? Carnegie 2ellon Uni$ersit"
rc <as in R.cshrcS or ROetcOrcS= ` RRunComS
RrcS deri$es from RruncomS? from the 29T CTSS s"stem? ca. )(65.
[There /as a facilit" that /ould e'ecute a 8unch of commands stored in a file\ it /as called
RruncomS for Rrun commandsS? and the file 8egan to 8e called Ra runcom.S
RrcS in Uni' is a fossil from that usage.J
)07
+rian Dernighan X ennis Ritchie? as told to Vicki +ro/n
RrcS is also the name of the shell from the ne/ Plan ( operating s"stem.
Perl ` RPractical &'traction and Report LanguageS
Perl ` RPathologicall" &clectic Ru88ish ListerS
The Perl language is Larr" .allJs highl" popular
freel"1a$aila8le completel" porta8le te't? process? and file
manipulation tool that 8ridges the gap 8et/een shell and C
programming <or 8et/een doing it on the command line and
pulling "our hair out=. -or further information? see the
Usenet ne/sgroup comp.lang.perl.misc.
on Li8esJ 8ook RLife /ith Uni'S contains lots more of these tid8its.
).7= 5o/ does the gate/a" 8et/een Rcomp.uni'.EuestionsS and the
Rinfo1uni'S mailing list /orkB
Rinfo1uni'S and Runi'1/iFardsS are mailing list $ersions of comp.uni'.Euestions and
comp.uni'./iFards respecti$el". There should 8e no difference in content 8et/een the
mailing list and the ne/sgroup.
To get on or off either of these lists? send mail to
info1uni'1reEuesta8rl.mil or uni'1/iFards1reEuesta8rl.mil.
+e sure to use the [1ReEuestJ. onJt e'pect an immediate response.
5ere are the gor" details? courtes" of the listJs maintainer? +o8 Reschl".
```` postings to info1U69; and U69;1/iFards lists ````
#n"thing su8mitted to the list is posted\ 9 do not moderate incoming trafficP+RL functions
as a reflector. Postings su8mitted 8" 9nternet su8scri8ers should 8e addressed to the list
address <info1U69; or U69;1 /iFards=\ the [1reEuestJ addresses are for correspondence /ith
the list maintainer HmeI. Postings su8mitted 8" US&6&T readers should 8e addressed to the
appropriate ne/s group <comp.uni'.Euestions or comp.uni'./iFards=.
-or 9nternet su8scri8ers? recei$ed traffic /ill 8e of t/o t"pes\ indi$idual messages? and
digests. Traffic /hich comes to +RL from the 9nternet and +9T6&T <$ia the +9T6&T1
9nternet gate/a"= is immediatel" resent to all addressees on the mailing list. Traffic
originating on US&6&T is gathered up into digests /hich are sent to all list mem8ers dail".
+9T6&T traffic is much like 9nternet traffic. The main difference is that 9 maintain onl" one
address for traffic destined to all +9T6&T su8scri8ers. That address points to a list e'ploder
/hich then sends copies to indi$idual +9T6&T su8scri8ers. This /a" onl" one cop" of a
gi$en message has to cross the +9T6&T19nternet gate/a" in either direction.
US&6&T su8scri8ers see onl" indi$idual messages. #ll messages originating on the 9nternet
side are for/arded to our US&6&T machine. The" are then posted to the appropriate
ne/sgroup. Unfortunatel"? for gate/a"ed messages? the sender 8ecomes Rne/sa8rl1admS.
This is currentl" an una$oida8le side1effect of the soft/are /hich performs the gate/a"
function.
#s for readership? US&6&T has an e'tremel" large readership 1 9 /ould guess se$eral
thousand hosts and tens of thousands of readers. The master list maintained here at +RL runs
)05
a8out t/o hundred fift" entries /ith roughl" ten percent of those 8eing local redistri8ution
lists. 9 donJt ha$e a good feel for the siFe of the +9T6&T redistri8ution? 8ut 9 /ould guess it
is roughl" the same siFe and composition as the master list. Traffic runs )5*D to 7**D 8"tes
per list per /eek on a$erage.
).5= .hat are some useful Uni' or C 8ooksB
2itch .right <mitchacirrus.com= maintains a useful list of Uni' and C 8ooks? /ith
descriptions and some mini1re$ie/s. There are currentl" )6! titles on his list.
You can o8tain a cop" of this list 8" anon"mous ftp from ftp.rahul.net <)(,.)6*.)0.)=? /here
itJs Rpu8OmitchOY#+LO"a8lS. Send additions or suggestions to mitchacirrus.com.
Samuel Do <kkoasfu.ca= maintains another list of Uni' 8ooks. This list contains onl"
recommended 8ooks? and is therefore some/hat shorter. This list is also a classified list? /ith
8ooks grouped into categories? /hich ma" 8e 8etter if "ou are looking for a specific t"pe of
8ook.
You can o8tain a cop" of this list 8" anon"mous ftp from rtfm.mit.edu? /here itJs
Rpu8OusenetOne/s.ans/ersO8ooksOuni'S. Send additions or suggestions to kkoasfu.ca.
9f "ou canJt use anon"mous ftp? email the line RhelpS to Rftpmailadec/rl.dec.comS for
instructions on retrie$ing things $ia email.
).6= .hat happened to the pronunciation list that used to 8e part of this
documentB
-rom its inception in )(%(? this -#G document included a
comprehensi$e pronunciation list maintained 8" 2aarten Litmaath
<thanks? 2aartenY=. 9t /as originall" created 8" Carl Paukstis
Zcarlpafrigg.isc18r.comV.
9t has 8een retired? since it is not reall" rele$ant to the topic of RUni' EuestionsS. You can
still find it as part of the /idel"1distri8uted RCargonS file <maintained 8" &ric S. Ra"mond?
ericasnark.th"rsus.com= /hich seems like a much more appropriate forum for the topic of
R5o/ do "ou pronounce OA BS
9f "ouJd like a cop"? "ou can ftp one from ftp./g.omron.co.Np <)00.,)*.7.7=? itJs Rpu8Ouni'1
faEOdocsOPronunciation13uideS.
)06
Uni" ' $re<uently Asked >uestions 041 ?$re<uent
posting@
This article includes ans/ers toK
,.)= 5o/ do 9 remo$e a file /hose name 8egins /ith a R1R B
,.,= 5o/ do 9 remo$e a file /ith funn" characters in the filename B
,.0= 5o/ do 9 get a recursi$e director" listingB
,.7= 5o/ do 9 get the current director" into m" promptB
,.5= 5o/ do 9 read characters from the terminal in a shell scriptB
,.6= 5o/ do 9 rename RA.fooS to RA.8arS? or change file names to lo/ercaseB
,.!= .h" do 9 get Hsome strange error messageI /hen 9 Rrsh host commandS B
,.%= 5o/ do 9 dset an en$ironment $aria8le? change director"e inside a program or shell
script and ha$e that change affect m" current shellB
,.(= 5o/ do 9 redirect stdout and stderr separatel" in cshB
,.)*= 5o/ do 9 tell inside .cshrc if 9Jm a login shellB
,.))= 5o/ do 9 construct a shell glo81pattern that matches all files e'cept R.S and R..S B
,.),= 5o/ do 9 find the last argument in a +ourne shell scriptB
,.)0= .hatJs /rong /ith ha$ing [.J in "our LP#T5 B
,.)7= 5o/ do 9 ring the terminal 8ell during a shell scriptB
,.)5= .h" canJt 9 use RtalkS to talk /ith m" friend on machine ;B
,.)6= .h" does calendar produce the /rong outputB
9f "ouJre looking for the ans/er to? sa"? Euestion ,.5? and /ant to skip e$er"thing else? "ou can
search ahead for the regular e'pression R],.5=S.
.hile these are all legitimate Euestions? the" seem to crop up in comp.uni'.Euestions or
comp.uni'.shell on an annual 8asis? usuall" follo/ed 8" plent" of replies <onl" some of /hich are
correct= and then a period of griping a8out ho/ the same Euestions keep coming up. You ma"
also like to read the monthl" article R#ns/ers to -reEuentl" #sked GuestionsS in the ne/sgroup
Rne/s.announce.ne/usersS? /hich /ill tell "ou /hat RU69;S stands for.
.ith the $ariet" of Uni' s"stems in the /orld? itJs hard to guarantee that these ans/ers /ill /ork
e$er"/here. Read "our local manual pages 8efore tr"ing an"thing suggested here. 9f "ou ha$e
suggestions or corrections for an" of these ans/ers? please send them to to tmatimaraisgtec.com.
,.)= 5o/ do 9 remo$e a file /hose name 8egins /ith a R1R B
-igure out some /a" to name the file so that it doesnJt 8egin
/ith a dash. The simplest ans/er is to use
rm .O1filename
)0!
<assuming R1filenameS is in the current director"? of course.= This method of a$oiding the
interpretation of the R1R /orks /ith other commands too.
2an" commands? particularl" those that ha$e 8een /ritten to use the Rgetopt<0=S argument
parsing routine? accept a RPR argument /hich means Rthis is the last option? an"thing after
this is not an optionS? so "our $ersion of rm might handle Rrm 11 1filenameS. Some $ersions
of rm that donJt use getopt<= treat a single R1R in the same /a"? so "ou can also tr" Rrm 1
1filenameS.
,.,= 5o/ do 9 remo$e a file /ith funn" characters in the filename B
9f the [funn" characterJ is a [OJ? skip to the last part of this ans/er. 9f the funn" character is
something else? such as a [ [ or control character or character /ith the %
th
8it set? keep reading.
The classic ans/ers are
rm 1i someApatternAthatAmatchesAonl"AtheAfileA"ouA/ant
/hich asks "ou /hether "ou /ant to remo$e each file matching the indicated pattern\
depending on "our shell? this ma" not /ork if the filename has a character /ith the %
th
8it set
<the shell ma" strip that off=\
and
rm 1ri .
/hich asks "ou /hether to remo$e each file in the director".
#ns/er R"S to the pro8lem file and RnS to e$er"thing else. Unfortunatel" this doesnJt /ork
/ith man" $ersions of rm. #lso unfortunatel"? this /ill /alk through e$er" su8director" of
R.S? so "ou might /ant to Rchmod a1'S those directories temporaril" to make them
unsearcha8le.
#l/a"s take a deep 8reath and think a8out /hat "ouJre doing and dou8le check /hat "ou
t"ped /hen "ou use rmJs R1rS flag or a /ildcard on the command line\
and
find . 1t"pe f ... 1ok rm [deJ f\
/here R...S is a group of predicates that uniEuel" identif" the
file. 4ne possi8ilit" is to figure out the inode num8er of the
pro8lem file <use Rls 1i .S= and then use
find . 1inum ),075 1ok rm [deJ f\
or
find . 1inum ),075 1ok m$ [deJ ne/1file1name f\
R1okS is a safet" check 1 it /ill prompt "ou for confirmation of the command itJs a8out to
e'ecute. You can use R1e'ecS instead to a$oid the prompting? if "ou /ant to li$e
dangerousl"? or if "ou suspect that the filename ma" contain a funn" character seEuence that
/ill mess up "our screen /hen printed.
.hat if the filename has a [OJ in itB
These files reall" are special cases? and can onl" 8e created 8" 8ugg" kernel code <t"picall"
8" implementations of 6-S that donJt filter out illegal characters in file names from remote
machines.= The first thing to do is to tr" to understand e'actl" /h" this pro8lem is so
strange.
)0%
Recall that Uni' directories are simpl" pairs of filenames and inode num8ers. # director"
essentiall" contains information like thisK
filename inode
file) ),075
file,.c ),07(
file0 ),07!
Theoreticall"? [OJ and [f*J are the onl" t/o characters that cannot appear in a filename 1 [OJ
8ecause itJs used to separate directories and files? and [f*J 8ecause it terminates a filename.
Unfortunatel" some implementations of 6-S /ill 8lithel" create filenames /ith em8edded
slashes in response to reEuests from remote machines. -or instance? this could happen /hen
someone on a 2ac or other non1Uni' machine decides to create a remote 6-S file on "our
Uni' machine /ith the date in the filename. Your Uni' director" then has this in itK
filename inode
()O*,O*! ),05!
6o amount of messing around /ith [findJ or [rmJ as descri8ed a8o$e /ill delete this file?
since those utilities and all other Uni' programs? are forced to interpret the [OJ in the normal
/a".
#n" ordinar" program /ill e$entuall" tr" to do unlink<R()O*,O*!S=? /hich as far as the kernel
is concerned means Runlink the file *! in the su8director" *, of director" ()S? 8ut thatJs not
/hat /e ha$e 1 /e ha$e a 7<LE named R()O*,O*!S in the current director". This is a su8tle
8ut crucial distinction.
.hat can "ou do in this caseB The first thing to tr" is to return to the 2ac that created this
crumm" entr"? and see if "ou can con$ince it and "our local 6-S daemon to rename the file
to something /ithout slashes.
9f that doesnJt /ork or isnJt possi8le? "ouJll need help from "our s"stem manager? /ho /ill
ha$e to tr" the one of the follo/ing. Use Rls 1iS to find the inode num8er of this 8ogus file?
then unmount the file s"stem and use RclriS to clear the inode? and RfsckS the file s"stem /ith
"our fingers crossed. This destro"s the information in the file. 9f "ou /ant to keep it? "ou
can tr"K
create a ne/ director" in the same parent director" as the one containing the 8ad file name\
mo$e e$er"thing "ou can <i.e. e$er"thing 8ut the file /ith the 8ad name= from the old
director" to the ne/ one\
do Rls 1idS on the director" containing the file /ith the 8ad name to get its inum8er\
umount the file s"stem\
RclriS the director" containing the file /ith the 8ad name\
RfsckS the file s"stem.
Then? to find the file?
remount the file s"stem\
rename the director" "ou created to ha$e the name of the old director" <since the old
director" should ha$e 8een 8lo/n a/a" 8" RfsckS=
mo$e the file out of Rlost:foundS into the director" /ith a 8etter name.
)0(
#lternati$el"? "ou can patch the director" the hard /a" 8" cra/ling around in the ra/ file
s"stem. Use Rfsd8S? if "ou ha$e it.
,.0= 5o/ do 9 get a recursi$e director" listingB
4ne of the follo/ing ma" do /hat "ou /antK
ls 1R <not all $ersions of RlsS ha$e 1R=
find . 1print <should /ork e$er"/here=
du 1a . <sho/s "ou 8oth the name and siFe=
9f "ouJre looking for a /ildcard pattern that /ill match all R.cS
files in this director" and 8elo/? "ou /onJt find one? 8ut "ou
can use
T some1command [find . 1name [A.cJ 1print[
RfindS is a po/erful program. Learn a8out it.
,.7= 5o/ do 9 get the current director" into m" promptB
9t depends /hich shell "ou are using. 9tJs eas" /ith some shells? hard or impossi8le /ith
others.
C Shell <csh=K
Put this in "our .cshrc 1 customiFe the prompt $aria8le the /a" "ou /ant.
alias setprompt [set prompt`SLdc/deT RJ
setprompt M to set the initial prompt
alias cd [chdir fYA XX setpromptJ
9f "ou use pushd and popd? "ouJll also need
alias pushd [pushd fYA XX setpromptJ
alias popd [popd fYA XX setpromptJ
Some C shells donJt keep a Lc/d $aria8le 1 "ou can use [p/d[ instead.
9f "ou Nust /ant the last component of the current director"
in "our prompt <RmailT R instead of ROusrOspoolOmailT R=
"ou can use
alias setprompt [set prompt`SLc/dKtT RJ
Some older cshJs get the meaning of XX and WW re$ersed.
Tr" doingK
false XX echo 8ug
9f it prints R8ugS? "ou need to s/itch XX and WW <and get a 8etter $ersion of csh.=
+ourne Shell <sh=K
9f "ou ha$e a ne/er $ersion of the +ourne Shell <SVR, or ne/er= "ou can use a shell
function to make "our o/n command? R'cdS sa"K
)7*
'cd<= d cd LA \ PS)`S[p/d[ L R\ e
9f "ou ha$e an older +ourne shell? itJs complicated 8ut not impossi8le. 5ereJs one /a". #dd
this to "our .profile fileK
L4396^S5&LL`LL e'port L4396^S5&LL
C2-9L&`OtmpOcd.LL e'port C2-9L&
M )6 is S93UR3? pick a signal thatJs not likel" to 8e used
PR42PTS93`)6 e'port PR42PTS93
trap [. LC2-9L&J LPR42PTS93
and then put this e'ecuta8le script </ithout the indentationY=?
letJs call it R'cdS? some/here in "our P#T5
K 'cd director" 1 change director" and set prompt
K 8" signalling the login shell to read a command file
cat VLdC2-9L&BSnot setSe ZZ&4-
cd L)
PS)`Sf[p/df[L R
&4-
kill 1LdPR42PTS93BSnot setSe LdL4396^S5&LLBSnot setSe
6o/ change directories /ith R'cd OsomeOdirS.
Dorn Shell <ksh=K
Put this in "our .profile fileK
PS)`JLP. L [
9f "ou Nust /ant the last component of the director"? use
PS)`JLdP.MMAOe L [
T C shell <tcsh=
Tcsh is a popular enhanced $ersion of csh /ith some e'tra 8uiltin $aria8les <and man" other
features=K
T> the current director"? using > for L542&
TO the full pathname of the current director"
Tc or T. the trailing component of the current director"
so "ou can do
set prompt`JT> [
+#S5 <-S-Js R+ourne #gain S5ellS=
f/ in LPS) gi$es the full pathname of the current director"?
/ith > e'pansion for L542&\ f. gi$es the 8asename of
the current director". So? in addition to the a8o$e sh and
ksh solutions? "ou could use
PS)`Jf/ L [
or
PS)`Jf. L [
)7)
,.5= 5o/ do 9 read characters from the terminal in a shell scriptB
9n sh? use read. 9t is most common to use a loop like
/hile read line
do
...
done
9n csh? use LZ like thisK
/hile < ) =
set line ` RLZS
if < RLlineS `` RS = 8reak
...
end
Unfortunatel" csh has no /a" of distinguishing 8et/een a 8lank line and an end1of1file.
9f "ouJre using sh and /ant to read a sin&e character from the
terminal? "ou can tr" something like
echo 1n R&nter a characterK R
stt" c8reak M or stt" ra/
readchar`[dd if`Ode$Ott" 8s`) count`) ,VOde$Onull[
stt" 1c8reak
echo RThank "ou for t"ping a Lreadchar .S
,.6= 5o/ do 9 rename RA.fooS to RA.8arS? or change file names to lo/ercaseB
.h" doesnJt Rm$ A.foo A.8arS /orkB Think a8out ho/ the shell e'pands /ildcards. RA.fooS
and RA.8arS are e'panded 8efore the m$ command e$er sees the arguments. epending on
"our shell? this can fail in a couple of /a"s. CS5 prints R6o match.S 8ecause it canJt match
RA.8arS. S5 e'ecutes Rm$ a.foo 8.foo c.foo A.8arS? /hich /ill onl" succeed if "ou happen to
ha$e a single director" named RA.8arS? /hich is $er" unlikel" and almost certainl" not /hat
"ou had in mind.
epending on "our shell? "ou can do it /ith a loop to Rm$S each file indi$iduall". 9f "our
s"stem has R8asenameS? "ou can useK
C ShellK
foreach f < A.foo =
set 8ase`[8asename Lf .foo[
m$ Lf L8ase.8ar
end
+ourne ShellK
for f in A.foo\ do
8ase`[8asename Lf .foo[
m$ Lf L8ase.8ar
done
)7,
Some shells ha$e their o/n $aria8le su8stitution features? so instead of using R8asenameS?
"ou can use simpler loops likeK
C ShellK
foreach f < A.foo =
m$ Lf LfKr.8ar
end
Dorn ShellK
for f in A.foo\ do
m$ Lf LdfTfooe8ar
done
9f "ou donJt ha$e R8asenameS or /ant to do something like
renaming foo.A to 8ar.A? "ou can use something like RsedS to
strip apart the original file name in other /a"s? 8ut the general
looping idea is the same. You can also con$ert file names into
Rm$S commands /ith [sedJ? and hand the commands off to RshS for
e'ecution. Tr"
ls 1d A.foo W sed 1e [sO.AOm$ X XOJ 1e [sOfooLO8arOJ W sh
# program 8" Vladimir Lanin called Rmm$S that does this No8
nicel" /as posted to comp.sources.uni' <Volume ,)? issues %! and
%%= in #pril )((*. 9t lets "ou use
mm$ [A.fooJ [`).8arJ
Shell loops like the a8o$e can also 8e used to translate file names from upper to lo/er case or
$ice $ersa. You could use something like this to rename uppercase files to lo/ercaseK
C ShellK
foreach f < A =
m$ Lf [echo Lf W tr [H#1UIJ [Ha1FIJ[ end +ourne ShellK
for f in A\ do
m$ Lf [echo Lf W tr [H#1UIJ [Ha1FIJ[ done Dorn ShellK
t"peset 1l l
for f in A\ do
l`SLfS
m$ Lf Ll
done
9f "ou /anted to 8e reall" thorough and handle files /ith [funn"J
names <em8edded 8lanks or /hate$er= "ouJd need to use
+ourne ShellK
for f in A\ do
g`[e'pr R'''LfS K ['''f<.Af=J W tr [H#1UIJ [Ha1FIJ[
m$ RLfS RLgS
done
The [e'prJ command /ill al/a"s print the filename? e$en if it eEuals [1nJ or if it contains a
S"stem V escape seEuence like [fcJ.
)70
Some $ersions of RtrS reEuire the H and I? some donJt. 9t happens to 8e harmless to include
them in this particular e'ample\ $ersions of tr that donJt /ant the HI /ill con$enientl" think
the" are supposed to translate [HJ to [HJ and [IJ to [IJ.
9f "ou ha$e the RperlS language installed? "ou ma" find this rename script 8" Larr" .all $er"
useful. 9t can 8e used to accomplish a /ide $ariet" of filename changes.
MYOusrO8inOperl
M
M rename script e'amples from l/allK
M rename [sOf.origLOOJ A.orig
M rename ["O#1UOa1FO unless O]2akeOJ A
M rename [L^ .` R.8adSJ A.f
M rename [print RL^K R\ sOfooO8arO if ZstdinV `> O]"OiJ A
Lop ` shift\
for <a#R3V= d
L/as ` L^\
e$al Lop\
die La if La\
rename<L/as?L^= unless L/as eE L^\
e
,.!= .h" do 9 get Hsome strange error messageI /hen 9 Rrsh host commandS B
<.eJre talking a8out the remote shell program RrshS or sometimes RremshS or RremoteS\ on
some machines? there is a restricted shell called RrshS? /hich is a different thing.=
9f "our remote account uses the C shell? the remote host /ill fire up a C shell to e'ecute
[commandJ for "ou? and that shell /ill read "our remote .cshrc file. Perhaps "our .cshrc
contains a Rstt"S? R8iffS or some other command that isnJt appropriate for a non1interacti$e
shell. The une'pected output or error message from these commands can scre/ up "our rsh
in odd /a"s.
5ereJs an e'ample. Suppose "ou ha$e
stt" erase ]5
8iff "
in "our .cshrc file. YouJll get some odd messages like this.
T rsh some1machine date
stt"K K CanJt assign reEuested address
.here are "ouB
Tue 4ct ) *(K,7K75 &ST )(()
You might also get similar errors /hen running certain RatS or RcronS No8s that also read "our
.cshrc file.
-ortunatel"? the fi' is simple. There are? Euite possi8l"? a /hole 'unch of operations in "our
R.cshrcS <e.g.? Rset histor"`6S= that are simpl" not /orth doing e'cept in interacti$e shells.
.hat "ou do is surround them in "our R.cshrcS /ithK
if < LBprompt = then
)77
operations....
endif
and? since in a non1interacti$e shell RpromptS /onJt 8e set? the operations in Euestion /ill
onl" 8e done in interacti$e shells.
You ma" also /ish to mo$e some commands to "our .login file\ if those commands onl"
need to 8e done /hen a login session starts up <checking for ne/ mail? unread ne/s and so
on= itJs 8etter to ha$e them in the .login file.
,.%= 5o/ do 9 dset an en$ironment $aria8le? change director"e inside
a program or shell script and ha$e that change affect m" current shellB
9n general? "ou canJt? at least not /ithout making special arrangements. .hen a child
process is created? it inherits a cop" of its parentJs $aria8les <and current director"=. The
child can change these $alues all it /ants 8ut the changes /onJt affect the parent shell? since
the child is changing a cop" of the original data.
Some special arrangements are possi8le. Your child process could /rite out the changed
$aria8les? if the parent /as prepared to read the output and interpret it as commands to set its
o/n $aria8les.
#lso? shells can arrange to run other shell scripts in the conte't of the current shell? rather
than in a child process? so that changes /ill affect the original shell.
-or instance? if "ou ha$e a C shell script named Rm"scriptSK
cd O$er"OlongOpath
seten$ P#T5 OsomethingKOsomething1else
or the eEui$alent +ourne or Dorn shell script
cd O$er"OlongOpath
P#T5`OsomethingKOsomething1else e'port P#T5
and tr" to run Rm"scriptS from "our shell? "our shell /ill fork and run the shell script in a
su8process. The su8process is also running the shell\ /hen it sees the RcdS command it
changes its current director"? and /hen it sees the Rseten$S command it changes its
en$ironment? 8ut neither has an" effect on the current director" of the shell at /hich "ouJre
t"ping <"our login shell? letJs sa"=.
9n order to get "our login shell to e'ecute the script </ithout
forking= "ou ha$e to use the R.S command <for the +ourne or Dorn
shells= or the RsourceS command <for the C shell=. 9.e. "ou t"pe
. m"script
to the +ourne or Dorn shells? or
source m"script
to the C shell.
9f all "ou are tr"ing to do is change director" or set an en$ironment $aria8le? it /ill pro8a8l"
8e simpler to use a C shell alias or +ourneODorn shell function. See the Rho/ do 9 get the
current director" into m" promptS section of this article for some e'amples.
# much more detailed ans/er prepared 8"
)75
'tmatelelogic.se <Thomas 2ichanek= can 8e found at
ftp./g.omron.co.Np in Opu8Ouni'1faEOdocsOscript1$s1en$.
,.(= 5o/ do 9 redirect stdout and stderr separatel" in cshB
9n csh? "ou can redirect stdout /ith RVS? or stdout and stderr
together /ith RVXS 8ut there is no direct /a" to redirect stderr
onl". The 8est "ou can do is
< command Vstdout^file = VXstderr^file
/hich runs RcommandS in a su8shell\ stdout is redirected inside the su8shell to stdout^file?
and 8oth stdout and stderr from the su8shell are redirected to stderr^file? 8ut 8" this point
stdout has alread" 8een redirected so onl" stderr actuall" /inds up in stderr^file.
9f /hat "ou /ant is to a$oid redirecting stdout at all? let sh do it for "ou.
sh 1c [command ,Vstderr^fileJ
,.)*= 5o/ do 9 tell inside .cshrc if 9Jm a login shellB
.hen people ask this? the" usuall" mean either
5o/ can 9 tell if itJs an interacti$e shellB or
5o/ can 9 tell if itJs a top1le$el shellB
You could perhaps determine if "our shell trul" is a login shell <i.e. is going to source
R.loginS after it is done /ith R.cshrcS= 8" fooling around /ith RpsS and RLLS. Login shells
generall" ha$e names that 8egin /ith a [1[. 9f "ouJre reall" interested in the other t/o
Euestions? hereJs one /a" "ou can organiFe "our .cshrc to find out.
if <Y LBCS5L&V&L= then
M
M This is a Rtop1le$elS shell?
M perhaps a login shell? perhaps a shell started up 8" M [rsh machine some1commandJ M This
is /here /e should set P#T5 and an"thing else /e M /ant to appl" to e$er" one of our
shells.
M
seten$ CS5L&V&L *
set home ` >username M Nust to 8e sure
source >O.en$ M en$ironment stuff /e al/a"s /ant
else
M
M This shell is a child of one of our other shells so M /e donJt need to set all the en$ironment
$aria8les again.
M
set tmp ` LCS5L&V&L
a tmp::
seten$ CS5L&V&L Ltmp
endif
M &'it from .cshrc if not interacti$e? e.g. under rsh
)76
if <Y LBprompt= e'it
M 5ere /e could set the prompt or aliases that /ould 8e useful M for interacti$e shells onl".
source >O.aliases
,.))= 5o/ do 9 construct a shell glo81pattern that matches all files
e'cept R.S and R..S B
YouJd think this /ould 8e eas".
2atches all files that donJt 8egin /ith a R.S\
.A 2atches all files that do 8egin /ith a R.S? 8ut
this includes the special entries R.S and R..S?
/hich often "ou donJt /ant\
.HY.IA <6e/er shells onl"\ some shells use a R]S instead of
the RYS\ P4S9; shells must accept the RYS? 8ut ma"
accept a R]S as /ell\ all porta8le applications shall
not use an unEuoted R]S immediatel" follo/ing the RHS=
2atches all files that 8egin /ith a R.S and are
follo/ed 8" a non1R.S\ unfortunatel" this /ill miss
R..fooS\
.BBA 2atches files that 8egin /ith a R.S and /hich are
at least 0 characters long. This neatl" a$oids
R.S and R..S? 8ut also misses R.aS .
So to match all files e'cept R.S and R..S safel" "ou ha$e to use 0 patterns <if "ou donJt ha$e
filenames like R.aS "ou can lea$e out the first=K
.HY.IA .BBA A
#lternati$el" "ou could emplo" an e'ternal program or t/o and use 8ackEuote su8stitution.
This is prett" goodK
[ls 1a W sed 1e [O]f.LOdJ 1e [O]f.f.LOdJ[
<or [ls 1#[ in some Uni' $ersions=
8ut e$en it /ill mess up on files /ith ne/lines? 9-S characters or /ildcards in their names.
9n ksh? "ou can useK .Y<.W= A
,.),= 5o/ do 9 find the last argument in a +ourne shell scriptB
#ns/er 8"K
2artin .eitFel Zamikros.s"stem/are.deKmartinam/tech.uucpV
2aarten Litmaath Zmaartanat.$u.nlV
)7!
9f "ou are sure the num8er of arguments is at most (? "ou can useK
e$al last`fLdLMe
9n P4S9;1compati8le shells it /orks for #6Y num8er of arguments.
The follo/ing /orks al/a"s tooK
for last
do
K
done
This can 8e generaliFed as follo/sK
for i
do
third^last`Lsecond^last
second^last`Llast
last`Li
done
6o/ suppose "ou /ant to R&24V& the last argument from the list? or R&V&RS& the
argument list? or #CC&SS the 61th argument directl"? /hate$er 6 ma" 8e. 5ere is a 8asis of
ho/ to do it? using onl" 8uilt1in shell constructs? /ithout creating su8processesK
t*` u*` rest`J) , 0 7 5 6 ! % (J arg$`
for h in R Lrest
do
for t in RLt*S Lrest
do
for u in Lu* Lrest
do
case LM in
*=
8reak 0
esac
e$al arg$LhLtLu`fL)
arg$`RLarg$ fRfLarg$LhLtLufRR M <)=
shift
done
u*`*
done
t*`*
done
M no/ restore the arguments
e$al set ' RLarg$S M <,=
shift
This e'ample /orks for the first ((( arguments. &noughB Take a good look at the lines
marked <)= and <,= and con$ince "ourself that the original arguments are restored indeed? no
matter /hat funn" characters the" containY
To find the 61th argument no/ "ou can use thisK
)7%
e$al arg6`fLarg$L6
To re$erse the arguments the line marked <)= must 8e changed toK
arg$`RfRfLarg$LhLtLufR Larg$R
5o/ to remo$e the last argument is left as an e'ercise.
9f "ou allo/ su8processes as /ell? possi8l" e'ecuting non8uilt1in commands? the [arg$6J
$aria8les can 8e set up more easil"K
6`)
for i
do
e$al arg$L6`fLi
6`[e'pr L6 : )[
done
To re$erse the arguments there is still a simpler method? that e$en does not create
su8processes. This approach can also 8e taken if "ou /ant to delete e.g. the last argument?
8ut in that case "ou cannot refer directl" to the 61th argument an" more? 8ecause the [arg$6J
$aria8les are set up in re$erse orderK
arg$`
for i
do
e$al arg$LM`fLi
arg$`SfSfLarg$LMfS Larg$S
shift
done
e$al set ' RLarg$S
shift
,.)0= .hatJs /rong /ith ha$ing [.J in "our LP#T5 B
# 8it of 8ackgroundK the P#T5 en$ironment $aria8le is a list of directories separated 8"
colons. .hen "ou t"pe a command name /ithout gi$ing an e'plicit path <e.g. "ou t"pe RlsS?
rather than RO8inOlsS= "our shell searches each director" in the P#T5 list in order? looking for
an e'ecuta8le file 8" that name? and the shell /ill run the first matching program it finds.
4ne of the directories in the P#T5 list can 8e the current
director" R.S . 9t is also permissi8le to use an empt" director"
name in the P#T5 list to indicate the current director". +oth of
these are eEui$alent
for csh usersK
seten$ P#T5 KOusrOuc8KO8inKOusrO8in
seten$ P#T5 .KOusrOuc8KO8inKOusrO8in
for sh or ksh users
P#T5`KOusrOuc8KO8inKOusrO8in e'port P#T5
P#T5`.KOusrOuc8KO8inKOusrO8in e'port P#T5
)7(
5a$ing R.S some/here in the P#T5 is con$enient 1 "ou can t"pe Ra.outS instead of R.Oa.outS
to run programs in the current director". +ut thereJs a catch.
Consider /hat happens in the case /here R.S is the first entr" in the P#T5. Suppose "our
current director" is a pu8licall"1/rita8le one? such as ROtmpS. 9f there Nust happens to 8e a
program named ROtmpOlsS left there 8" some other user? and "ou t"pe RlsS <intending? of
course? to run the normal RO8inOlsS program=? "our shell /ill instead run R.OlsS? the other userJs
program. 6eedless to sa"? the results of running an unkno/n program like this might
surprise "ou.
9tJs slightl" 8etter to ha$e R.S at the end of the P#T5K
seten$ P#T5 OusrOuc8KO8inKOusrO8inK.
6o/ if "ouJre in Otmp and "ou t"pe RlsS? the shell /ill
search OusrOuc8? O8in and OusrO8in for a program named
RlsS 8efore it gets around to looking in R.S? and there
is less risk of inad$ertentl" running some other userJs
RlsS program. This isnJt )**T secure though 1 if "ouJre
a clums" t"pist and some da" t"pe Rsl 1lS instead of Rls 1lS? "ou run the risk of running R.OslS?
if there is one.
Some Rcle$erS programmer could anticipate common t"ping mistakes and lea$e programs 8"
those names scattered throughout pu8lic directories. +e/are.
2an" seasoned Uni' users get 8" Nust fine /ithout ha$ing R.S in the P#T5 at allK
seten$ P#T5 OusrOuc8KO8inKOusrO8in
9f "ou do this? "ouJll need to t"pe R.OprogramS instead of RprogramS to run programs in the
current director"? 8ut the increase in securit" is pro8a8l" /orth it.
,.)7= 5o/ do 9 ring the terminal 8ell during a shell scriptB
The ans/er depends on "our Uni' $ersion <or rather on the kind of RechoS program that is
a$aila8le on "our machine=.
# +S1like RechoS uses the R1nS option for suppressing the final
ne/line and does not understand the octal fnnn notation. Thus
the command is
echo 1n []3J
/here ]3 means a literal +&L1character <"ou can produce this in emacs using RCtrl1G Ctrl1
3S and in $i using RCtrl1V Ctrl13S=.
# S"sV1like RechoS understands the fnnn notation and uses fc to suppress the final ne/line?
so the ans/er isK
echo [f**!fcJ
,.)5= .h" canJt 9 use RtalkS to talk /ith m" friend on machine ;B
Uni' has three common RtalkS programs? none of /hich can talk /ith an" of the others. The
RoldS talk accounts for the first t/o t"pes. This $ersion <often called otalk= did not take
)5*
RendianS order into account /hen talking to other machines. #s a conseEuence? the Va'
$ersion of otalk cannot talk /ith the Sun $ersion of otalk. These $ersions of talk use port
5)!.
#round )(%!? most $endors <e'cept Sun? /ho took 6 "ears longer than an" of their
competitors= standardiFed on a ne/ talk <often called ntalk= /hich kno/s a8out net/ork 8"te
order. This talk /orks 8et/een all machines that ha$e it. This $ersion of talk uses port 5)%.
There are no/ a fe/ talk programs that speak 8oth ntalk and one $ersion of otalk. The most
common of these is called R"talkS.
,.)6= .h" does calendar produce the /rong outputB
-reEuentl"? people find that the output for the Uni' calendar program? [calJ produces output
that the" do not e'pect.
The calendar for Septem8er )!5, is $er" oddK
Septem8er )!5,
S 2 Tu . Th - S
) , )7 )5 )6 )! )% )( ,* ,) ,, ,0 ,7 ,5 ,6 ,! ,% ,( 0*
This is the month in /hich the US <the entire +ritish &mpire actuall"= s/itched from the
Culian to the 3regorian calendar.
The other common pro8lem people ha$e /ith the calendar program is that the" pass it
arguments like [cal ( (7J. This gi$es the calendar for Septem8er of # (7? 64T )((7.
Uni" ' $re<uently Asked >uestions 071 ?$re<uent
posting@
This article includes ans/ers toK
0.)= 5o/ do 9 find the creation time of a fileB
0.,= 5o/ do 9 use RrshS /ithout ha$ing the rsh hang around until the remote command
has completedB
0.0= 5o/ do 9 truncate a fileB
0.7= .h" doesnJt findJs RdeS s"m8ol do /hat 9 /antB
0.5= 5o/ do 9 set the permissions on a s"m8olic linkB
0.6= 5o/ do 9 RundeleteS a fileB
0.!= 5o/ can a process detect if itJs running in the 8ackgroundB
0.%= .h" doesnJt redirecting a loop /ork as intendedB <+ourne shell=
)5)
0.(= 5o/ do 9 run [pass/dJ? [ftpJ? [telnetJ? [tipJ and other interacti$e programs from a
shell script or in the 8ackgroundB
0.)*= 5o/ do 9 find the process 9 of a program /ith a particular name from inside a shell script
or C programB
0.))= 5o/ do 9 check the e'it status of a remote command e'ecuted $ia RrshS B
0.),= 9s it possi8le to pass shell $aria8le settings into an a/k programB
0.)0= 5o/ do 9 get rid of Fom8ie processes that perse$ereB
0.)7= 5o/ do 9 get lines from a pipe as the" are /ritten instead of onl" in larger 8locksB
0.)5= 5o/ do 9 get the date into a filenameB
0.)6= .h" do some scripts start /ith MY ... B
9f "ouJre looking for the ans/er to? sa"? Euestion 0.5? and /ant to skip e$er"thing else? "ou can
search ahead for the regular e'pression R]0.5=S.
.hile these are all legitimate Euestions? the" seem to crop up in comp.uni'.Euestions or
comp.uni'.shell on an annual 8asis? usuall" follo/ed 8" plent" of replies <onl" some of /hich are
correct= and then a period of griping a8out ho/ the same Euestions keep coming up. You ma"
also like to read the monthl" article R#ns/ers to -reEuentl" #sked GuestionsS in the ne/sgroup
Rne/s.announce.ne/usersS? /hich /ill tell "ou /hat RU69;S stands for.
.ith the $ariet" of Uni' s"stems in the /orld? itJs hard to guarantee that these ans/ers /ill /ork
e$er"/here. Read "our local manual pages 8efore tr"ing an"thing suggested here. 9f "ou ha$e
suggestions or corrections for an" of these ans/ers? please send them to to tmatimaraisgtec.com.
0.)= 5o/ do 9 find the creation time of a fileB
You canJt 1 it isnJt stored an"/here. -iles ha$e a last1modified time <sho/n 8" Rls 1lS=? a
last1accessed time <sho/n 8" Rls 1luS= and an inode change time <sho/n 8" Rls 1lcS=. The
latter is often referred to as the Rcreation timeS 1 e$en in some man pages 1 8ut thatJs /rong\
itJs also set 8" such operations as m$? ln? chmod? cho/n and chgrp.
The man page for Rstat<,=S discusses this.
0.,= 5o/ do 9 use RrshS /ithout ha$ing the rsh hang around until the
remote command has completedB
<See note in Euestion ,.! a8out /hat RrshS /eJre talking a8out.=
The o8$ious ans/ers failK
rsh machine command X
or rsh machine [command XJ
-or instance? tr" doing rsh machine [sleep 6* XJ and "ouJll see
that the [rshJ /onJt e'it right a/a". 9t /ill /ait 6* seconds until the remote [sleepJ command
finishes? e$en though that command /as started in the 8ackground on the remote machine.
So ho/ do "ou get the [rshJ to e'it immediatel" after the [sleepJ is startedB
)5,
The solution 1 if "ou use csh on the remote machineK
rsh machine 1n [command VXOde$Onull ZOde$Onull XJ
9f "ou use sh on the remote machineK
rsh machine 1n [command VOde$Onull ,VX) ZOde$Onull XJ
.h"B R1nS attaches rshJs stdin to Ode$Onull so "ou could run the complete rsh command in
the 8ackground on the L4C#L machine. Thus R1nS is eEui$alent to another specific RZ
Ode$OnullS. -urthermore? the inputOoutput redirections on the R&24T& machine <inside the
single Euotes= ensure that rsh thinks the session can 8e terminated <thereJs no data flo/ an"
more.=
6oteK The file that "ou redirect toOfrom on the remote machine doesnJt ha$e to 8e Ode$Onull\
an" ordinar" file /ill do.
9n man" cases? $arious parts of these complicated commands arenJt necessar".
0.0= 5o/ do 9 truncate a fileB
The +S function ftruncate<= sets the length of a file.
<+ut not all $ersions 8eha$e identicall".= 4ther Uni' $ariants all seem to support some
$ersion of truncation as /ell.
-or s"stems /hich support the ftruncate function? there are three kno/n 8eha$ioursK
+S 7., 1 Ultri'? S39? L"n'4S
truncation doesnJt gro/ file
truncation doesnJt mo$e file pointer
+S 7.0 1 Sun4S? Solaris? 4S-O)? 5POU;? #miga
truncation can gro/ file
truncation doesnJt mo$e file pointer
Cra" 1 UniC4S !? UniC4S %
truncation doesnJt gro/ file
truncation changes file pointer
4ther s"stems come in four $arietiesK
-^C5S9U& 1 4nl" SC4
some s"stems define -^C5S9U& 8ut donJt support it
8eha$es like +S 7.0
-^-R&&SP 1 4nl" 9nterati$e Uni'
some s"stems <eg. 9nteracti$e Uni'= define -^-R&&SP 8ut donJt support it
8eha$es like +S 7.0
chsiFe<= 1 G6; and SC4
some s"stems <eg. 9nteracti$e Uni'= ha$e chsiFe<= 8ut donJt support it
)50
8eha$es like +S 7.0
nothing 1 no kno/n s"stems
there /ill 8e s"stems that donJt support truncate at all
2oderatorJs 6oteK 9 gra88ed the functions 8elo/ a fe/ "ears 8ack.
9 can no longer identif" the original author.
S. Spencer Sun Zspencerancd.comV has also contri8uted a $ersion for -^-R&&SP.
functions for each non1nati$e ftruncate follo/
OA ftruncate emulations that /ork on some S"stem VJs.
This file is in the pu8lic domain. AO
Minclude
Minclude
Mifdef -^C5S9U&
int
ftruncate <fd? length=
int fd\
off^t length\
d
return fcntl <fd? -^C5S9U&? length=\
e
Melse
Mifdef -^-R&&SP
OA The follo/ing function /as /ritten 8"
kucharskaSol8ourne.com <.illiam Ducharski= AO
Minclude
Minclude
Minclude
int
ftruncate <fd? length=
int fd\
off^t length\
d
struct flock fl\
struct stat file8uf\
if <fstat <fd? Xfile8uf= Z *=
return 1)\
if <file8uf.st^siFe Z length=
d
OA &'tend file length. AO
if <lseek <fd? <length 1 )=? S&&D^S&T= Z *=
return 1)\
)57
OA .rite a R*S 8"te. AO
if </rite <fd? RS? )= Y` )=
return 1)\
e
else
d
OA Truncate length. AO
fl.l^/hence ` *\
fl.l^len ` *\
fl.l^start ` length\
fl.l^t"pe ` -^.RLCD\ OA .rite lock on file space. AO
OA This relies on the U64CU2&6T& -^-R&&SP argument to fcntl? /hich truncates the
file so that it ends at the position indicated 8" fl.l^start.
.ill minor miracles ne$er ceaseB AO
if <fcntl <fd? -^-R&&SP? Xfl= Z *=
return 1)\
e
return *\
e
Melse
int
ftruncate <fd? length=
int fd\
off^t length\
d
return chsiFe <fd? length=\
e
Mendif
Mendif
0.7= .h" doesnJt findJs RdeS s"m8ol do /hat 9 /antB
RfindS has a 1e'ec option that /ill e'ecute a particular command on all the selected files. -ind
/ill replace an" RdeS it sees /ith the name of the file currentl" under consideration.
So? some da" "ou might tr" to use RfindS to run a command on e$er" file? one director" at a
time. You might tr" thisK
find Opath 1t"pe d 1e'ec command deOfA f\
hoping that find /ill e'ecute? in turn
command director")OA
command director",OA
...
Unfortunatel"? find onl" e'pands the RdeS token /hen it appears
8" itself. -ind /ill lea$e an"thing else like RdeOAS alone? so
)55
instead of doing /hat "ou /ant? it /ill do
command deOA
command deOA
...
once for each director". This might 8e a 8ug? it might 8e a feature? 8ut /eJre stuck /ith the
current 8eha$iour.
So ho/ do "ou get around thisB 4ne /a" /ould 8e to /rite a
tri$ial little shell script? letJs sa" R.OdoitS? that consists of
command RL)SOA
You could then use
find Opath 1t"pe d 1e'ec .Odoit de f\
4r if "ou /ant to a$oid the R.OdoitS shell script? "ou can use
find Opath 1t"pe d 1e'ec sh 1c [command L*OAJ de f\
<This /orks 8ecause /ithin the [commandJ of Rsh 1c [commandJ # + C ...S?
L* e'pands to #? L) to +? and so on.=
or "ou can use the construct1a1command1/ith1sed trick
find Opath 1t"pe d 1print W sed [sK.AKcommand XOAKJ W sh
9f all "ouJre tr"ing to do is cut do/n on the num8er of times
that RcommandS is e'ecuted? "ou should see if "our s"stem has the
R'argsS command. ;args reads arguments one line at a time from
the standard input and assem8les as man" of them as /ill fit into
one command line. You could use
find Opath 1print W 'args command
/hich /ould result in one or more e'ecutions of
command file) file, file0 file7 dir)Ofile) dir)Ofile,
Unfortunatel" this is not a perfectl" ro8ust or secure solution. ;args e'pects its input lines to
8e terminated /ith ne/lines? so it /ill 8e confused 8" files /ith odd characters such as
ne/lines in their names.
0.5= 5o/ do 9 set the permissions on a s"m8olic linkB
Permissions on a s"m8olic link donJt reall" mean an"thing. The onl" permissions that count
are the permissions on the file that the link points to.
0.6= 5o/ do 9 RundeleteS a fileB
Someda"? "ou are going to accidentall" t"pe something like Rrm A .fooS? and find "ou Nust
deleted RAS instead of RA.fooS. Consider it a rite of passage.
)56
4f course? an" decent s"stems administrator should 8e doing regular 8ackups. Check /ith
"our s"sadmin to see if a recent 8ackup cop" of "our file is a$aila8le. +ut if it isnJt? read on.
-or all intents and purposes? /hen "ou delete a file /ith RrmS it is gone. 4nce "ou RrmS a
file? the s"stem totall" forgets /hich 8locks scattered around the disk /ere part of "our file.
&$en /orse? the 8locks from the file "ou Nust deleted are going to 8e the first ones taken and
scri88led upon /hen the s"stem needs more disk space. 5o/e$er? ne$er sa" ne$er. 9t is
theoreticall" possi8le if "ou shut do/n the s"stem immediatel" after the RrmS to reco$er
portions of the data. 5o/e$er? "ou had 8etter ha$e a $er" /iFardl" t"pe person at hand /ith
hours or da"s to spare to get it all 8ack.
Your first reaction /hen "ou RrmS a file 8" mistake is /h" not make a shell alias or
procedure /hich changes RrmS to mo$e files into a trash 8in rather than delete themB That
/a" "ou can reco$er them if "ou make a mistake? and periodicall" clean out "our trash 8in.
T/o pointsK first? this is generall" accepted as a 'ad idea. You /ill 8ecome dependent upon
this 8eha$iour of RrmS? and "ou /ill find "ourself someda" on a normal s"stem /here RrmS
is reall" RrmS? and "ou /ill get "ourself in trou8le. Second? "ou /ill e$entuall" find that the
hassle of dealing /ith the disk space and time in$ol$ed in maintaining the trash 8in? it might
8e easier Nust to 8e a 8it more careful /ith RrmS. -or starters? "ou should look up the R1iS
option to RrmS in "our manual.
9f "ou are still undaunted? then here is a possi8le simple ans/er. You can create "ourself a
RcanS command /hich mo$es files into a trashcan director". 9n csh<)= "ou can place the
follo/ing commands in the R.loginS file in "our home director"K
alias can [m$ fYA >O.trashcanJ M Nunk file<s= to trashcan
alias mtcan [rm 1f >O.trashcanOAJ M irretrie$a8l" empt" trash
if < Y 1d >O.trashcan = mkdir >O.trashcan M ensure trashcan e'ists
You might also /ant to put aK
rm 1f >O.trashcanOA
in the R.logoutS file in "our home director" to automaticall" empt" the trash /hen "ou log
out. <sh and ksh $ersions are left as an e'ercise for the reader.=
29TJs ProNect #thena has produced a comprehensi$e deleteOundeleteOe'pungeOpurge package?
/hich can ser$e as a complete replacement for rm /hich allo/s file reco$er". This package
/as posted to comp.sources.misc <$olume )!? issue *,01*,6=
0.!= 5o/ can a process detect if itJs running in the 8ackgroundB
-irst of allK do "ou /ant to kno/ if "ouJre running in the 8ackground? or if "ouJre running
interacti$el"B 9f "ouJre deciding /hether or not "ou should print prompts and the like? thatJs
pro8a8l" a 8etter criterion. Check if standard input is a terminalK
shK if H 1t * I\ then ... fi
CK if<isatt"<*== d ... e
9n general? "ou canJt tell if "ouJre running in the 8ackground. The fundamental pro8lem is that
different shells and different $ersions of U69; ha$e different notions of /hat RforegroundS and
R8ackgroundS mean 1 and on the most common t"pe of s"stem /ith a 8etter1defined notion of
/hat the" mean? programs can 8e mo$ed ar8itraril" 8et/een foreground and 8ackgroundY
)5!
U69; s"stems /ithout No8 control t"picall" put a process into the 8ackground 8" ignoring
S9396T and S93GU9T and redirecting the standard input to ROde$OnullS\ this is done 8" the
shell.
Shells that support No8 control? on U69; s"stems that support No8 control? put a process into
the 8ackground 8" gi$ing it a process group 9 different from the process group to /hich the
terminal 8elongs. The" mo$e it 8ack into the foreground 8" setting the terminalJs process
group 9 to that of the process. Shells that do not support No8 control? on U69; s"stems that
support No8 control? t"picall" do /hat shells do on s"stems that donJt support No8 control.
0.%= .h" doesnJt redirecting a loop /ork as intendedB <+ourne shell=
Take the follo/ing e'ampleK
foo`8ar
/hile read line
do
M do something /ith Lline
foo`8letch
done Z OetcOpass/d
echo Rfoo is no/K LfooS
espite the assignment Rfoo`8letchS this /ill print Rfoo is no/K 8arS in man"
implementations of the +ourne shell. .h"B +ecause of the follo/ing? often undocumented?
feature of historic +ourne shellsK redirecting a control structure <such as a loop? or an RifS
statement= causes a su8shell to 8e created? in /hich the structure is e'ecuted\ $aria8les set in
that su8shell <like the Rfoo`8letchS assignment= donJt affect the current shell? of course.
The P4S9; )**0., Shell and Tools 9nterface standardiFation committee for8ids the
8eha$iour descri8ed a8o$e? i.e. in P)**0., conformant +ourne shells the e'ample /ill print
Rfoo is no/K 8letchS.
9n historic <and P)**0., conformant= implementations "ou can use the follo/ing [trickJ to get
around the redirection pro8lemK
foo`8ar
M make file descriptor ( a duplicate of file descriptor * <stdin=\
M then connect stdin to OetcOpass/d\ the original stdin is no/
M [remem8eredJ in file descriptor (\ see dup<,= and sh<)=
e'ec (ZX* Z OetcOpass/d
/hile read line
do
M do something /ith Lline
foo`8letch
done
M make stdin a duplicate of file descriptor (? i.e. reconnect
M it to the original stdin\ then close file descriptor (
e'ec *ZX( (ZX1
)5%
echo Rfoo is no/K LfooS
This should al/a"s print Rfoo is no/K 8letchS.
Right? take the ne't e'ampleK
foo`8ar
echo 8letch W read foo
echo Rfoo is no/K LfooS
This /ill print Rfoo is no/K 8arS in man" implementations? Rfoo is no/K 8letchS in some
others. .h"B 3enerall" each part of a pipeline is run in a different su8shell\ in some
implementations though? the last command in the pipeline is made an e'ceptionK if it is a
8uiltin command like RreadS? the current shell /ill e'ecute it? else another su8shell is created.
P4S9; )**0., allo/s 8oth 8eha$iours so porta8le scripts cannot depend on an" of them.
0.(= 5o/ do 9 run [pass/dJ? [ftpJ? [telnetJ? [tipJ and other interacti$e
programs from a shell script or in the 8ackgroundB
These programs e'pect a terminal interface. Shells makes no special pro$isions to pro$ide
one. 5ence? such programs cannot 8e automated in shell scripts.
The [e'pectJ program pro$ides a programma8le terminal interface for automating interaction
/ith such programs. The follo/ing e'pect script is an e'ample of a non1interacti$e $ersion
of pass/d<)=.
M username is passed as )
st
arg? pass/ord as ,
nd
set pass/ord Hinde' Larg$ ,I
spa/n pass/d Hinde' Larg$ )I
e'pect RApass/ordKS
send RLpass/ordfrS
e'pect RApass/ordKS
send RLpass/ordfrS
e'pect eof
e'pect can partiall" automate interaction /hich is especiall" useful for telnet? rlogin?
de8uggers or other programs that ha$e no 8uilt1in command language. The distri8ution
pro$ides an e'ample script to rerun rogue until a good starting configuration appears. Then?
control is gi$en 8ack to the user to enNo" the game.
-ortunatel" some programs ha$e 8een /ritten to manage the connection to a pseudo1tt" so
that "ou can run these sorts of programs in a script.
To get e'pect? email Rsend pu8Oe'pectOe'pect.shar.US to
li8rar"acme.nist.go$ or anon"mous ftp same from
ftp.cme.nist.go$.
#nother solution is pro$ided 8" the pt" 7.* program? /hich runs a program under a pseudo1
tt" session and /as posted to comp.sources.uni'? $olume ,5. # pt"18ased solution using
named pipes to do the same as the a8o$e might look like thisK
MYO8inOsh
OetcOmknod out.LL p\ e'ec ,VX)
)5(
< e'ec 7Zout.LL\ rm 1f out.LL
ZX7 /aitfor [pass/ordKJ
echo RL,S
ZX7 /aitfor [pass/ordKJ
echo RL,S
ZX7 cat VOde$Onull
= W < pt" pass/d RL)S Vout.LL =
5ere? [/aitforJ is a simple C program that searches for
its argument in the input? character 8" character.
# simpler pt" solution </hich has the dra/8ack of not
s"nchroniFing properl" /ith the pass/d program= is
MYO8inOsh
< sleep 5\ echo RL,S\ sleep 5\ echo RL,S= W pt" pass/d RL)S
0.)*= 5o/ do 9 find the process 9 of a program /ith a particular name
from inside a shell script or C programB
9n a shell scriptK
There is no utilit" specificall" designed to map 8et/een program names and process 9s.
-urthermore? such mappings are often unrelia8le? since itJs possi8le for more than one
process to ha$e the same name? and since itJs possi8le for a process to change its name once
it starts running. 5o/e$er? a pipeline like this can often 8e used to get a list of processes
<o/ned 8" "ou= /ith a particular nameK
ps u' W a/k [OnameO XX YOa/kO dprint L,eJ
You replace RnameS /ith the name of the process for /hich "ou are searching.
The general idea is to parse the output of ps? using a/k or grep or other utilities? to search for
the lines /ith the specified name on them? and print the P9Js for those lines. 6ote that the
RYOa/kOS a8o$e pre$ents the a/k process for 8eing listed.
You ma" ha$e to change the arguments to ps? depending on /hat kind of Uni' "ou are using.
9n a C programK
Cust as there is no utilit" specificall" designed to map 8et/een program names and process
9s? there are no <porta8le= C li8rar" functions to do it either.
5o/e$er? some $endors pro$ide functions for reading Dernel memor"\ for e'ample? Sun
pro$ides the Rk$m^S functions? and ata 3eneral pro$ides the Rdg^S functions. 9t ma" 8e
possi8le for an" user to use these? or the" ma" onl" 8e usea8le 8" the super1user <or a user in
group RkmemS= if read1access to kernel memor" on "our s"stem is restricted. -urthermore?
these functions are often not documented or documented 8adl"? and might change from
release to release.
Some $endors pro$ide a ROprocS files"stem? /hich appears as a director" /ith a 8unch of
filenames in it. &ach filename is a num8er? corresponding to a process 9? and "ou can open
)6*
the file and read it to get information a8out the process. 4nce again? access to this ma" 8e
restricted? and the interface to it ma" change from s"stem to s"stem.
9f "ou canJt use $endor1specific li8rar" functions? and "ou donJt ha$e Oproc? and "ou still
/ant to do this completel" in C? "ou are going to ha$e to do the rummaging through kernel
memor" "ourself. -or a good e'ample of ho/ to do this on man" s"stems? see the sources to
RofilesS? a$aila8le in the comp.sources.uni' archi$es. <# package named RkstuffS to help
/ith kernel rummaging /as posted to alt.sources in 2a" )(() and is also a$aila8le $ia
anon"mous ftp as usenetOalt.sourcesOarticlesOd0,(d6?!?%?(e?00*d*?)ee.U from
/uarchi$e./ustl.edu.=
0.))= 5o/ do 9 check the e'it status of a remote command
e'ecuted $ia RrshS B
This doesnJt /orkK
rsh some1machine some1crumm"1command WW echo RCommand failedS
The e'it status of [rshJ is * <success= if the rsh program itself completed successfull"? /hich
pro8a8l" isnJt /hat "ou /anted.
9f "ou /ant to check on the e'it status of the remote program? "ou can tr" using 2aarten
LitmaathJs [ershJ script? /hich /as posted to alt.sources in 4cto8er )((7. ersh is a shell
script that calls rsh? arranges for the remote machine to echo the status of the command after
it completes? and e'its /ith that status.
0.),= 9s it possi8le to pass shell $aria8le settings into an a/k programB
There are t/o different /a"s to do this. The first in$ol$es simpl" e'panding the $aria8le
/here it is needed in the program.
-or e'ample? to get a list of all tt"s "ouJre usingK
/ho W a/k [O]JSLUS&RSJO d print L, eJ <)=
Single Euotes are usuall" used to enclose a/k programs 8ecause the character [LJ is often
used in them? and [LJ /ill 8e interpreted 8" the shell if enclosed inside dou8le Euotes? 8ut not
if enclosed inside single Euotes. 9n this case? /e want the [LJ in RLUS&RS to 8e interpreted
8" the shell? so /e close the single Euotes and then put the RLUS&RS inside dou8le Euotes.
6ote that there are no spaces in an" of that? so the shell /ill
see it all as one argument. 6ote? further? that the dou8le
Euotes pro8a8l" arenJt necessar" in this particular case <i.e. /e
could ha$e done
/ho W a/k [O]JLUS&RJO d print L, eJ <,=
=? 8ut the" should 8e included ne$ertheless 8ecause the" are necessar" /hen the shell $aria8le
in Euestion contains special characters or spaces.
)6)
The second /a" to pass $aria8le settings into a/k is to use an often undocumented feature of
a/k /hich allo/s $aria8le settings to 8e specified as Rfake file namesS on the command line.
-or e'ampleK
/ho W a/k [L) `` user d print L, eJ user`SLUS&RS 1 <0=
Varia8le settings take effect /hen the" are encountered on the command line? so? for
e'ample? "ou could instruct a/k on ho/ to 8eha$e for different files using this techniEue.
-or e'ampleK
a/k [d program that depends on s eJ s`) file) s`* file, <7=
6ote that some $ersions of a/k /ill cause $aria8le settings encountered 8efore an" real
filenames to take effect 8efore the +&396 8lock is e'ecuted? 8ut some /onJt so neither /a"
should 8e relied upon.
6ote? further? that /hen "ou specif" a $aria8le setting? a/k /onJt automaticall" read from
stdin if no real files are specified? so "ou need to add a R1R argument to the end of "our
command? as 9 did at <0= a8o$e.
# third option is to use a ne/er $ersion of a/k <na/k=? /hich allo/s direct access to
en$ironment $aira8les. &g.
na/k [&6 d print RYour path $aria8le is R &6V9R46HSP#T5SI eJ Ode$Onull
0.)0= 5o/ do 9 get rid of Fom8ie processes that perse$ereB
Unfortunatel"? itJs impossi8le to generaliFe ho/ the death of child processes should 8eha$e?
8ecause the e'act mechanism $aries o$er the $arious fla$ors of Uni'.
-irst of all? 8" default? "ou ha$e to do a /ait<= for child processes under #LL fla$ors of Uni'.
That is? there is no fla$or of Uni' that 9 kno/ of that /ill automaticall" flush child processes
that e'it? e$en if "ou donJt do an"thing to tell it to do so.
Second? under some S"sV1deri$ed s"stems? if "ou do Rsignal<S93C5L? S93^936=S </ell?
actuall"? it ma" 8e S93CL instead of S93C5L? 8ut most of the ne/er S"sV s"stems ha$e
RMdefine S93C5L S93CLS in the header files=? then child processes /ill 8e cleaned up
automaticall"? /ith no further effort in "our part. The 8est /a" to find out if it /orks at "our
site is to tr" it? although if "ou are tr"ing to /rite porta8le code? itJs a 8ad idea to rel" on this
in an" case.
Unfortunatel"? P4S9; doesnJt allo/ "ou to do this\ the 8eha$ior
of setting the S93C5L to S93^936 under P4S9; is undefined? so
"ou canJt do it if "our program is supposed to 8e
P4S9;1compliant.
So? /hatJs the P4S9; /a"B #s mentioned earlier? "ou must install a signal handler and /ait.
Under P4S9; signal handlers are installed /ith sigaction. Since "ou are not interested in
RstoppedS children? onl" in terminated children? add S#^64CLST4P to sa^flags. .aiting
/ithout 8locking is done /ith /aitpid<=. The first argument to /aitpid should 8e 1) </ait for
an" pid=? the third should 8e .645#63. This is the most porta8le /a" and is likel" to
8ecome more porta8le in future.
9f "our s"stems doesnJt support P4S9;? thereJs a num8er of /a"s.
)6,
The easiest /a" is signal<S93C5L? S93^936=? if it /orks. 9f S93^936 cannot 8e used to
force automatic clean1up? then "ouJ$e got to /rite a signal handler to do it. 9t isnJt eas" at all
to /rite a signal handler that does things right on all fla$ors of Uni'? 8ecause of the follo/ing
inconsistenciesK
4n some fla$ors of Uni'? the S93C5L signal handler is called if one or more children ha$e
died. This means that if "our signal handler onl" does one /ait<= call? then it /onJt clean up
all of the children. -ortunatel"? 9 8elie$e that all Uni' fla$ors for /hich this is the case ha$e
a$aila8le to the programmer the /ait0<= or /aitpid<= call? /hich allo/s the .645#63
option to check /hether or not there are an" children /aiting to 8e cleaned up. Therefore? on
an" s"stem that has /ait0<=O/aitpid<=? "our signal handler should call /ait0<=O/aitpid<= o$er
and o$er again /ith the .645#63 option until there are no children left to clean up.
.aitpid<= is the preferred interface? as it is in P4S9;.
4n S"sV1deri$ed s"stems? S93C5L signals are regenerated if there are child processes still
/aiting to 8e cleaned up after "ou e'it the S93C5L signal handler. Therefore? itJs safe on
most S"sV s"stems to assume /hen the signal handler gets called that "ou onl" ha$e to clean
up one signal? and assume that the handler /ill get called again if there are more to clean up
after it e'its.
4n older s"stems? there is no /a" to pre$ent signal handlers from 8eing automaticall" reset to
S93^-L /hen the signal handler gets called. 4n such s"stems? "ou ha$e to put
Rsignal<S93C59L? catcher^func=S </here Rcatcher^funcS is the name of the handler
function= as the last thing in the signal handler? so that it gets reset.
-ortunatel"? ne/er implementations allo/ signal handlers to 8e installed /ithout 8eing reset
to S93^-L /hen the handler function is called. To get around this pro8lem? on s"stems that
do not ha$e /ait0<=O/aitpid<= 8ut do ha$e S93CL? "ou need to reset the signal handler /ith
a call to signal<= after doing at least one /ait<= /ithin the handler? each time it is called. -or
8ack/ard compati8ilit" reasons? S"stem V /ill keep the old semantics <reset handler on call=
of signal<=. Signal handlers that stick can 8e installed /ith sigaction<= or sigset<=.
The summar" of all this is that on s"stems that ha$e /aitpid<= <P4S9;= or /ait0<=? "ou
should use that and "our signal handler should loop? and on s"stems that donJt? "ou should
ha$e one call to /ait<= per in$ocation of the signal handler.
4ne more thingPif "ou donJt /ant to go through all of this trou8le? there is a porta8le /a" to
a$oid this pro8lem? although it is some/hat less efficient. Your parent process should fork?
and then /ait right there and then for the child process to terminate. The child process then
forks again? gi$ing "ou a child and a grandchild. The child e'its immediatel" <and hence the
parent /aiting for it notices its death and continues to /ork=? and the grandchild does
/hate$er the child /as originall" supposed to. Since its parent died? it is inherited 8" init?
/hich /ill do /hate$er /aiting is needed. This method is inefficient 8ecause it reEuires an
e'tra fork? 8ut is prett" much completel" porta8le.
0.)7= 5o/ do 9 get lines from a pipe as the" are /ritten instead of onl" in
larger 8locksB
The stdio li8rar" does 8uffering differentl" depending on /hether it thinks itJs running on a
tt". 9f it thinks itJs on a tt"? it does 8uffering on a per1line 8asis\ if not? it uses a larger 8uffer
than one line.
)60
9f "ou ha$e the source code to the client /hose 8uffering "ou /ant to disa8le? "ou can use
set8uf<= or set$8uf<= to change the 8uffering.
9f not? the 8est "ou can do is tr" to con$ince the program that itJs running on a tt" 8" running
it under a pt"? e.g. 8" using the Rpt"S program mentioned in Euestion 0.(.
0.)5= 5o/ do 9 get the date into a filenameB
This isnJt hard? 8ut it is a 8it cr"ptic at first sight. LetJs 8egin /ith the date command itselfK
date can take a formatting string? to modif" the /a" in /hich the date info is printed. The
formatting string has to 8e enclosed in Euotes? to stop the shell tr"ing to interpret it 8efore the
date command itself gets it.
Tr" thisK
date [:TdTmT"J
"ou should get 8ack something like )0*((7. 9f "ou /ant to punctuate this? Nust put the
characters "ou /ould like to use in the formatting string <64 SL#S5&S [OJ=K
date [:Td.Tm.T"J
There are lots of token "ou can use in the formatting stringK
ha$e a look at the man page for date to find out a8out them.
6o/? getting this into a file name. LetJs sa" that /e /ant to create files called report.)0*((7
<or /hate$er the date is toda"=K
-9L&6#2&`report.[date [:TdTmT"J[
6otice that /e are using t/o sets of Euotes hereK the inner set are to protect the formatting
string from premature interpretation\ the outer set are to tell the shell to e'ecute the enclosed
command? and su8stitute the result into the e'pression <command su8stitution=.
0.)6= .h" do some scripts start /ith MY ... B
Chip Rosenthal has ans/ered a closel" related Euestion in comp.uni'.'eni' in the past.
9 think /hat confuses people is that there e'ist t/o different mechanisms? 8oth spelled /ith
the letter [MJ. The" 8oth sol$e the same pro8lem o$er a $er" restricted set of casesP8ut the"
are none the less different.
Some 8ackground. .hen the U69; kernel goes to run a program <one of the e'ec<= famil"
of s"stem calls=? it takes a peek at the first )6 8its of the file. Those )6 8its are called a
[magic num8erJ. -irst? the magic num8er pre$ents the kernel from doing something sill" like
tr"ing to e'ecute "our customer data8ase file. 9f the kernel does not recogniFe the magic
num8er then it complains /ith an &64&;&C error. 9t /ill e'ecute the program onl" if the
magic num8er is recogniFa8le.
Second? as time /ent on and different e'ecuta8le file formats /ere introduced? the magic
num8er not onl" told the kernel if it could e'ecute the file? 8ut also how to e'ecute the file.
-or e'ample? if "ou compile a program on an SC4 ;&69;O0%6 s"stem and carr" the 8inar"
o$er to a S"sVO0%6 U69; s"stem? the kernel /ill recogniFe the magic num8er and sa" [#haY
This is an '.out 8inar"YJ and configure itself to run /ith ;&69; compati8le s"stem calls.
)67
6ote that the kernel can onl" run 8inar" e'ecuta8le images. So ho/? "ou might ask? do
scripts get runB #fter all? 9 can t"pe [m".scriptJ at a shell prompt and 9 donJt get an
&64&;&C error. Script e'ecution is done not 8" the kernel? 8ut 8" the shell. The code in
the shell might look something likeK
OA tr" to run the program AO
e'ecl<program? 8asename<program=? <char A=*=\
OA the e'ec failedPma"8e it is a shell scriptB AO
if <errno `` &64&;&C=
e'ecl <RO8inOshS? RshS? R1cS? program? <char A=*=\
OA oh no mr 8illYY AO
perror<program=\
return 1)\
<This e'ample is highl" simplified. There is a lot
more in$ol$ed? 8ut this illustrates the point 9Jm
tr"ing to make.=
9f e'ecl<= is successful in starting the program then the code 8e"ond the e'ecl<= is ne$er
e'ecuted. 9n this e'ample? if /e can e'ecl<= the [programJ then none of the stuff 8e"ond it is
run. 9nstead the s"stem is off running the 8inar" [programJ.
9f? ho/e$er? the first e'ecl<= failed then this h"pothetical shell looks at /h" it failed. 9f the
e'ecl<= failed 8ecause [programJ /as not recogniFed as a 8inar" e'ecuta8le? then the shell
tries to run it as a shell script.
The +erkele" folks had a neat idea to e'tend ho/ the kernel starts up programs. The" hacked
the kernel to recogniFe the magic num8er [MYJ. <2agic num8ers are )618its and t/o %18it
characters makes )6 8its? rightB= .hen the [MYJ magic num8er /as recogniFed? the kernel
/ould read in the rest of the line and treat it as a command to run upon the contents of the
file. .ith this hack "ou could no/ do things likeK
MY O8inOsh
MY O8inOcsh
MY O8inOa/k 1-K
This hack has e'isted solel" in the +erkele" /orld? and has migrated to US3 kernels as part
of S"stem V Release 7. Prior to V.7? unless the $endor did some special $alue added? the
kernel does not ha$e the capa8ilit" of doing an"thing other than loading and starting a 8inar"
e'ecuta8le image.
6o/? lets re/ind a fe/ "ears? to the time /hen more and more folks running US3 8ased
unices /ere sa"ing [O8inOsh sucks as an interacti$e user interfaceY 9 /ant cshYJ. Se$eral
$endors did some $alue added magic and put csh in their distri8ution? e$en though csh /as
not a part of the US3 U69; distri8ution.
This? ho/e$er? presented a pro8lem. LetJs sa" "ou s/itch "our login shell to O8inOcsh. LetJs
further suppose that "ou are a cretin and insist upon programming csh scripts. YouJd
certainl" /ant to 8e a8le to t"pe [m".scriptJ and get it run? e$en though it is a csh script.
9nstead of pumping it through O8inOsh? "ou /ant the script to 8e started 8" runningK
e'ecl <RO8inOcshS? RcshS? R1cS? Rm".scriptS? <char A=*=\
)65
+ut /hat a8out all those e'isting scriptsPsome of /hich are part of the s"stem distri8utionB
9f the" started getting run 8" csh then things /ould 8reak. So "ou needed a /a" to run some
scripts through csh? and others through sh.
The solution introduced /as to hack csh to take a look at the first character of the script "ou
are tr"ing to run. 9f it /as a [MJ then csh /ould tr" to run the script through O8inOcsh?
other/ise it /ould run the script through O8inOsh. The e'ample code from the a8o$e might
no/ look something likeK
OA tr" to run the program AO
e'ecl<program? 8asename<program=? <char A=*=\
OA the e'ec failedPma"8e it is a shell scriptB AO
if <errno `` &64&;&C XX <fp ` fopen<program? RrS== Y` 6ULL= d
i ` getc<fp=\
<$oid= fclose<fp=\ if <i `` [MJ=
e'ecl <RO8inOcshS? RcshS? R1cS? program? <char A=*=\
else
e'ecl <RO8inOshS? RshS? R1cS? program? <char A=*=\
e
OA oh no mr 8illYY AO
perror<program=\
return 1)\
T/o important points. -irst? this is a [cshJ hack. 6othing has 8een changed in the kernel and
nothing has 8een changed in the other shells. 9f "ou tr" to e'ecl<= a script? /hether or not it
8egins /ith [MJ? "ou /ill still get an &64&;&C failure. 9f "ou tr" to run a script 8eginning
/ith [MJ from something other than csh <e.g. O8inOsh=? then it /ill 8e run 8" sh and not csh.
Second? the magic is that either the script 8egins /ith [MJ or it doesnJt 8egin /ith [MJ. .hat
makes stuff like [KJ and [K O8inOshJ at the front of a script magic is the simple fact that the" are
not [MJ. Therefore? all of the follo/ing are identical at the start of a scriptK
K
K O8inOsh
Z111 a 8lank line
K OusrOgamesOrogue
echo R3ee...9 /onder /hat shell 9 am running underBBBS
9n all these cases? all shells /ill tr" to run the script /ith O8inOsh.
Similarl"? all of the follo/ing are identical at the start of a scriptK
M
M O8inOcsh
MY O8inOcsh
)66
MY O8inOsh
M 3ee...9 /onder /hat shell 9 am running underBBB
#ll of these start /ith a [MJ. This means that the script /ill 8e run 8" csh on0 if "ou tr" to
start it from csh? other/ise it /ill 8e run 8" O8inOsh.
<6oteK if "ou are running ksh? su8stitute [kshJ for
[shJ in the a8o$e. The Dorn shell is theoreticall"
compati8le /ith +ourne shell? so it tries to run these scripts itself. Your mileage ma" $ar" on
some of the other a$aila8le shells such as Fsh? 8ash? etc.=
48$iousl"? if "ouJ$e got support for [MYJ in the kernel then the [MJ hack 8ecomes superfluous.
9n fact? it can 8e dangerous 8ecause it creates confusion o$er /hat should happen /ith [MY
O8inOshJ.
The [MYJ handling is 8ecoming more and more pre$elant. S"stem V Release 7 picks up a
num8er of the +erkele" features? including this. Some S"stem V Release 0., $endors are
hacking in some of the more $isi8le V.7 features such as this and tr"ing to con$ince "ou this
is sufficient and "ou donJt need things like real? /orking streams or d"namicall" adNusta8le
kernel parameters.
;&69; does not support [MYJ. The ;&69; O8inOcsh does ha$e the [MJ hack. Support for [MYJ
in ;&69; /ould 8e nice? 8ut 9 /ouldnJt hold m" 8reath /aiting for it.
Uni" ' $re<uently Asked >uestions 0A1 ?$re<uent
posting@
This article includes ans/ers toK
7.)= 5o/ do 9 read characters from a terminal /ithout reEuiring the user to hit R&TUR6B
7.,= 5o/ do 9 check to see if there are characters to 8e read /ithout actuall" readingB
7.0= 5o/ do 9 find the name of an open fileB
7.7= 5o/ can an e'ecuting program determine its o/n pathnameB
7.5= 5o/ do 9 use popen<= to open a process for reading #6 /ritingB
7.6= 5o/ do 9 sleep<= in a C program for less than one secondB
7.!= 5o/ can 9 get setuid shell scripts to /orkB
7.%= 5o/ can 9 find out /hich user or process has a file open or is using a particular file
s"stem <so that 9 can unmount itB=
7.(= 5o/ do 9 keep track of people /ho are fingering meB
7.)*= 9s it possi8le to reconnect a process to a terminal after it has 8een disconnected? e.g. after
starting a program in the 8ackground and logging outB
7.))= 9s it possi8le to Rsp"S on a terminal? displa"ing the output thatJs appearing on it on
another terminalB
)6!
9f "ouJre looking for the ans/er to? sa"? Euestion 7.5? and /ant to skip e$er"thing else? "ou can
search ahead for the regular e'pression R]7.5=S.
.hile these are all legitimate Euestions? the" seem to crop up in comp.uni'.Euestions or
comp.uni'.shell on an annual 8asis? usuall" follo/ed 8" plent" of replies <onl" some of /hich are
correct= and then a period of griping a8out ho/ the same Euestions keep coming up. You ma"
also like to read the monthl" article R#ns/ers to -reEuentl" #sked GuestionsS in the ne/sgroup
Rne/s.announce.ne/usersS? /hich /ill tell "ou /hat RU69;S stands for.
.ith the $ariet" of Uni' s"stems in the /orld? itJs hard to guarantee that these ans/ers /ill /ork
e$er"/here. Read "our local manual pages 8efore tr"ing an"thing suggested here. 9f "ou ha$e
suggestions or corrections for an" of these ans/ers? please send them to to tmatimaraisgtec.com.
7.)= 5o/ do 9 read characters from a terminal /ithout reEuiring the user
to hit R&TUR6B
Check out c8reak mode in +S? >9C#646 mode in S"sV.
9f "ou donJt /ant to tackle setting the terminal parameters "ourself <using the Rioctl<,=S
s"stem call= "ou can let the stt" program do the /ork 1 8ut this is slo/ and inefficient? and
"ou should change the code to do it right some timeK
Minclude Zstdio.hV
main<=
d
int c\
printf<R5it an" character to continuefnS=\
OA
ioctl<= /ould 8e 8etter here\ onl" laF"
programmers do it this /a"K
AO
s"stem<RO8inOstt" c8reakS=\ OA or Rstt" ra/S AO
c ` getchar<=\
s"stem<RO8inOstt" 1c8reakS=\
printf<RThank "ou for t"ping Tc.fnS? c=\
e'it<*=\
e
Se$eral people ha$e sent me $arious more correct solutions to this pro8lem. 9Jm sorr" that
9Jm not including an" of them here? 8ecause the" reall" are 8e"ond the scope of this list.
You might like to check out the documentation for the RcursesS li8rar" of porta8le screen
functions. 4ften if "ouJre interested in single1character 9O4 like this? "ouJre also interested in
doing some sort of screen displa" control? and the curses li8rar" pro$ides $arious porta8le
routines for 8oth functions.
7.,= 5o/ do 9 check to see if there are characters to 8e read /ithout
actuall" readingB
)6%
Certain $ersions of U69; pro$ide /a"s to check /hether characters
are currentl" a$aila8le to 8e read from a file descriptor. 9n
+S? "ou can use select<,=. You can also use the -946R&# ioctl?
/hich returns the num8er of characters /aiting to 8e read? 8ut
onl" /orks on terminals? pipes and sockets. 9n S"stem V Release
0? "ou can use poll<,=? 8ut that onl" /orks on streams. 9n ;eni'
and therefore Uni' S"sV r0., and later 1 the rdchk<= s"stem call reports /hether a read<=
call on a gi$en file descriptor /ill 8lock.
There is no /a" to check /hether characters are a$aila8le to 8e read from a -9L& pointer.
<You could poke around inside stdio data structures to see if the input 8uffer is nonempt"?
8ut that /ouldnJt /ork since "ouJd ha$e no /a" of kno/ing /hat /ill happen the ne't
time "ou tr" to fill the 8uffer.=
Sometimes people ask this Euestion /ith the intention of /riting
if <characters a$aila8le from fd=
read<fd? 8uf? siFeof 8uf=\ in order to get the effect of a non8locking read. This is not the
8est /a" to do this? 8ecause it is possi8le that characters /ill 8e a$aila8le /hen "ou test for
a$aila8ilit"? 8ut /ill no longer 8e a$aila8le /hen "ou call read. 9nstead? set the 4^6&L#Y
flag </hich is also called -6&L#Y under +S= using the -^S&T-L option of fcntl<,=.
4lder s"stems <Version !? 7.) +S= donJt ha$e 4^6&L#Y\ on these s"stems the closest
"ou can get to a non8locking read is to use alarm<,= to time out the read.
7.0= 5o/ do 9 find the name of an open fileB
9n general? this is too difficult. The file descriptor ma" 8e attached to a pipe or pt"? in /hich
case it has no name. 9t ma" 8e attached to a file that has 8een remo$ed. 9t ma" ha$e multiple
names? due to either hard or s"m8olic links.
9f "ou reall" need to do this? and 8e sure "ou think long and hard a8out it and ha$e decided
that "ou ha$e no choice? "ou can use find /ith the 1inum and possi8l" 1'de$ option? or "ou
can use ncheck? or "ou can recreate the functionalit" of one of these /ithin "our program.
Cust realiFe that searching a 6** mega8"te files"stem for a file that ma" not e$en e'ist is
going to take some time.
7.7= 5o/ can an e'ecuting program determine its o/n pathnameB
Your program can look at arg$H*I\ if it 8egins /ith a ROS? it is pro8a8l" the a8solute pathname
to "our program? other/ise "our program can look at e$er" director" named in the
en$ironment $aria8le P#T5 and tr" to find the first one that contains an e'ecuta8le file
/hose name matches "our programJs arg$H*I </hich 8" con$ention is the name of the file
8eing e'ecuted=. +" concatenating that director" and the $alue of arg$H*I "ouJd pro8a8l"
ha$e the right name.
You canJt reall" 8e sure though? since it is Euite legal for one program to e'ec<= another /ith
an" $alue of arg$H*I it desires. 9t is merel" a con$ention that ne/ programs are e'ecJd /ith
the e'ecuta8le file name in arg$H*I.
)6(
-or instance? purel" a h"pothetical e'ampleK
Minclude Zstdio.hV
main<=
d
e'ecl<ROusrOgamesOrogueS? R$i ThesisS? <char A=6ULL=\
e
The e'ecuted program thinks its name <its arg$H*I $alue= is
R$i ThesisS. <Certain other programs might also think that
the name of the program "ouJre currentl" running is R$i ThesisS?
8ut of course this is Nust a h"pothetical e'ample? donJt
tr" it "ourself K1=
7.5= 5o/ do 9 use popen<= to open a process for reading #6 /ritingB
The pro8lem /ith tr"ing to pipe 8oth input and output to an ar8itrar" sla$e process is that
deadlock can occur? if 8oth processes are /aiting for not1"et1generated input at the same
time. eadlock can 8e a$oided onl" 8" ha$ing +4T5 sides follo/ a strict deadlock1free
protocol? 8ut since that reEuires cooperation from the processes it is inappropriate for a
popen<=1like li8rar" function.
The [e'pectJ distri8ution includes a li8rar" of functions that a C programmer can call
directl". 4ne of the functions does the eEui$alent of a popen for 8oth reading and /riting. 9t
uses pt"s rather than pipes? and has no deadlock pro8lem. 9tJs porta8le to 8oth +S and SV.
See Euestion 0.( for more a8out [e'pectJ.
7.6= 5o/ do 9 sleep<= in a C program for less than one secondB
The first thing "ou need to 8e a/are of is that all "ou can specif" is a 29692U2 amount of
dela"\ the actual dela" /ill depend on scheduling issues such as s"stem load? and could 8e
ar8itraril" large if "ouJre unluck".
There is no standard li8rar" function that "ou can count on in all en$ironments for RnappingS
<the usual name for short sleeps=. Some en$ironments suppl" a Rusleep<n=S function /hich
suspends e'ecution for n microseconds. 9f "our en$ironment doesnJt support usleep<=? here
are a couple of implementations for +S and S"stem V en$ironments.
The follo/ing code is adapted from oug 3/"nJs S"stem V emulation support for 7+S
and e'ploits the 7+S select<= s"stem call. oug originall" called it [nap<=J\ "ou pro8a8l"
/ant to call it Rusleep<=S\
OA
usleepPsupport routine for 7.,+S s"stem call emulations
last editK ,(14ct1)(%7 # 3/"n
AO
e'tern int select<=\
)!*
int
usleep< usec = OA returns * if ok? else 1) AO
long usec\ OA dela" in microseconds AO
d
static struct OA [time$alJ AO
d
long t$^sec\ OA seconds AO
long t$^usec\ OA microsecs AO
e dela"\ OA ^select<= timeout AO
dela".t$^sec ` usec O )******L\
dela".t$^usec ` usec T )******L\
return select< *? <long A=*? <long A=*? <long A=*? Xdela" =\
e
4n S"stem V "ou might do it this /a"K
OA
su8seconds sleeps for S"stem V 1 or an"thing that has poll<=
on Li8es? 7O)O)(()
The +S analog to this function is defined in terms of microseconds /hile poll<= is defined in
terms of milliseconds. -or compati8ilit"? this function pro$ides accurac" Ro$er the long runS
8" truncating actual reEuests to milliseconds and accumulating microseconds across calls
/ith the idea that "ou are pro8a8l" calling it in a tight loop? and that o$er the long run? the
error /ill e$en out.
9f "ou arenJt calling it in a tight loop? then "ou almost certainl" arenJt making microsecond1
resolution reEuests an"/a"? in /hich case "ou donJt care a8out microseconds. #nd if "ou
did? "ou /ouldnJt 8e using U69; an"/a" 8ecause random s"stem indigestion <i.e.?
scheduling= can make mincemeat out of an" timing code.
Returns * if successful timeout? 1) if unsuccessful.
AO
Minclude Zpoll.hV
int
usleep<usec=
unsigned int usec\ OA microseconds AO
d
static su8total ` *\ OA microseconds AO
int msec\ OA milliseconds AO
OA [fooJ is onl" here 8ecause some $ersions of 5.0 ha$e
a 8ug /here the first argument to poll<= is checked
for a $alid memor" address e$en if the second argument is *.
AO
struct pollfd foo\
)!)
su8total :` usec\
OA if less then ) msec reEuest? do nothing 8ut remem8er it AO if <su8total Z )***= return<*=\
msec ` su8totalO)***\ su8total ` su8totalT)***\ return poll<Xfoo?<unsigned long=*?msec=\
e
#nother possi8ilit" for nap<=ing on S"stem V? and pro8a8l" other non1+S Unices is Con
UeeffJs s5nap package? posted to comp.sources.misc? $olume 7. 9t does reEuire a installing a
de$ice dri$er? 8ut /orks fla/lessl" once installed. <9ts resolution is limited to the kernel 5U
$alue? since it uses the kernel dela"<= routine.=
2an" ne/er $ersions of Uni' ha$e a nanosleep function.
7.!= 5o/ can 9 get setuid shell scripts to /orkB
H This is a long ans/er? 8ut itJs a complicated and freEuentl"1asked
Euestion. Thanks to 2aarten Litmaath for this ans/er? and for the RindirS program
mentioned 8elo/. I
Let us first assume "ou are on a U69; $ariant <e.g. 7.0+S or Sun4S= that kno/s a8out so1
called [e'ecuta8le shell scriptsJ.
Such a script must start /ith a line likeK
MYO8inOsh
The script is called [e'ecuta8leJ 8ecause Nust like a real <8inar"= e'ecuta8le it starts /ith a so1
called [magic num8erJ indicating the t"pe of the e'ecuta8le. 9n our case this num8er is [MYJ
and the 4S takes the rest of the first line as the interpreter for the script? possi8l" follo/ed 8"
) initial option likeK
MYO8inOsed 1f
Suppose this script is called [fooJ and is found in O8in?
then if "ou t"peK
foo arg) arg, arg0
the 4S /ill rearrange things as though "ou had t"pedK
O8inOsed 1f O8inOfoo arg) arg, arg0
There is one difference thoughK if the setuid permission 8it for [fooJ is set? it /ill 8e honored
in the first form of the command\ if "ou reall" t"pe the second form? the 4S /ill honor the
permission 8its of O8inOsed? /hich is not setuid? of course.
4D? 8ut /hat if m" shell script does 64T start /ith such a [MYJ line or m" 4S does not kno/
a8out itB
.ell? if the shell <or an"8od" else= tries to e'ecute it? the 4S /ill return an error indication?
as the file does not start /ith a $alid magic num8er. Upon recei$ing this indication the shell
#SSU2&S the file to 8e a shell script and gi$es it another tr"K
O8inOsh shell^script arguments
)!,
+ut /e ha$e alread" seen that a setuid 8it on [shell^scriptJ /ill 64T 8e honored in this caseY
Right? 8ut /hat a8out the securit" risks of setuid shell scriptsB
.ell? suppose the script is called [OetcOsetuid^scriptJ? starting /ithK
MYO8inOsh
6o/ let us see /hat happens if /e issue the follo/ing commandsK
L cd Otmp
L ln OetcOsetuid^script 1i
L P#T5`.
L 1i
.e kno/ the last command /ill 8e rearranged toK
O8inOsh 1i
+ut this command /ill gi$e us an interacti$e shell? setuid to the o/ner of the scriptY
-ortunatel" this securit" hole can easil" 8e closed 8" making the first lineK
MYO8inOsh 1
The [1[ signals the end of the option listK the ne't argument [1iJ /ill 8e taken as the name of
the file to read commands from? Nust like it shouldY
There are more serious pro8lems thoughK
L cd Otmp
L ln OetcOsetuid^script temp
L nice 1,* temp X
L m$ m"^script temp
The third command /ill 8e rearranged toK
nice 1,* O8inOsh 1 temp
#s this command runs so slo/l"? the fourth command might 8e a8le to replace the original
[tempJ /ith [m"^scriptJ +&-4R& [tempJ is opened 8" the shellY There are 7 /a"s to fi' this
securit" holeK
)= let the 4S start setuid scripts in a different? secure /a"
S"stem V R7 and 7.7+S use the Ode$Ofd dri$er to pass the interpreter a file
descriptor for the script
,= let the script 8e interpreted indirectl"? through a frontend that makes sure e$er"thing is all
right 8efore starting the real interpreter 1 if "ou use the [indirJ program from
comp.sources.uni' the setuid script /ill look like thisK
MYO8inOindir 1u
MBO8inOsh OetcOsetuid^script
0= make a [8inar" /rapperJK a real e'ecuta8le that is setuid and /hose onl" task is to
e'ecute the interpreter /ith the name of the script as an argument
)!0
7= make a general [setuid script ser$erJ that tries to locate the reEuested [ser$iceJ in a
data8ase of $alid scripts and upon success /ill start the right interpreter /ith the right
arguments.
6o/ that /e ha$e made sure the right file gets interpreted? are there an" risks leftB
Certainl"Y -or shell scripts "ou must not forget to set the P#T5 $aria8le to a safe path
e'plicitl". Can "ou figure out /h"B #lso there is the 9-S $aria8le that might cause trou8le if
not set properl". 4ther en$ironment $aria8les might turn out to compromise securit" as /ell?
e.g. S5&LL... -urthermore "ou must make sure the commands in the script do not allo/
interacti$e shell escapesY Then there is the umask /hich ma" ha$e 8een set to something
strange...
&tcetera. You should realise that a setuid script [inheritsJ all the 8ugs and securit" risks of
the commands that it callsY
#ll in all /e get the impression setuid shell scripts are Euite a risk" 8usinessY You ma" 8e
8etter off /riting a C program insteadY
7.%= 5o/ can 9 find out /hich user or process has a file open or is using
a particular file s"stem <so that 9 can unmount itB=
Use fuser <s"stem V=? fstat <+S=? ofiles <pu8lic domain= or pff <pu8lic domain=. These
programs /ill tell "ou $arious things a8out processes using particular files.
# port of the 7.0 +S fstat to "ni'? Sun4S and Ultri' can 8e found in archi$es of
comp.sources.uni'? $olume )%.
pff is part of the kstuff package? and /orks on Euite a fe/ s"stems.
9nstructions for o8taining kstuff are pro$ided in Euestion 0.)*.
9J$e 8een informed that there is also a program called lsof. 9 donJt kno/ /here it can 8e
o8tained.
2ichael -ink Z2ichael.-inkaui8k.ac.atV addsK
9f "ou are una8le to unmount a file s"stem for /hich a8o$e tools do not report an" open files
make sure that the file s"stem that "ou are tr"ing to unmount does not contain an" acti$e
mount points <df<)==.
7.(= 5o/ do 9 keep track of people /ho are fingering meB
3enerall"? "ou canJt find out the userid of someone /ho is fingering "ou from a remote
machine. You ma" 8e a8le to find out /hich machine the remote reEuest is coming from.
4ne possi8ilit"? if "our s"stem supports it and assuming the finger daemon doesnJt o8Nect? is
to make "our .plan file a Rnamed pipeS instead of a plain file. <Use [mknodJ to do this.=
You can then start up a program that /ill open "our .plan file for /riting\ the open /ill 8lock
until some other process <namel" fingerd= opens the .plan for reading. 6o/ "ou can feed
/hate$er "ou /ant through this pipe? /hich lets "ou sho/ different .plan information e$er"
)!7
time someone fingers "ou. 4ne program for doing this is the RplannerS package in $olume
7) of the comp.sources.misc archi$es.
4f course? this ma" not /ork at all if "our s"stem doesnJt
support named pipes or if "our local fingerd insists
on ha$ing plain .plan files.
Your program can also take the opportunit" to look at the output of RnetstatS and spot /here
an incoming finger connection is coming from? 8ut this /onJt get "ou the remote user.
3etting the remote userid /ould reEuire that the remote site 8e running an identit" ser$ice
such as R-C (0). There are no/ three R-C (0) implementations for popular +S machines?
and se$eral applications <such as the /uarchi$e ftpd= supporting the ser$er.
-or more information Noin the rfc(0)1users mailing list?
Vrfc(0)1users1reEuestakramden.acf.n"u.edu.
There are three ca$eats relating to this ans/er. The first is that man" 6-S s"stems /onJt
recogniFe the named pipe correctl". This means that tr"ing to read the pipe on another
machine /ill either 8lock until it times out? or see it as a Fero1length file? and ne$er print it.
The second pro8lem is that on man" s"stems? fingerd checks that the .plan file contains data
<and is reada8le= 8efore tr"ing to read it. This /ill cause remote fingers to miss "our .plan
file entirel".
The third pro8lem is that a s"stem that supports named pipes usuall" has a fi'ed num8er of
named pipes a$aila8le on the s"stem at an" gi$en time 1 check the kernel config file and
-9-4C6T option. 9f the num8er of pipes on the s"stem e'ceeds the -9-4C6T $alue? the
s"stem 8locks ne/ pipes until some8od" frees the resources. The reason for this is that
8uffers are allocated in a non1paged memor".
7.)*= 9s it possi8le to reconnect a process to a terminal after it has
8een disconnected? e.g. after starting a program in the 8ackground and logging outB
2ost $ariants of Uni' do not support RdetachingS and RattachingS processes? as operating
s"stems such as V2S and 2ultics support. 5o/e$er? there are three freel" redistri8uta8le
packages /hich can 8e used to start processes in such a /a" that the" can 8e later reattached
to a terminal.
The first is Rscreen?S /hich is descri8ed in the comp.sources.uni' archi$es as RScreen?
multiple /indo/s on a CRTS <see the Rscreen10.,S package in comp.sources.misc? $olume
,%.= This package /ill run on at least +S? S"stem V r0., and SC4 U69;.
The second is Rpt"?S /hich is descri8ed in the comp.sources.uni' archi$es as a package to
RRun a program under a pt" sessionS <see Rpt"S in $olume ,0=. pt" is designed for use under
+S1like s"stem onl".
The third is Rdislocate?S /hich is a script that comes /ith the
e'pect distri8ution. Unlike the pre$ious t/o? this should run on all U69; $ersions. etails
on getting e'pect can 8e found in Euestion 0.( .
6one of these packages is retroacti$e? i.e. "ou must ha$e started a process under screen or pt"
in order to 8e a8le to detach and reattach it.
)!5
7.))= 9s it possi8le to Rsp"S on a terminal? displa"ing the output
thatJs appearing on it on another terminalB
There are a fe/ different /a"s "ou can do this? although none of them is perfectK
ki8itF allo/s t/o <or more= people to interact /ith a shell <or an" ar8itar" program=.
Uses includeK
/atching or aiding another personJs terminal session\
recording a con$ersation /hile retaining the a8ilit" to scroll 8ack/ards? sa$e the
con$ersation? or e$en edit it /hile in progress\
teaming up on games? document editing? or other cooperati$e tasks /here each
person has strengths and /eakness that complement one another.
ki8itF comes as part of the e'pect distri8ution. See Euestion 0.(.
ki8itF reEuires permission from the person to 8e sp"ed upon. To sp" /ithout permission
reEuires less pleasant approachesK
You can /rite a program that rummages through Dernel structures and /atches the
output 8uffer for the terminal in Euestion? displa"ing characters as the" are output. This?
o8$iousl"? is not something that should 8e attempted 8" an"one /ho does not ha$e
e'perience /orking /ith the Uni' kernel. -urthermore? /hate$er method "ou come up
/ith /ill pro8a8l" 8e Euite non1porta8le.
9f "ou /ant to do this to a particular hard1/ired terminal all the time <e.g. if "ou /ant
operators to 8e a8le to check the console terminal of a machine from other machines=?
"ou can actuall" splice a monitor into the ca8le for the terminal. -or e'ample? plug the
monitor output into another machineJs serial port? and run a program on that port that
stores its input some/here and then transmits it out another port? this one reall" going to
the ph"sical terminal. 9f "ou do this? "ou ha$e to make sure that an" output from the
terminal is transmitted 8ack o$er the /ire? although if "ou splice onl" into the computer1
Vterminal /ires? this isnJt much of a pro8lem. This is not something that should 8e
attempted 8" an"one /ho is not $er" familiar /ith terminal /iring and such.
The latest $ersion of screen includes a multi1user mode.
Some details a8out screen can 8e found in Euestion 7.)*.
9f the s"stem 8eing used has streams <Sun4S? SVR7=? the ad$ise program that /as posted
in $olume ,% of comp.sources.misc can 8e used. #6 it doesnJt reEuirethat it 8e run
first <"ou do ha$e to configure "our s"stem in ad$ance to automaticall" push the ad$ise
module on the stream /hene$er a tt" or pt" is opened=.
Uni" ' $re<uently Asked >uestions 0;1 ?$re<uent
posting@
This article includes ans/ers toK
5.)= Can shells 8e classified into categoriesB
5.,= 5o/ do 9 RincludeS one shell script from /ithin another shell scriptB
5.0= o all shells ha$e aliasesB 9s there something else that can 8e usedB
)!6
5.7= 5o/ are shell $aria8les assignedB
5.5= 5o/ can 9 tell if 9 am running an interacti$e shellB
5.6= .hat RdotS files do the $arious shells useB
5.!= 9 /ould like to kno/ more a8out the differences 8et/een the $arious shells. 9s this
information a$aila8le some placeB
9f "ouJre looking for the ans/er to? sa"? Euestion 5.5? and /ant to skip e$er"thing else? "ou can
search ahead for the regular e'pression R]5.5=S.
.hile these are all legitimate Euestions? the" seem to crop up in comp.uni'.Euestions or
comp.uni'.shell on an annual 8asis? usuall" follo/ed 8" plent" of replies <onl" some of /hich are
correct= and then a period of griping a8out ho/ the same Euestions keep coming up. You ma"
also like to read the monthl" article R#ns/ers to -reEuentl" #sked GuestionsS in the ne/sgroup
Rne/s.announce.ne/usersS? /hich /ill tell "ou /hat RU69;S stands for.
.ith the $ariet" of Uni' s"stems in the /orld? itJs hard to guarantee that these ans/ers /ill /ork
e$er"/here. Read "our local manual pages 8efore tr"ing an"thing suggested here. 9f "ou ha$e
suggestions or corrections for an" of these ans/ers? please send them to to tmatimaraisgtec.com.
5.)= Can shells 8e classified into categoriesB
9n general there are t/o main class of shells. The first class are those shells deri$ed from the
+ourne shell /hich includes sh? ksh? 8ash? and Fsh. The second class are those shells deri$ed
from C shell and include csh and tcsh. 9n addition there is rc /hich most people consider to
8e in a Rclass 8" itselfS although some people might argue that rc 8elongs in the +ourne shell
class.
.ith the classification a8o$e? using care? it is possi8le to /rite scripts that /ill /ork for all
the shells from the +ourne shell categor"? and /rite other scripts that /ill /ork for all of the
shells from the C shell categor".
5.,= 5o/ do 9 RincludeS one shell script from /ithin another shell scriptB
#ll of the shells from the +ourne shell categor" <including rc= use the R.S command. #ll of
the shells from the C shell categor" use RsourceS.
5.0= o all shells ha$e aliasesB 9s there something else that can 8e usedB
#ll of the maNor shells other than sh ha$e aliases? 8ut the" donJt all /ork the same /a". -or
e'ample? some donJt accept arguments.
#lthough not strictl" eEui$alent? shell functions </hich e'ist in most shells from the +ourne
shell categor"= ha$e almost the same functionalit" of aliases. Shell functions can do things
that aliases canJt do. Shell functions did not e'ist in 8ourne shells deri$ed from Version !
Uni'? /hich includes S"stem 999 and +S 7.,. +S 7.0 and S"stem V shells do support shell
functions.
Use unalias to remo$e aliases and unset to remo$e functions.
)!!
5.7= 5o/ are shell $aria8les assignedB
The shells from the C shell categor" use Rset $aria8le`$alueS for $aria8les local to the shell
and Rseten$ $aria8le $alueS for en$ironment $aria8les. To get rid of $aria8les in these shells
use unset and unseten$. The shells from the +ourne shell categor" use R$aria8le`$alueS and
ma" reEuire an Re'port V#R9#+L&^6#2&S to place the $aria8le into the en$ironment. To
get rid of the $aria8les use unset.
5.5= 5o/ can 9 tell if 9 am running an interacti$e shellB
9n the C shell categor"? look for the $aria8le Lprompt.
9n the +ourne shell categor"? "ou can look for the $aria8le LPS)? ho/e$er? it is 8etter to
check the $aria8le L1. 9f L1 contains an [iJ? the shell is interacti$e. Test like soK
case L1 in
i= M do things for interacti$e shell
\\
A= M do things for non1interacti$e shell
\\
esac
5.6= .hat RdotS files do the $arious shells useB
#lthough this ma" not 8e a complete listing? this pro$ides the maNorit" of information.
csh
Some $ersions ha$e s"stem1/ide .cshrc and .login files. &$er" $ersion puts them in different
places.
Start1up <in this order=K
.cshrc 1 al/a"s\ unless the 1f option is used.
.login 1 login shells.
Upon terminationK
.logout 1 login shells.
4thersK
.histor" 1 sa$es the histor" <8ased on Lsa$ehist=.
tcsh
Start1up <in this order=K
OetcOcsh.cshrc 1 al/a"s.
OetcOcsh.login 1 login shells.
.tcshrc 1 al/a"s.
.cshrc 1 if no .tcshrc /as present.
.login 1 login shells
Upon terminationK
)!%
.logout 1 login shells.
4thersK
.histor" 1 sa$es the histor" <8ased on Lsa$ehist=.
.cshdirs 1 sa$es the director" stack.
sh
Start1up <in this order=K
OetcOprofile 1 login shells.
.profile 1 login shells.
Upon terminationK
an" command <or script= specified using the commandK
trap RcommandS *
ksh
Start1up <in this order=K
OetcOprofile 1 login shells.
.profile 1 login shells\ unless the 1p option is used.
L&6V 1 al/a"s? if it is set\ unless the 1p option is used.
OetcOsuid^profile 1 /hen the 1p option is used.
Upon terminationK
an" command <or script= specified using the commandK
trap RcommandS *
8ash
Start1up <in this order=K
OetcOprofile 1 login shells.
.8ash^profile 1 login shells.
.profile 1 login if no .8ash^profile is present.
.8ashrc 1 interacti$e non1login shells.
L&6V 1 al/a"s? if it is set.
Upon terminationK
.8ash^logout 1 login shells.
4thersK
.inputrc 1 Readline initialiFation.
Fsh
Start1up <in this order=K
.Fshen$ 1 al/a"s? unless 1f is specified.
.Fprofile 1 login shells.
.Fshrc 1 interacti$e shells? unless 1f is specified.
.Flogin 1 login shells.
)!(
Upon terminationK
.Flogout 1 login shells.
rc
Start1upK
.rcrc 1 login shells
5.!= 9 /ould like to kno/ more a8out the differences 8et/een the
$arious shells. 9s this information a$aila8le some placeB
# $er" detailed comparison of sh? csh? tcsh? ksh? 8ash? Fsh? and rc is a$aila8le $ia anon. ftp in
se$eral placesK
ftp.u/p.edu <,*7.(5.)6,.)(*=Kpu8O$iOdocsOshell1)**.+eta#.U
utsun.s.u1tok"o.ac.NpKmiscO$i1archi$eOdocsOshell1)**.+eta#.U
This file compares the flags? the programming s"nta'? inputOoutput redirection? and
parametersOshell en$ironment $aria8les. 9t doesnJt discuss /hat dot files are used and the
inheritance for en$ironment $aria8les and functions.
)%*