Anda di halaman 1dari 180

Using the Shell Prompt...................................................................................................

- 5 -
Running Commands from the Shell................................................................................- 5 -
Using Virtual Terminals..................................................................................................- 5 -
Choosing Your Shell.......................................................................................................- 6 -
Checking Your ogin Session.........................................................................................- 6 -
Checking !irectories and Permissions...........................................................................- " -
Checking S#stem $cti%it#...............................................................................................- & -
'(iting the Shell..............................................................................................................- ) -
Using the Shell in inu(...............................................................................................- *+ -
ocating Commands.....................................................................................................- *+ -
Starting ,ackground Processes.....................................................................................- *- -
Using .oreground and ,ackground Commands...........................................................- */ -
0orking 1ith the inu( .ile S#stem............................................................................- */ -
Using .ile-Redirection 2etacharacters........................................................................- *6 -
isting .iles...................................................................................................................- *" -
Cop#ing .iles................................................................................................................- *" -
2o%ing and Renaming .iles.........................................................................................- *& -
!eleting .iles and !irectories.......................................................................................- *& -
Changing !irectories....................................................................................................- *) -
2aking !irectories.......................................................................................................- *) -
Remo%ing !irectories...................................................................................................- *) -
2aking inks to .iles or !irectories............................................................................- *) -
Concatenating .iles.......................................................................................................- -+ -
Vie1ing .iles 1ith more and less.................................................................................- -+ -
Vie1ing the Start or 'nd of .iles..................................................................................- -* -
Searching .iles 1ith grep..............................................................................................- -* -
.inding .iles 1ith find and locate.................................................................................- -* -
,asic User and 3roup Concepts...................................................................................- -- -
Creating Users and 3roups...........................................................................................- -/ -
0orking 1ith .ile 41nership and Permissions............................................................- -/ -
2ounting and Unmounting .iles#stems.......................................................................- -6 -
S#stem information related commands.........................................................................- -" -
2emor# Reporting 1ith the free Command.................................................................- -" -
Virtual 2emor# Reporting 1ith the vmstat...............................................................- -" -
Reclaiming 2emor# 1ith the kill Command...............................................................- -& -
!etermining 5o1 ong inu( 5as ,een Running......................................................- -& -
Runle%els.......................................................................................................................- -) -
Using the %i Te(t 'ditor................................................................................................- /+ -
$utomated Tasks...........................................................................................................- /6 -
Cron...............................................................................................................................- /6 -
7.S...............................................................................................................................- /" -
Setting Up an 7.S Ser%er.............................................................................................- /" -
3etting the ser%ices Started...........................................................................................- 6- -
The !aemons................................................................................................................- 6- -
Verif#ing that 7.S is running.......................................................................................- 6/ -
Setting up an 7.S Client..............................................................................................- 66 -
*
2ounting Remote !irectories.......................................................................................- 66 -
3etting 7.S .ile S#stems to 8e 2ounted at ,oot Time...............................................- 65 -
2ount 4ptions..............................................................................................................- 66 -
79S................................................................................................................................- 6" -
5o1 79S 1orks.........................................................................................................- 6" -
5o1 79S: 1orks......................................................................................................- 6& -
2anaging S#stem ogs.................................................................................................- 6& -
ogrotate.......................................................................................................................- 5* -
The difference 8et1een hard and soft links..................................................................- 5/ -
.ile Compression and $rchi%ing...................................................................................- 5& -
Package 2anagement 1ith RP2..................................................................................- 6* -
Compiling from the original source..............................................................................- "+ -
#um................................................................................................................................- "6 -
s#sctl..............................................................................................................................- ") -
inu( Partitions.............................................................................................................- &+ -
Partition T#pes..............................................................................................................- &6 -
V2..............................................................................................................................- )+ -
U79; Sumar#...............................................................................................................- )6 -
Typographical conventions.....................................................................................- )6 -
9ntroduction...................................................................................................................- )6 -
The U79; operating s#stem.....................................................................................- )5 -
The kernel.............................................................................................................- )5 -
The shell................................................................................................................- )5 -
.iles and processes....................................................................................................- )6 -
The !irector# Structure............................................................................................- )6 -
Starting an ;terminal session...................................................................................- )6 -
Part 4ne.........................................................................................................................- )& -
*.* isting files and directories.................................................................................- )& -
ls <list=...................................................................................................................- )& -
*.- 2aking !irectories.............................................................................................- )) -
mkdir <make director#=.........................................................................................- )) -
*./ Changing to a different director#........................................................................- )) -
cd <change director#=............................................................................................- )) -
'(ercise *a............................................................................................................- )) -
*.6 The directories . and ...........................................................................................- )) -
*.5 Pathnames.........................................................................................................- *++ -
p1d <print 1orking director#=.............................................................................- *++ -
'(ercise *8..........................................................................................................- *+* -
*.6 2ore a8out home directories and pathnames...................................................- *+* -
Understanding pathnames...................................................................................- *+* -
> <#our home director#=......................................................................................- *+- -
Summar#.................................................................................................................- *+- -
Part T1o......................................................................................................................- *+/ -
-.* Cop#ing .iles....................................................................................................- *+/ -
cp <cop#=.............................................................................................................- *+/ -
'(ercise -a..........................................................................................................- *+/ -
-
-.- 2o%ing files......................................................................................................- *+/ -
m% <mo%e=...........................................................................................................- *+/ -
-./ Remo%ing files and directories.........................................................................- *+6 -
rm <remo%e=? rmdir <remo%e director#=...............................................................- *+6 -
'(ercise -8..........................................................................................................- *+6 -
-.6 !ispla#ing the contents of a file on the screen.................................................- *+5 -
clear <clear screen=..............................................................................................- *+5 -
cat <concatenate=.................................................................................................- *+5 -
less.......................................................................................................................- *+5 -
head.....................................................................................................................- *+5 -
tail........................................................................................................................- *+6 -
-.5 Searching the contents of a file.........................................................................- *+6 -
Simple searching using less................................................................................- *+6 -
grep <don@t ask 1h# it is called grep=..................................................................- *+6 -
1c <1ord count=..................................................................................................- *+" -
Summar#.................................................................................................................- *+& -
Part Three....................................................................................................................- *+& -
/.* Redirection........................................................................................................- *+& -
/.- Redirecting the 4utput......................................................................................- *+) -
'(ercise /a..........................................................................................................- *+) -
/./ Redirecting the 9nput.........................................................................................- **+ -
/.6 Pipes..................................................................................................................- *** -
'(ercise /8..........................................................................................................- *** -
Summar#.................................................................................................................- **- -
Part .our......................................................................................................................- **- -
6.* 0ildcards...........................................................................................................- **- -
The characters A and B.........................................................................................- **- -
6.- .ilename con%entions........................................................................................- **- -
6./ 3etting 5elp......................................................................................................- **/ -
4n-line 2anuals..................................................................................................- **/ -
$propos...............................................................................................................- **/ -
Summar#.................................................................................................................- **6 -
Part .i%e......................................................................................................................- **6 -
5.* .ile s#stem securit# <access rights=...................................................................- **6 -
$ccess rights on files...........................................................................................- **5 -
$ccess rights on directories.................................................................................- **5 -
Some e(amples....................................................................................................- **6 -
5.- Changing access rights......................................................................................- **6 -
chmod <changing a file mode=.............................................................................- **6 -
'(ercise 5a..........................................................................................................- **6 -
5./ Processes and Co8s............................................................................................- **" -
Running 8ackground processes...........................................................................- **" -
,ackgrounding a current foreground process.....................................................- **" -
5.6 isting suspended and 8ackground processes...................................................- **& -
5.5 Dilling a process................................................................................................- **& -
kill <terminate or signal a process=......................................................................- **& -
/
ps <process status=...............................................................................................- **) -
Summar#.................................................................................................................- **) -
Part Si(........................................................................................................................- *-+ -
4ther useful U79; commands...............................................................................- *-+ -
Euota....................................................................................................................- *-+ -
df.........................................................................................................................- *-+ -
du.........................................................................................................................- *-+ -
compress..............................................................................................................- *-* -
gFip......................................................................................................................- *-* -
file.......................................................................................................................- *-* -
histor#..................................................................................................................- *-* -
Part Se%en...................................................................................................................- *-- -
".* Compiling U79; soft1are packages................................................................- *-- -
Compiling Source Code......................................................................................- *-- -
make and the 2akefile........................................................................................- *-/ -
configure.............................................................................................................- *-/ -
".- !o1nloading source code.................................................................................- *-6 -
"./ '(tracting the source code................................................................................- *-6 -
".6 Configuring and creating the 2akefile.............................................................- *-5 -
".5 ,uilding the package.........................................................................................- *-5 -
".6 Running the soft1are........................................................................................- *-6 -
"." Stripping unnecessar# code...............................................................................- *-6 -
Part 'ight.....................................................................................................................- *-& -
&.* U79; Varia8les.................................................................................................- *-& -
&.- 'n%ironment Varia8les......................................................................................- *-& -
.inding out the current %alues of these %aria8les................................................- *-& -
&./ Shell Varia8les...................................................................................................- *-) -
.inding out the current %alues of these %aria8les................................................- *-) -
So 1hat is the difference 8et1een P$T5 and path B...........................................- *-) -
&.6 Using and setting %aria8les...............................................................................- *-) -
&.5 Setting shell %aria8les in the .cshrc file.............................................................- */+ -
&.6 Setting the path..................................................................................................- */* -
Uni( - .reEuentl# $sked Guestions <*= H.reEuent postingI........................................- */- -
Uni( - .reEuentl# $sked Guestions <-= H.reEuent postingI........................................- */" -
Uni( - .reEuentl# $sked Guestions </= H.reEuent postingI........................................- *5- -
Uni( - .reEuentl# $sked Guestions <6= H.reEuent postingI........................................- *6& -
Uni( - .reEuentl# $sked Guestions <5= H.reEuent postingI........................................- *"" -
6
Using the Shell Prompt
9f #our inu( s#stem has no graphical user interface <or one that isnJt 1orking at the
moment=? #ou 1ill most likel# see a shell prompt after #ou log in.? T#ping commands
from the shell 1ill pro8a8l# 8e #our primar# means of using the inu( 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 inu( 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 1ith Otmp as the current director# 1ould
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 1ith the shell? itJs eas# to
8egin 8# Nust t#ping a fe1 commands. Tr# some of the commands sho1n in the
remainder of this section to 8ecome familiar 1ith #our current shell en%ironment.
9n the e(amples that follo1? the L and M s#m8ols indicate a prompt. The prompt is
follo1ed 8# the command that #ou t#pe <and then #ou press 'nter or Return?
depending on #our ke#8oard=. The lines that follo1 sho1 the output resulting from
the command.
Using Virtual Terminals
2an# inu( s#stems? including .edora and Red 5at 'nterprise inu(? start multiple
%irtual terminals running on the computer. Virtual terminals are a 1a# to ha%e multiple
shell sessions open at once 1ithout ha%ing a 3U9 running.
You can s1itch 8et1een %irtual terminals much the same 1a# that #ou 1ould
s1itch 8et1een 1orkspaces on a 3U9. Press Ctrl:$lt:.* <or .-? ./? .6? and so on up to
.6 on .edora and other inu( s#stems= to displa# one of si( %irtual terminals.
The ne(t %irtual 1orkspace after the %irtual terminals is 1here 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 1ith four %irtual terminals? #ouJd return to the 3U9 8#
pressing Ctrl:$lt:.5.=
5
Choosing Your Shell
9n most inu( s#stems? #our default shell is the 8ash shell. To find out 1hat #our
current login shell is? t#pe the follo1ing 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 ne1 shellJs command <ksh? tcsh? csh? sh? 8ash? and so
forth= from the current shell.
2ost full inu( s#stems include all of the shells descri8ed in this section. 5o1e%er?
some smaller inu( distri8utions ma# include onl# one or t1o shells. The 8est 1a#
to find out if a particular shell is a%aila8le is to t#pe the command and see if the
shell starts.
You might 1ant to choose a different shell to use 8ecauseK
You are used to using U79; S#stem V s#stems <often ksh 8# default= or Sun
2icros#stems and other ,erkele# U79;Q8ased distri8utions <freEuentl# csh
8# default=? and #ou are more comforta8le using default shells from those
en%ironments.
You 1ant to run shell scripts that 1ere created for a particular shell en%ironment?
and #ou need to run the shell for 1hich the# 1ere 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# inu( Users 3roup prefers ksh o%er 8ash 8ecause he doesnJt
like the 1a# aliases are al1a#s set up 1ith 8ash.
9f #ou donJt like #our default shell? simpl# t#pe the name of the shell #ou 1ant 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 follo1ing as root user from a shellK
# usermod -s /bin/csh chris
Checking Your Login Session
0hen #ou log in to a inu( s#stem? inu( %ie1s #ou as ha%ing a particular identit#?
1hich includes #our username? group name? user 9!? and group 9!. inu( also keeps
track of #our login sessionK it kno1s 1hen #ou logged in? ho1 long #ou ha%e 8een
idle? and 1here #ou logged in from.
To find out information a8out #our identit#? use the id command as follo1sK
$ id
uid=501(chris) gid=105(sales) groups=105(sales),4(adm),(lp)
9n this e(ample? the username is chris? 1hich is represented 8# the numeric user
9! <uid= 5+*. The primar# group for chris is called sales? 1hich has a group 9!
<gid= of *+5. The user chris also 8elongs to other groups called adm <gid 6= 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 1ho command.
9n the follo1ing e(ample? the -u option sa#s to add information a8out idle
time and the process 9! and -5 asks that a header 8e printedK
$ who -H
!"#$ %&!$ '&#$ &(%$ )&( *+##$!'
chris tt,1 -an 1. /005 1 /01.
The output from this 1ho command sho1s that the user chris is logged in on tt#*
<1hich is the monitor connected to the computer=? and his login session 8egan at
-+K5" on Canuar# */. The 9!' time sho1s ho1 long the shell has 8een open 1ithout
an# command 8eing t#ped <the dot indicates that it is currentl# acti%e=. P9!
sho1s the process 9! of the userJs login shell. C422'7T 1ould sho1 the name of the
remote computer the user had logged in from? if that user had logged in from
another computer on the net1ork? or the name of the local ; displa# if #ou 1ere
using a Terminal 1indo1 <such as K+.+=.
Checking Directories and Permissions
$ssociated 1ith each shell is a location in the inu( file s#stem kno1n as the current
or working directory. 'ach user has a director# that is identified as the userJs
home director#. 0hen #ou first log in to inu(? #ou 8egin 1ith #our home director#
as the current director#.
0hen #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 1hen #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 1hat #our current director# is? t#pe the p1d commandK
$ pwd
/usr/bin
9n this e(ample? the currentO1orking director# is OusrO8in. To find out the name of
#our home director#? t#pe the echo command? follo1ed 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 follo1ed 8# a director#
name changes the current director# to the director# that #ou choose? simpl# t#ping
"
cd 1ith 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 1ithout a director# name. Using the -a option to
ls ena8les #ou to %ie1 the hidden files <dot files= as 1ell as all other files. 0ith the
-l option? #ou can see a long? detailed list of information on each file. <You can put
multiple single-letter options together after a single dash? for e(ample? -la.=
$ ls -la /home/chris
total 152
dr34r34r34 / chris sales 4056 #a, 1/ 1.055 1
dr34r74r74 . root root 4056 #a, 10 01045 11
7r37777777 1 chris sales //04 #a, 12 /10.0 1bash8histor,
7r37r77r77 1 chris sales /4 #a, 10 01050 1bash8logout
7r37r77r77 1 chris sales /.0 #a, 10 01050 1bash8pro9le
7r37r77r77 1 chris sales 1/4 #a, 10 01050 1bashrc
dr37r77r77 1 chris sales 4056 #a, 10 01050 1kde
7r37r37r77 1 chris sales 1452/ #a, 11 //045 letter
!ispla#ing a long list <-l option= of the contents of #our home director# sho1s #ou
more a8out file siFes and directories. The total line sho1s 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 1ith a d and each file 8egins 1ith a -=. The file
and director# names are sho1n in column ". 9n this e(ample? a dot <.= represents
OhomeOchris and t1o 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# non-dot file in this list is the one named letter1
The num8er of characters sho1n for a director# <6+)6 8#tes in these e(amples=
reflects the siFe of the file containing information a8out the director#. 0hile this
num8er can gro1 a8o%e 6+)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? inu( 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. inu( pro%ides tools for
listing running processes? monitoring s#stem usage? and stopping <or killing= processes
1hen necessar#.
&
The most common utilit# for checking running processes is the ps command. Use it
to see 1hich programs are running? the resources the# are using? and 1ho is running
them. 5ereJs an e(ample of the ps commandK
$ ps -au
:;$< )&( =*): =#$# >;? <;; ''@ ;'"' ;'"<' '&#$ *+##"!(
root /146 010 012 1502 1100 tt,p0 ; 14050 0000 login 77 Aake
Aake /14 010 01 12.6 10/0 tt,p0 ; 14050 0000 7bash
Aake /.10 010 01 /55/ 51/ tt,p0 < 120// 0000 ps Bau
9n this e(ample? the -a option asks to sho1 processes of all users 1ho are associated
1ith #our current terminal? and the -u option asks that usernames 8e sho1n?
as 1ell 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 sho1s that the
user named Nake logged in to the login process <1hich is controlled 8# the root
user=. The ne(t process sho1s that Nake is using a 8ash shell and has Nust run the
ps -au command. The terminal de%ice tt#p+ is 8eing used for the login session.
The ST$T column represents the state of the process? 1ith R indicating a currentl#
running process and S representing a sleeping process.
The US'R column sho1s the name of the user 1ho 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 runa1a# process.= The TCPU and T2'2 columns
sho1 the percentage of the processor and random access memor#? respecti%el#? that the
process is consuming. VSU <%irtual set siFe= sho1s the siFe of the image process
<in kilo8#tes=? and RSS <resident set siFe= sho1s the siFe of the program in memor#.
ST$RT sho1s the time the process 8egan running? and T92' sho1s the cumulati%e
s#stem time used.
$lso tr# t#ping top? !ree and vmstat commands.
!"iting the Shell
To e(it the shell 1hen #ou are done? t#pe e"it or press #trl$%&
YouJ%e Nust seen a fe1 commands that can help #ou Euickl# familiariFe #ourself
1ith #our inu( 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 1hat each
command does. There are also administrati%e commands in Os8in or OusrOs8in
directories.
Using the Shell in Linu"
0hen #ou t#pe a command in a shell? #ou can include other characters that change
or add to ho1 the command 1orks. 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 -la lists the contents of the current director#. The -l asks
for a detailed <long= list of information? and the -a asks that files 8eginning
1ith a dot <.= also 8e listed. 0hen a single option consists of a 1ord? it is usuall#
preceded 8# a dou8le dash <--=. .or e(ample? to use the help option on
man# commands? #ou enter --help on the command line.
You can use the --help option 1ith most commands to see the options and
arguments that the# support. .or e(ample? hostname --help.
(rgmentsP2an# 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 OetcOpass1d displa#s the contents of the OetcOpass1d file
on #our screen. 9n this case? OetcOpass1d is the argument.
Environment varia)lesPThe shell itself stores information that ma# 8e useful
to the userJs shell session in 1hat are called environment variables.
'(amples of en%ironment %aria8les include LS5' <1hich identifies the shell
#ou are using=? LPS* <1hich defines #our shell prompt=? and L2$9 <1hich
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 declare to list the current
en%ironment %aria8les. 4r #ou can t#pe echo $VALUE? 1here VALUE is
replaced 8# the name of a particular en%ironment %aria8le #ou 1ant 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 fe1. 2etacharacters are discussed later in this chapter.
Locating Commands
9f #ou kno1 the director# that contains the command #ou 1ant to run? one 1a# 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#
1ith a long path name. The 8etter 1a# is to ha%e commands stored in 1ellkno1n
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 follo1ingK
$ echo $PATH
/bin0/usr/bin0/usr/local/bin0/usr/bin/C110/usr/C11<6/bin0/home/chris/bin
5ere are some places #ou can look to supplement 1hat #ou learn in this chapterK
*+
Check the P$T5PT#pe echo $+(TH. You see a list of the directories containing
commands
that are immediatel# accessi8le to #ou. isting the contents of those directories
displa#s most standard inu( 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 sho1s options
a%aila8le 1ith each of them. <T#pe help W less to page through the list.= .or help
1ith a particular 8uilt-in command? t#pe help command? replacing command 1ith
the name that interests #ou. The help command 1orks 1ith the 8ash shell onl#.
Use --help 1ith the commandP2an# commands include a --help option that
#ou can use to get information a8out ho1 the command is used. .or e(ample? t#pe
date --help , less. The output sho1s not onl# options? 8ut also time formats #ou
can use 1ith the date command.
Use the man commandPTo learn more a8out a particular command? t#pe man
command. <Replace command 1ith the command name #ou 1ant.= $ description
of the command and its options appears on the screen.
$ type bash
bash is /bin/bash
To tr# out a 8it of command-line editing? t#pe the follo1ingK
$ 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 1hich #ou can go through the rest
of the output a line <press 'nter= or a page <press space 8ar= at a time <press G
1hen #ou are done=.
To %ie1 #our histor# list? use the histor# command. T#pe the command 1ithout
options or follo1ed 8# a num8er to list that man# of the most recent commands.
.or e(ampleK
$ history !
.2/ date
.2. ls /usr/bin D sort 7a D more
.24 man sort
.25 cd /usr/local/bin
.26 man more
.2 useradd 7m /home/chris 7u 101 chris
.25 histor, 2
$ num8er precedes each command line in the list. There are se%eral 1a#s to run a
command immediatel# from this list? includingK
YnPRun command num8er. Replace the n 1ith the num8er of the command
**
line? and that line is run. .or e(ample? hereJs ho1 to repeat the date command
sho1n as command num8er /&- in the preceding histor# listingK
$ "#$%
date
'hu "pr 1. /10.0006 )(' /006
E FFG <un previous command1 <uns the previous command line1 HereIs ho3
,ouId immediatel, run that same date command0
$ FF
date
'hu "pr 1. /10.00.5 )(' /006
Starting #ackground Processes
9f #ou ha%e programs that #ou 1ant to run 1hile #ou continue to 1ork 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 !ind -sr . -tmp-allsr!iles /
This e(ample command finds all files on #our inu( 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 1hich commands
#ou ha%e running in the 8ackground? use the No8s command? as follo1sK
$ &obs
J1K ;topped (tt, output) vi /tmp/m,9le
J/K <unning 9nd /usr 7print L /tmp/allusr9les M
J.K <unning nroN 7man /usr/man//O L/tmp/man/ M
J4K7 <unning nroN 7man /usr/man./O L/tmp/man. M
J5KP ;topped nroN 7man /usr/man4/O L/tmp/man4
'he 9rst Aob sho3s a te4t7editing command (vi) that & placed in the background
and stopped b, pressing *trlP? 3hile & 3as editing1 -ob / sho3s the 9nd command
& Aust ran1 -obs . and 4 sho3 nroN commands currentl, running in the background1
-ob 5 had been running in the shell (Qoreground) until & decided too man,
processes 3ere running and pressed *trlP? to stop Aob 5 until a Qe3 processes had
completed1
Using $oreground and #ackground Commands
Continuing 1ith 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? 1ith all te(t as it 1as 1hen #ou stopped
the %i No8.
TPRefers to the most recent command put into the 8ackground <indicated
8# the plus sign 1hen #ou t#pe the No8s command=. This action 8rings the
command to the foreground.
*-
TstringPRefers to a No8 1here the command 8egins 1ith a particular
string of characters. The string must 8e unam8iguous. <9n other 1ords?
t#ping T%i 1hen there are t1o %i commands in the 8ackground results in an
error message.=
TBstringPRefers to a No8 1here the command line contains a string at an#
point. The string must 8e unam8iguous or the match 1ill fail.
T--PRefers 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 -man man6OA VOtmpOman6
T#pe the follo1ingK
$ b' (*
$fter that? the No8 runs in the 8ackground. 9ts No8s entr# appears as follo1sK
H5I Running nroff -man man6OA VOtmpOman6 X
%orking &ith the Linu" $ile System
The inu( file s#stem is the structure in 1hich all the information on #our computer
is stored. .iles are organiFed 1ithin a hierarch# of directories. 'ach director#
can contain files? as 1ell as other directories.
9f #ou 1ere to map out the files and directories in inu(? it 1ould look like an
upside-do1n tree. $t the top is the root director#? 1hich is represented 8# a single
slash <O=. ,elo1 that is a set of common directories in the inu( s#stem? such as
8in? de%? home? li8? and tmp? to name a fe1. 'ach of those directories? as 1ell as
directories added to the root? can contain su8directories.
.igure --* illustrates ho1 the inu( file s#stem is organiFed as a hierarch#. To
demonstrate ho1 directories are connected? the figure sho1s a Ohome director#
that contains su8directories for three usersK chris? mar#? and tom. 0ithin 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 inu( directories that ma# interest #ou include the follo1ingK
O8inPContains common inu( user commands? such as ls? sort? date? and
chmod.
O8ootP5as the 8oota8le inu( 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 C!-R42 <cdA=. <Users normall# access these de%ices
directl# through the de%ice files.=
*/
OetcPContains administrati%e configuration files.
OhomePContains directories assigned to each user 1ith a login account.
OmediaPPro%ides a standard location for mounting and automounting
de%ices? such as remote file s#stems and remo%a8le media <1ith director#
names of cdrecorder? flopp#? and so on=.
OmntP$ common mount point for man# de%ices 8efore it 1as supplanted 8#
the standard Omedia director#. Some 8oota8le inu( 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 <$ Oproc-like file s#stem? ne1 in the inu( -.6 kernel and intended to
contain files for getting hard1are 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 1here #ou 1ould place files that #ou share as an .TP ser%er
<O%arOftp= or a 0e8 ser%er <O%arO111=. 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 8et1een the 8rackets? 1hich can
include a dash-separated range of letters or num8ers.
Tr# out some of these file-matching metacharacters 8# first going to an empt#
*6
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 fe1 commands sho1 #ou ho1 to
use shell metacharacters 1ith the ls command to match filenames. Tr# the follo1ing
commands to see if #ou get the same responsesK
$ ls a-
apple
$ ls '-
grape
grapeQruit
$ ls '-t
grapeQruit
$ ls -e-
apple grape grapeQruit 3atermelon
$ ls -n-
banana 3atermelon
The first e(ample matches an# file that 8egins 1ith an a <apple=. The ne(t e(ample
matches an# files that 8egin 1ith g <grape? grapefruit=. 7e(t? files 8eginning 1ith
g and ending in t are matched <grapefruit=. 7e(t? an# file that contains an e in the
name is matched <apple? grape? grapefruit? 1atermelon=. .inall#? an# file that
contains an n is matched <8anana? 1atermelon=.
5ere are a fe1 e(amples of pattern matching 1ith the Euestion mark <B=K
$ ls ....e
apple grape
$ ls '...e-
grape grapeQruit
The first e(ample matches an# fi%e-character file that ends in e <apple? grape=. The
second matches an# file that 8egins 1ith 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 3atermelon
$ ls /a'w0-/ne0
apple grape 3atermelon
9n the first e(ample? an# file 8eginning 1ith a? 8? or 1 is matched. 9n the second? an#
file that 8egins 1ith a? g? or 1 and also ends 1ith either n or e is matched. You can
also include ranges 1ithin 8rackets. .or e(ampleK
$ ls /a-'0-
apple banana grape grapeQruit
5ere? an# filenames 8eginning 1ith 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. 0ith files? #ou can use less than <Z= and greater than <V=
signs to direct data to and from files. 5ere are the file-redirection charactersK
ZP!irects the contents of a file to the command.
VP!irects the output of a command to a file? deleting the e(isting file.
VVP!irects 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 1here information is directed to and
from filesK
$ mail root 1 ~/2bashrc
$ man chmod | col -b 3 /tmp/chmod
$ echo 45 6nished the pro&ect on $7date89 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 -
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 follo1ing te(tJs 8eing added to the
userJs proNect fileK
9 finished the proNect on Sat Can -5 */K66K6) 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 1hat #ou reall# need to kno1 is that ls -l gi%es a RlongS
listing sho1ing the file siFes and permissions? and that the -a option sho1s e%en
RhiddenS filesPthose 1ith a dot at the start of their names. The shell e(pands the A
character to mean Ran# string of characters not starting 1ith [.J.S <See the discussion
of 1ildcards in the R$d%anced Shell .eaturesS section earlier in this chapter for more
information a8out ho1 and 1h# this 1orks.= Therefore? A.doc is interpreted as an#
filename ending 1ith .doc that does not start 1ith a dot and aA means Ran# filename
starting 1ith the letter a.S .or e(ampleK
ls -laP3i%es a long listing of all files in the current director# including RhiddenS
files 1ith names staring 1ith a dot
ls aAPists all files in the current director# 1hose names start 1ith a
ls -l A.docP3i%es a long listing of all files in the current director# 1hose
names end 1ith .doc
Copying $iles
The cp <cop#= command copies a file? files? or director# to another location. The
option -R allo1s #ou to cop# directories recursi%el# <in general? -R or -r in commands
*6
often has the meaning of Rrecursi%eS=. 9f the last argument to the cp command
is a director#? the files mentioned 1ill 8e copied into that director#. 7ote that 8#
default? cp 1ill Rclo88erS e(isting files? so in the second e(ample that follo1s? if there
is alread# a file called afile in the director# OhomeO8i8le? it 1ill 8e o%er1ritten 1ithout
asking for an# confirmation. Consider the follo1ing e(amplesK
cp afile afile.8akPCopies the file afile to a ne1 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 -a docs docs.8akPRecursi%el# copies the director# docs 8eneath the current
director# to a ne1 director# docs.8ak? 1hile preser%ing file attri8utes and
cop#ing all files including hidden files 1hose names start 1ith a dot. The -a
option implies the -R option? as a con%enience.
cp QiP,# default? if #ou cop# a file to a location 1here a file of the same
name alread# e(ists? the old file 1ill 8e silentl# o%er1ritten. The -i option
makes the command interacti%e\ in other 1ords it asks 8efore o%er1riting.
cp Q%P0ith the Q% <%er8ose= option? the cp command 1ill tell #ou 1hat it is
doing. $ great man# inu( commands ha%e a Q% option 1ith 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 follo1s? the file afile 1ill 8e renamed to the name 8file. 9n the
second e(ample? the file afile in the current director# 1ill 8e mo%ed to the director#
OtmpO.
m% afile 8filePRenames the e(isting file afile 1ith the ne1 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 1arnedK
r m is a dangerous command. 9t doesnJt reall# offer #ou a second chance. 0hen files
are deleted? the#Jre gone. You can use r m -i as in the last e(ample that follo1s.
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 *6= that makes the r m command
act like rm -i. 0e 1ould ad%ise at least to 8e careful a8out thisK 9t 1ill lull #ou into
a false sense of securit#? and 1hen #ouJre 1orking on a s#stem 1here this change
has not 8een made? #ou ma# regret it.
!oug 31#n? a 1ell-kno1n 9nternet personalit#? once said? RUni( 1as ne%er designed
to keep people from doing stupid things 8ecause that polic# 1ould 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 -rf O. <You ha%e to 8e logged in as a user? such as the
*"
root user? 1ho 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
1ill not remo%e directories unless #ou also specif# the -r <recursi%e=
option.
rm -rf doomedPRemo%es the director# doomed and e%er#thing in it.
rm Qi aAPRemo%es all files 1ith names 8eginning 1ith 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 inu( s#stems? #our prompt 1ill tell #ou 1hat director# #ouJre in
<depending on the setting #ouJ%e used for the PS* en%ironment %aria8le=.
5o1e%er\ if #ou e%er e(plicitl# need to kno1 1hat director# #ouJre in? #ou can use
the p1d command to identif# the 1orking director# for the current process <process
1orking director#? hence p1d=.
(aking Directories
You can use the mkdir <make director#= command to make directories. .or e(ampleK
mkdir photosP2akes a director# called photos 1ithin the current director#.
mkdir -p thisOthatOtheotherP2akes the nested su8directories named
1ithin the current director#.
Remoing Directories
The command rmdir 1ill remo%e a director# that is empt#.
(aking Links to $iles or Directories
9n inu(? #ou can use the ln <link= command to make links to a file or director#. $
file can ha%e an# num8er of so-called 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 no1 t1o names for the same file. 9f #ou edit afile? the changes
#ouJ%e made 1ill 8e in 8file. ,ut if #ou delete afile? 8file 1ill still e(ist\ it 1ill disappear
onl# 1hen 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 some1hat similar to
the use of RshortcutsS in 0indo1s. You can also create s#m8olic links to directories?
1hich can 8e %er# useful if #ou freEuentl# use a su8director# that is hidden
se%eral le%els deep 8elo1 #our home director#. 9n the last e(ample that follo1s?
#ou 1ill end up 1ith a s#m8olic link called useful in the current director#. Thus? the
command cd useful 1ill ha%e the same effect as cd docsOlinu(OsuseOuseful.
ln afile 8fileP2akes a RhardS link to afile called 8file
ln -s afile linkfileP2akes a s#m8olic link to afile called linkfile
ln -s 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 1ant to
%ie1 the contents of a short te(t file? the easiest thing to do is to cat it? 1hich sends
its contents to the shellJs standard output? 1hich is the shell in 1hich #ou t#ped the
cat command. 9f #ou cat t1o files? #ou 1ill see the contents of each fl#ing past on
the screen. ,ut if #ou 1ant to com8ine those t1o files into one? all #ou need to do is
cat them and redirect the output to the cat command to a file using V.
inu( has a sense of humor. The cat command displa#s files to standard output?
starting 1ith the first line and ending 1ith the last. The tac command <cat spelled
8ack1ard= displa#s files in re%erse order? 8eginning 1ith the last line and ending
1ith the first. The command tac is amusingK Tr# itY
cat OetcOpass1dPPrints OetcOpass1d to the screen
cat afile 8filePPrints the contents of afile to the screen follo1ed 8# the contents
of 8file
cat afile 8file V cfilePCom8ines the contents of afile and 8file and 1rites
them to a ne1 file? cfile
Vie&ing $iles &ith more and less
The more and less commands are kno1n as pagers 8ecause the# allo1 #ou to %ie1
the contents of a te(t file one screen at a time and to page for1ard and 8ack1ard
through the file <1ithout editing it=. The name of the more command is deri%ed from
the fact that it allo1s #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=. 7o1ada#s? the less command has man# added features?
including the fact that #ou can use ke#8oard shortcuts such as pressing the
letter b 1hen %ie1ing a file to mo%e 8ack1ard 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 1hile reading
it using less. ,oth more and less use the hot ke# q to e(it.
more OetcOpass1dPVie1s the contents of OetcOpass1d
*)
less OetcOpass1dPVie1s the contents of OetcOpass1d
Vie&ing the Start or !nd of $iles
The head and tail commands allo1 #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 -f to keep an e#e on a file as it gro1s. This is particularl# useful for 1atching
1hat is 8eing 1ritten to a log file 1hile #ou make changes in the s#stem.
Consider the follo1ing e(amplesK
head -n5 OetcOpass1dPPrints the first fi%e lines of the file OetcOpass1d to
the screen
tail -n5 OetcOpass1dPPrints the last fi%e lines of OetcOpass1d to the
screen
tail -f O%arOlogOmessagesPVie1s the last fe1 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 follo1 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(portsPooks for all lines in the file OetcOe(ports that
include the string bible
tail -*++ O%arOlogOapacheOaccess.logWgrep 6+6Pooks for the string 404?
the 1e8 ser%erJs Rfile not foundS code? in the last hundred lines of the 1e8
ser%er log
tail -*++ O%arOlogOapacheOaccess.logWgrep -% google8otPooks in the last
*++ lines of the 1e8 ser%er log for lines that donJt indicate accesses 8# the
3oogle search ro8ot
grep -% ]M OetcOapache-Ohttpd.confPooks 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 1a# 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 al1a#s 8e up-to-dateP
this depends on 1hether its data8ase is up-to-date. To use the locate command?
#ou need to ha%e the package findutils-locate installed.
find is a po1erful command 1ith man# options? including the a8ilit# to search for
files 1ith date stamps in a particular range <useful for 8ackups= and to search for
-+
files 1ith particular permissions? o1ners? and other attri8utes. The documentation
for find can 8e found in its info pagesK info find.
find .-name A.rpmP.inds RP2 packages in the current director#
find .Wgrep pageP.inds files in the current director# and its su8directories
1ith the string page in their names
locate tracerouteP.inds files 1ith names including the string traceroute an#1here
on the s#stem.
#asic User and )roup Concepts
inu( is a trul# multiuser operating s#stem. The concept of users and groups in
inu( is inherited from the Uni( tradition? and among other things pro%ides a %er#
clear and precise distinction 8et1een 1hat normal users can do and 1hat a pri%ileged
user can do <such as the root user? the superuser and ultimate administrator
on a inu( s#stem? 1ho 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 1h# inu( <and Uni( in general= is fundamentall#
secure in a 1a# that 2icrosoft 0indo1s is not. $lthough modern %ersions of
0indo1s ha%e a similar concept of users and groups? the associated concept of the
permissions 1ith 1hich a process can 8e run lea%es a lot to 8e desired. This is 1h#
there are so man# 0indo1s %ulnera8ilities that are 8ased on e(ploiting the scripting
capa8ilities of programs that are run 1ith user pri%ileges 8ut that turn out to 8e
capa8le of su8%erting the s#stem.
9f #ouJre interested in the differences 8et1een the maNor operating s#stems? 'ric
Ra#mond? noted open source guru and philosopher? offers some interesting
comparisons and discussion at 111.cat8.orgO>esrO1ritingsOtaoupO
htmlOch+/s+-.html.
'%er# inu( s#stem has a num8er of users accountsK Some of these are human
users? and some of them are s#stem users? 1hich 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
OetcOpass1d. ook at #our o1n entr# in OetcOpass1d\ it 1ill look something like thisK
rogerK(K*+++K*++KRoger 0hittakerKOhomeOrogerKO8inO8ash
This sho1s? among other things? that the user 1ith username roger has the real
name Roger 0hittaker? that his home director# is OhomeOroger? and that his
default shell is O8inO8ash <the 8ash shell=.
There 1ill 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? 1hich looks after mail. This user canJt log in 8ecause
its shell is O8inOfalse? 8ut its home director# is O%arOspoolOpostfi(? and it o1ns
the spool directories in 1hich mail 8eing sent and deli%ered is held. The fact that
these directories are o1ned 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 1hole s#stem. Similar s#stem users e(ist for the 1e8 ser%er
<the user 111run= and %arious other ser%ices. You 1onJt often need to consider
these? 8ut it is important to understand that the# e(ist and that the correct o1nerships
of certain files and directories 8# these users is part of the o%erall securit#
model of the s#stem as a 1hole.
'ach user 8elongs to one or more groups. The groups on the s#stem are listed in the
file OetcOgroups. To find out 1hat groups #ou 8elong to? #ou can simpl# t#pe the
command grops <alternati%el# look at the file OetcOgroup and look for #our username=.
,# default? on a SUS' s#stem? #ou 1ill find that #ou 8elong to the group
users and also to a fe1 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
<1hich is arranged through file permissions as #ou shall see later in this chapter=.
Creating Users and )roups
The useradd command has options that allo1 #ou to specif# the groups to 1hich
the ne1 user 1ill 8elongK
useradd -c Guest User u 5555 g 500 G 501 m d /home/guest s /bin/bash p
password guest
9 1ouldnJt recommend to add the users directl# in the OetcOpass1d file unless #ou ha%e
some e(perience in inu(. $ltough if #ou choose to do so please check the OetcOgroups
and OetcOshado1 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 1hat these
commands do.
To %erif# user logged on the s#stem #ou can tr#
$ last
@ou might also 3ant to see 3hat commands like 0 who, whoami: id do1
%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 o1ned 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 1ho is tr#ing to access them.
The section that follo1s is a crash course in file permissions\ 1e go into greater
detail in Chapter */.
9f #ou look at a %ariet# of files and directories from across the s#stem and list them
1ith the ls -l command? #ou can see different patterns of o1nership 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 ten-place string? the num8er
of links to the file? the o1nership 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 1hat the user can do 1ith the file? 1hat mem8ers of
the group can do 1ith the file? and 1hat others can do 1ith the file? respecti%el#. 9n
most cases? these permissions are represented 8# the presence or a8sence of the
letters r <read=? 1 <1rite=? and ( <e(ecute= in the three positions. SoK
r1( means permission to read? 1rite? and e(ecute
r-- means permission to read 8ut not to 1rite or e(ecute
r-( means permission to read and e(ecute 8ut not to 1rite
Permission to 1rite to a file includes the right to o%er1rite 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 1ritten 8# its o1ner <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 1rita8le? and can 8e read onl# 8# its o1ner <roger=.
'%en roger 1ould ha%e to change the permissions on this file to 8e a8le to 1rite it.
ls -l /etc/passwd
-rw-r--r-- 1 root root 1598 2004-0 5-17 19:36 /etc/passwd
This is the pass1ord filePit is o1ned 8# root <and the group root to 1hich onl#
root 8elongs=? is reada8le 8# an#one? 8ut is group 1rita8le onl# 8# root.
ls -l /etc/shadow
-rw-r----- 1 root shadow 796 2004-0 5-17 19:36 /etc/shadow
This is the shado1 file? 1hich holds the encr#pted pass1ords for users. 9t can 8e
read onl# 8# root and the s#stem group shado1 and can 8e 1ritten 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 1ritten 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 -d flag to the ls command and the d in the
first position in the permissions=. 9t can 8e read and 1ritten 8# the root user? and
read and e(ecuted 8# e%er#one. 0hen 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#1
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#. 7o user apart from
root can access it in an# 1a#.
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 1here until no1 1e sa1 an (.
This indicates that the file runs 1ith the permissions of its o1ner <root= e%en 1hen
it is e(ecuted 8# another userK Such a file is kno1n 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 1a# could 8e found to make such a file
perform a task other than 1hat it 1as 1ritten for.
ls -l alink
lrwxrwxrwx 1 roger users 8 2004-0 5-17 22:19 alink -> file.bz2
7ote the lin the first positionK This is a s#m8olic link to file.8F- in the same director#.
;umerical Permissions
4n man# occasions 1hen permissions are discussed? #ou 1ill see them 8eing
descri8ed in a three-digit numerical form <sometimes more digits for e(ceptional
cases=? such as 666. 9f a file has permissions 666? it has read and 1rite permissions
for the o1ner and read permissions for the group and for others. This 1orks
8ecause inu( 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 o1ner? group? and others? a read permission is represented 8# 6 <the
high 8it of a /-8it octal %alue=? a 1rite permission is represented 8# - <the middle
8it of a /-8it octal %alue=? and an e(ecute permission is represented 8# * <the lo1
8it of a /-8it octal %alue=.
<han'in' Ownership and Permissions
You can change the o1nership of a file 1ith the command cho1n. 9f #ou are logged
in as root? #ou can issue a command like thisK
cho1n harpoKusers file.t(t
This changes the o1nership of the file file.t(t to the user harpo and the group
users.
To change the o1nership of a director# and e%er#thing in it? #ou can use the command
1ith the -R <recursi%e= option? like thisK
-6
cho1n -R harpoKusers OhomeOharpoOsome^director#O
The chmod command is used to change file permissions. You can use chmod 1ith
8oth the numerical and the r1( notation 1e discussed earlier in the chapter. $gain?
this is easiest to follo1 8# looking at a fe1 e(amplesK
chmod u:( afileP$dds e(ecute permissions for the o1ner of the file
chmod g:r afileP$dds read permissions for the group o1ning the file
chmod o-r afilePRemo%es read permission for others
chmod a:1 afileP$dds 1rite permissions for all
chmod 666 afilePChanges the permissions to 666 <o1ner can read and
1rite\ group mem8ers and others can onl# read=
chmod "55 afilePChanges the permissions to "55 <o1ner can read? 1rite
and e(ecute\ group mem8ers and others can onl# read and e(ecute=
9f #ou use chmod 1ith the r1( notation? u means the o1ner? g means the group? o
means others? and a means all. 9n addition? : means add permissions? and - means
remo%e permissions? 1hile r? 1? and ( still represent read? 1rite? and e(ecute?
respecti%el#. 0hen setting permissions? #ou can see the translation 8et1een the
t1o notations 8# e(ecuting the chmod command 1ith the -% <%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 1hat #ou need to do to make the files it contains a%aila8le?
and the mount command is 1hat #ou use to do that. 9n inu(? 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? 1hich is usuall# an empt# director#.
To find out 1hat is currentl# mounted? simpl# t#pe the command mont on its o1n.
0e discuss the mount command further in Chapters *6 and --.
SUS' inu( no1 uses su8fs to mount remo%a8le de%ices such as C!-R42s 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! 1ill 8e %isi8le.
mount *)-.*6&.*.*KOhomeO8i8leO OmntP2ounts the remote net1ork
files#stem OhomeO8i8leO from the machine *)-.*6&.*.* on the mount point
Omnt
mount Ode%Ohda/ OusrOlocalP2ounts the disk partition Ode%Ohda/ on the
mount point OusrOlocal
umount OmntPUnmounts 1hate%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 ,ou 9nd some inQormation about the s,stem status1
(emory Reporting &ith the free Command
The free command sho1s 8reakdo1ns of the amounts and totals of free and used
memor#? including #our s1apfile usage. This command has se%eral command-line
options? 8ut is eas# to run and understand? for e(ampleK
= ree
total used Qree shared buNers cached
#em0 .025/ /2004 /222 141./ .104 10444
7/P buNers0 14456 164.6
;3ap0 .4/62 564 /6.04
This sho1s a /-2, s#stem 1ith /62, s1ap space. 7otice that nearl# all the s#stem
memor# is 8eing used? and nearl# &2, of s1ap space has 8een used.
,# default? the free command displa#s memor# in kilo8#tes? or *+-6-8#te notation. You
can use the -8 option to displa# #our memor# in 8#tes? or the -m option to displa#
memor# in mega8#tes. You can also use the free command to constantl# monitor ho1
much memor# is 8eing used through the -s command. This is hand# as a real-time
monitor if #ou specif# a .+*-second update and run the free command in a terminal
1indo1 under ;**.
Virtual (emory Reporting &ith the >mstat
Command
The %mstat is a general-purpose monitoring program? 1hich offers real-time 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 1ithout an# command-line
options? #ouJll get a one-time snapshot? for e(ampleK
# >mstat
procs memor, s3ap io s,stem cpu
r b 3 s3pd Qree buN cache si so bi bo in cs us s, id
0 0 0 462 1060 4/22 1055/ 1 1 10 1 1.4 62 . / 56
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 1hat is going on 1ith #our
computer can help #ou if #ouJre tr#ing to find out 1h# #our computer suddenl# slo1s
do1n? or 1h# 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 1ith
the kill command. ,# default? the ps command lists processes #ou o1n and 1hich #ou
can kill? for e(ampleK
# ps
)&( ''@ ;'"' '&#$ *+##"!(
.6 p0 ; 0000 bash
521 p0 ; 0001 r4vt
52/ p1 ; 0000 (bash)
4 p0 ; 0000 (appli4)
205 p0 ; 0012 netscape inde41html
210 p0 ; 0000 (dns helper)
545 p0 < 0000 ps
The ps command 1ill list the currentl# running programs and the programJs process
num8er? or P9!. You can use this information to kill a process 1ith
# ?ill -@ $A@
$lso #ou should tr# out the top command and see 1hat it sho1s.
Determining +o& Long Linu" +as #een Running
with the uptime and w <ommands
The uptime command sho1s #ou ho1 long inu( has 8een running? ho1 man# users are
on? and three s#stem load a%erages? for e(ampleK
M ptime
*-K66am up &K*6? / users? load a%erageK +.**? +.*+? +.+6
9f this is too little information for #ou? tr# the 1 command? 1hich first sho1s the same
information as the uptime command? and then lists 1hat currentl# logged-in users are
doingK
# w
1/042am up 20/0, . users, load average0 0114, 0105, 0105
:;$< ''@ R<+# %+S&!T &(%$ -*): )*): UH"'
bball tt,p0 localhost1locald 504pm 15100s 01.2s 0116s bash
bball tt,p/ localhost1locald 1/042am 0100s 0116s 0102s 3
The 1 command gi%es a little more information? and it is especiall# helpful if #ou 1ould
like to monitor a 8us# s#stem 1ith a num8er of users.
Dernel %ersion and other related information <like hostname? ne1 mail? date? architecture=
can 8e found easil# 1ithK
-"
#uname Ba
ots of hard1are information #ou can get 1ithK
#dmidecode
4r
#lspci
Ror the pci installed on the s,stem or ma,be Qor devices0
#lsde>
(isk usage can be print 3ith0
#d Bh
Rile disk usage can be also checked 3ith0
#du -h
Runleels
inu( s#stems t#picall# use se%en different runle%els? 1hich define 1hat 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 do1n? and runle%el * <or S=
represents single-user mode. Runle%els - through 5 are multiuser modes? and runle%el 6
is the _re8oot_ le%el. !ifferent inu( %ariations ma# not use all runle%els? 8ut t#picall#?
runle%el - is multiuser te(t 1ithout 7.S? runle%el / is multiuser te(t? and runle%el 5 is
multiuser 3U9.
'ach runle%el has its o1n director# that defines 1hich ser%ices start and in 1hat order.
You@ll t#picall# find these directories at OetcOrc.dOrcB.d? 1here 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 1ith the _start_ parameter 1hen starting that runle%el. ike1ise?
there ma# 8e another s#mlink pointing to the same initscript 1ith the name D&&s#slog\
init 1ould e(ecute this script 1ith the _stop_ parameter 1hen e(iting the runle%el.
-&
The num8er follo1ing the S or D determines the order in 1hich init should start or stop
the ser%ice in relation to other ser%ices. You can see 8# the num8ers associated 1ith the
s#slog ser%ice that s#slog starts fairl# earl# in the 8oot process? 8ut it stops late in the
shutdo1n 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 1hich init starts
ser%ices 8# naming s#mlinks accordingl#. 9t@s also eas# to add in ne1 ser%ices 8#
s#mlinking to the master initscript.
To find out in 1hich runle%el #ou are Nust t#peK
Mrnlevel
4r
Mwho 0r
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 inu( for an# period of time and not need to use a te(t
editor. This is 8ecause most inu( configuration files are plain te(t files that #ou
1ill almost certainl# need to change manuall# at some point.
9f #ou are using a 3U9? #ou can run gedit? 1hich is fairl# intuiti%e for editing te(t.
ThereJs also a simple te(t editor #ou can run from the shell called nano. 5o1e%er?
most inu( shell users 1ill 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 character-8ased connection o%er a net1ork <using
telnet or ssh? for e(ample=Pno 3U9 is reEuired. The# also each contain tons of
features? so #ou can continue to gro1 1ith them.
This section pro%ides a 8rief tutorial on the %i te(t editor? 1hich #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 follo1ing commandK
$ >i /tmp/test
The 8o( at the top represents 1here #our cursor is. The 8ottom line keeps #ou
informed a8out 1hat is going on 1ith #our editing <here #ou Nust opened a ne1
file=. 9n 8et1een? there are tildes <>= as filler 8ecause there is no te(t in the file #et.
7o1 hereJs the intimidating partK There are no hints? menus? or icons to tell #ou
1hat 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 inu( isnJt friendl#.
The first things #ou need to kno1 are the different operating modesK command and
input. The %i editor al1a#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 t1o letters and an optional
num8er= to tell %i 1hat #ou 1ant to do. Case is important? so use uppercase and
lo1ercase e(actl# as sho1n in the e(amplesY To get into input mode? t#pe an input
command. To start out? t#pe either of the follo1ingK
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 keysP2o%e the cursor up? do1n? 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 1ith h <left=? l <right=? N <do1n=? or k <up=.
wP2o%es the cursor to the 8eginning of the ne(t 1ord.
)P2o%es the cursor to the 8eginning of the pre%ious 1ord.
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 upper-left 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 lo1er-left corner of the screen <last line on the
screen=.
The onl# other editing #ou need to kno1 is ho1 to delete te(t. 5ere are a fe1 %i
commands for deleting te(tK
(P!eletes the character under the cursor.
;P!eletes the character directl# 8efore the cursor.
d1P!eletes from the current character to the end of the current 1ord.
dLP!eletes from the current character to the end of the current line.
d+P!eletes from the pre%ious character to the 8eginning of the current line.
To 1rap things up? use the follo1ing 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 1orks 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 1a# to
e(it and a8andon #our changes. The file re%erts to the most recentl# changed %ersion.
So? if #ou Nust did a K1? #ou are stuck 1ith the changes up to that point. 9f #ou
Nust 1ant to undo a fe1 8ad edits? press u to 8ack out of changes.
/+
You ha%e learned a fe1 %i editing commands. 9 descri8e more commands in the follo1ing
sections. .irst? ho1e%er? here are a fe1 tips to smooth out #our first trials
1ith %iK
EscPRemem8er that 'sc gets #ou 8ack to command mode. <9J%e 1atched
people press e%er# ke# on the ke#8oard tr#ing to get out of a file.= 'sc follo1ed
8# UU gets #ou out of command mode? sa%es the file? and e(its.
PPress U to undo the pre%ious change #ou made. Continue to press u to
undo the change 8efore that? and the one 8efore that.
#trl$6P9f #ou decide #ou didnJt 1ant to undo the pre%ious command? use
Ctrl:R for Redo. 'ssentiall#? this command undoes #our undo.
#aps LockP,e1are of hitting Caps ock 8# mistake. '%er#thing #ou t#pe in
%i has a different meaning 1hen the letters are capitaliFed. You donJt get a
1arning that #ou are t#ping capitalsPthings Nust start acting 1eird.
#trl$7PPage ahead? one page at a time.
#trl$8PPage 8ack? one page at a time.
#trl$%PPage ahead one-half page at a time.
#trl$9PPage 8ack one-half 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. .ollo1 the slash or Euestion mark 1ith a pattern
<string of te(t= to search for1ard or 8ack1ard? respecti%el#? for that pattern. 0ithin
the search? #ou can also use metacharacters. 5ere are some e(amplesK
OhelloPSearches for1ard for the 1ord hello.
Bgood8#ePSearches 8ack1ard for the 1ord good8#e.
OThe.AfootPSearches for1ard for a line that has the 1ord The in it and
also? after that at some point? the 1ord foot.
BHpPIrintPSearches 8ack1ard for either print or Print. Remem8er that
case matters in inu(? so make use of 8rackets to search for 1ords that could
ha%e different capitaliFation.
You can precede most %i commands 1ith num8ers to ha%e the command repeated
that num8er of times. This is a hand# 1a# to deal 1ith se%eral lines? 1ords? or characters
at a time. 5ere are some e(amplesK
/d1P!eletes the ne(t three 1ords.
5clPChanges the ne(t fi%e letters <that is? remo%es the letters and enters
input mode=.
*-NP2o%es do1n *- 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.
/*
Automated Tasks
9n inu(? tasks can 8e configured to run automaticall# 1ithin a specified period of time?
on a specified date? or 1hen the s#stem load a%erage is 8elo1 a specified num8er. Red
5at 'nterprise inu( is pre-configured 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 inu( comes 1ith 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 1eek? and 1eek.
Cron assumes that the s#stem is on continuousl#. 9f the s#stem is not on 1hen a task is
scheduled? it is not e(ecuted.
To use the cron ser%ice? the %i(ie-cron RP2 package must 8e installed and the crond
ser%ice must 8e running. To determine if the package is installed? use the rpm -E %i(ie-
cron command. To determine if the ser%ice is running? use the command Os8inOser%ice
crond status.
#on!igring #ron Tasks
The main configuration file for cron? OetcOcronta8? contains the follo1ing linesK
S5'`O8inO8ash
P$T5`Os8inKO8inKOusrOs8inKOusrO8in
2$9T4`root
542'`O
M run-parts
+* A A A A root run-parts OetcOcron.hourl#
+- 6 A A A root run-parts OetcOcron.dail#
-- 6 A A + root run-parts OetcOcron.1eekl#
6- 6 * A A root run-parts OetcOcron.monthl#
The first four lines are %aria8les used to configure the en%ironment in 1hich the cron
tasks are run. The S5' %aria8le tells the s#stem 1hich shell en%ironment to use <in
this e(ample the 8ash shell=? 1hile the P$T5 %aria8le defines the path used to e(ecute
/-
commands. The output of the cron tasks are emailed to the username defined 1ith the
2$9T4 %aria8le. 9f the 2$9T4 %aria8le is defined as an empt# string <2$9T4`__=?
email is not sent. The 542' %aria8le can 8e used to set the home director# to use 1hen
e(ecuting commands or scripts.
'ach line in the OetcOcronta8 file represents a task and has the follo1ing formatK minute
hour da# of month month da# of 1eek command
.ields
M :---------------- minute <+ - 5)=
M W :------------- hour <+ - -/=
M W W :---------- da# of month <* - /*=
M W W W :------- month <* - *-=
M W W W W :---- da# of 1eek <+ - 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 -/
da# of month P an# integer from * to /* <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 1eek P an# integer from + to "? 1here + or " represents Sunda# <or the short
name of the 1eek 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
1ithin the constraints of the other %alues.
$ h#phen <-= 8et1een integers specifies a range of integers. .or e(ample? *-6 means the
integers *? -? /? and 6.
$ list of %alues separated 8# commas <?= specifies a list. .or e(ample? /? 6? 6? & indicates
those four specific integers.
The for1ard slash <O= can 8e used to specif# step %alues. The %alue of an integer can 8e
skipped 1ithin a range 8# follo1ing the range 1ith OZintegerV. .or e(ample? +-5)O- can
8e used to define e%er# other minute in the minute field. Step %alues can also 8e used
//
1ith an asterisk. .or instance? the %alue AO/ can 8e used in the month field to run the task
e%er# third month.
$n# lines that 8egin 1ith a hash mark <M= are comments and are not processed.
$s sho1n in the OetcOcronta8 file? the run-parts script e(ecutes the scripts in the
OetcOcron.hourl#O? OetcOcron.dail#O? OetcOcron.1eekl#O? and OetcOcron.monthl#O directories on
an hourl#? dail#? 1eekl#? 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#? 1eekl#? 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 /K/+$2 in the file OtmpOmeminfo
/+ / A A mon cat OprocOmeminfo VV OtmpOmeminfo
M run custom script the first da# of e%er# month at 6K*+$2
*+ 6 * A A OrootOscriptsO8ackup.sh
#ronta) E"amples
Users other than root can configure cron tasks 8# using the cronta8 utilit#. $ll user-
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 -e to edit the user@s cronta8 using the editor specified 8#
the V9SU$ or '!9T4R en%ironment %aria8le. The file uses the same format as
OetcOcronta8. 0hen the changes to the cronta8 are sa%ed? the cronta8 is stored according
to username and 1ritten 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.
#ontrolling (ccess to #ron
The OetcOcron.allo1 and OetcOcron.den# files are used to restrict access to cron. The format
of 8oth access control files is one username on each line. 0hitespace 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 al1a#s use cron? regardless of the usernames listed in the access control
files.
/6
9f the file cron.allo1 e(ists? onl# users listed in it are allo1ed to use cron? and the
cron.den# file is ignored.
9f cron.allo1 does not e(ist? users listed in cron.den# are not allo1ed to use cron.
Starting and Stopping 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
0hat is 7.SB
The 7et1ork .ile S#stem <7.S= 1as de%eloped to allo1 machines to mount a disk
partition on a remote machine as if it 1ere a local disk. 9t allo1s for fast? seamless
sharing of files across a net1ork.
9t also gi%es the potential for un1anted people to access #our hard dri%e o%er the net1ork
<and there8# possi8l# read #our email and delete all #our files as 1ell as 8reak into #our
s#stem= if #ou set it up incorrectl#.
There are other s#stems that pro%ide similar functionalit# to 7.S. Sam8a
<httpKOO111.sam8a.org= pro%ides file ser%ices to 0indo1s clients. The $ndre1 .ile
S#stem? originall# de%eloped 8# 9,2 <httpKOO111.openafs.org= and no1 open-source?
pro%ides a file sharing mechanism 1ith some additional securit# and performance
features. The Coda .ile S#stem <httpKOO111.coda.cs.cmu.eduO= com8ines file sharing
1ith a specific focus on disconnected clients. 2an# of the features of the $ndre1 and
Coda file s#stems are slated for inclusion in the ne(t %ersion of 7.S <Version 6=
<httpKOO111.nfs%6.org=. The ad%antage of 7.S toda# is that it is mature? standard? 1ell
understood? and supported ro8ustl# across a %ariet# of platforms.
Setting Up an ,$S Serer
<ntrodction to Server Setp
9t is assumed that #ou 1ill 8e setting up 8oth a ser%er and a client. Setting up the ser%er
1ill 8e done in t1o stepsK Setting up the configuration files for 7.S? and then starting the
7.S ser%ices.
/5
Setting p the #on!igration 7iles
There are three main configuration files #ou 1ill need to edit to set up an 7.S ser%erK
OetcOe(ports? OetcOhosts.allo1? and OetcOhosts.den# . Strictl# speaking? #ou onl# need to edit
OetcOe(ports to get 7.S to 1ork? 8ut #ou 1ould 8e left 1ith an e(tremel# insecure setup.
You ma# also need to edit #our startup scripts\
-etc-e"ports
This file contains a list of entries\ each entr# indicates a %olume that is shared and ho1 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 1ill pro8a8l# satisf# most people@s
needs.
$n entr# in OetcOe(ports 1ill t#picall# look like thisK
director# machine*<option**?option*-= machine-<option-*?option--=
1here
director#
the director# that #ou 1ant to share. 9t ma# 8e an entire %olume though it need not 8e. 9f
#ou share a director#? then all directories under it 1ithin the same file s#stem 1ill 8e
shared as 1ell.
machine* and machine-
client machines that 1ill ha%e access to the director#. The machines ma# 8e listed 8#
their !7S address or their 9P address <e.g.? machine.compan#.com or *)-.*6&.+.& =.
Using 9P addresses is more relia8le and more secure. !7S names ma# not al1a#s resol%e
the ip address
option((
the option listing for each machine 1ill descri8e 1hat kind of access that machine 1ill
ha%e. 9mportant options areK
roK The director# is shared read onl#\ the client machine 1ill not 8e a8le to 1rite it. This
is the default.
r1K The client machine 1ill ha%e read and 1rite 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# 1hich 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 1ill 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 1ant to perform an# administrati%e
/6
1ork on the client machine that in%ol%es the e(ported directories. You should not specif#
this option 1ithout 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 1ill speed up transfers.
s#ncK ,# default? all 8ut the most recent %ersion <%ersion *.**= of the e(portfs command
1ill use as#nc 8eha%ior? telling a client machine that a file 1rite is complete - that is? has
8een 1ritten to sta8le storage - 1hen 7.S has finished handing the 1rite 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 1e ha%e t1o client machines? sla%e* and sla%e-? that ha%e 9P addresses
*)-.*6&.+.* and *)-.*6&.+.-? respecti%el#. 0e 1ish to share our soft1are 8inaries and
home directories 1ith these machines. $ t#pical setup for OetcOe(ports might look like
thisK
OusrOlocal *)-.*6&.+.*<ro= *)-.*6&.+.-<ro=
Ohome *)-.*6&.+.*<r1= *)-.*6&.+.-<r1=
5ere 1e are sharing OusrOlocal read-onl# to sla%e* and sla%e-? 8ecause it pro8a8l#
contains our soft1are and there ma# not 8e 8enefits to allo1ing sla%e* and sla%e- to
1rite to it that out1eigh securit# concerns. 4n the other hand? home directories need to
8e e(ported read-1rite if users are to sa%e their 1ork 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 net1ork that reEuire access to #our ser%er. There are a fe1 1a#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 net1ork and a netmask. .or e(ample? if #ou 1anted
to allo1 access to all the machines 1ith 9P addresses 8et1een *)-.*6&.+.+ and
*)-.*6&.+.-55 then #ou could ha%e the entriesK
OusrOlocal *)-.*6&.+.+O-55.-55.-55.+<ro=
Ohome *)-.*6&.+.+O-55.-55.-55.+<r1=
See the 7et1orking-4%er%ie1 540T4 for further information on ho1 netmasks? and
#ou ma# also 1ish to look at the man pages for init and hosts.allo1.
Second? #ou can use 79S netgroups in #our entr#. To specif# a netgroup in #our e(ports
file? simpl# prepend the name of the netgroup 1ith an _a_. See the 79S 540T4 for
details on ho1 netgroups 1ork.
Third? #ou can use 1ildcards such as A.foo.com or *)-.*6&. instead of hostnames. There
1ere pro8lems 1ith 1ildcard implementation in the -.- kernel series that 1ere fi(ed in
kernel -.-.*).
/"
5o1e%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 net1ork that #ou do not trust
completel#.
$ fe1 cautions are in order a8out 1hat 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. 5o1e%er? e(porting 8oth should not 8e necessar# 8ecause listing the
parent director# in the OetcOe(ports file 1ill cause all underl#ing directories 1ithin that
file s#stem to 8e e(ported.
Second? it is a poor idea to e(port a .$T or V.$T <i.e.? 2S-!4S or 0indo1s )5O)&=
files#stem 1ith 7.S. .$T is not designed for use on a multi-user machine? and as a
result? operations that depend on permissions 1ill not 1ork 1ell. 2oreo%er? some of the
underl#ing files#stem design is reported to 1ork poorl# 1ith 7.S@s e(pectations.
Third? de%ice or other special files ma# not e(port correctl# to non-inu( clients.
-etc-hosts&allow and -etc-hosts&deny
These t1o files specif# 1hich computers on the net1ork 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. 0hen the ser%er gets a reEuest from a machine? it does the follo1ingK
9t first checks hosts.allo1 to see if the machine matches a rule listed here. 9f it does? then
the machine is allo1ed access.
9f the machine does not match an entr# in hosts.allo1 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 allo1ed access.
9n addition to controlling access to ser%ices handled 8# inetd <such as telnet and .TP=?
this file can also control access to 7.S 8# restricting connections to the daemons that
pro%ide 7.S ser%ices. Restrictions are done on a per-ser%ice 8asis.
The first daemon to restrict access to is the portmapper. This daemon essentiall# Nust tells
reEuesting clients ho1 to find all the 7.S ser%ices on the s#stem. Restricting access to
the portmapper is the 8est defense against someone 8reaking into #our s#stem through
7.S 8ecause completel# unauthoriFed clients 1on@t kno1 1here to find the 7.S
daemons. 5o1e%er? there are t1o things to 1atch out for. .irst? restricting portmapper
isn@t enough if the intruder alread# kno1s for some reason ho1 to find those daemons.
$nd second? if #ou are running 79S? restricting portmapper 1ill also restrict reEuests to
79S. That should usuall# 8e harmless since #ou usuall# 1ant to restrict 7.S and 79S in a
similar 1a#? 8ut Nust 8e cautioned. <Running 79S is generall# a good idea if #ou are
running 7.S? 8ecause the client machines need a 1a# of kno1ing 1ho o1ns 1hat files
on the e(ported %olumes. 4f course there are other 1a#s of doing this such as s#ncing
pass1ord files. See the 79S 540T4 for information on setting up 79S.=
/&
9n general it is a good idea 1ith 7.S <as 1ith most internet ser%ices= to e(plicitl# den#
access to 9P addresses that #ou don@t need to allo1 access to.
The first step in doing this is to add the follo1ng entr# to OetcOhosts.den#K
portmapK$
Starting 1ith nfs-utils +.-.+? #ou can 8e a 8it more careful 8# controlling access to
indi%idual daemons. 9t@s a good precaution since an intruder 1ill often 8e a8le to 1easel
around the portmapper. 9f #ou ha%e a ne1er %ersion of nfs-utils? add entries for each of
the 7.S daemonsK
lockdK$
mountdK$
rEuotadK$
statdK$
'%en if #ou ha%e an older %ersion of nfs-utils? adding these entries is at 1orst harmless
<since the# 1ill Nust 8e ignored= and at 8est 1ill sa%e #ou some trou8le 1hen #ou
upgrade. Some s#s admins choose to put the entr# $K$ in the file OetcOhosts.den#?
1hich causes an# ser%ice that looks at these files to den# access to all hosts unless it is
e(plicitl# allo1ed. 0hile this is more secure 8eha%ior? it ma# also get #ou in trou8le
1hen #ou are installing ne1 ser%ices? #ou forget #ou put it there? and #ou can@t figure out
for the life of #ou 1h# the# 1on@t 1ork.
7e(t? 1e need to add an entr# to hosts.allo1 to gi%e an# hosts access that 1e 1ant to
ha%e access. <9f 1e Nust lea%e the a8o%e lines in hosts.den# then no8od# 1ill ha%e access
to 7.S.= 'ntries in hosts.allo1 follo1 the formatK
ser%iceK host Hor net1orkOnetmaskI ? host Hor net1orkOnetmaskI
5ere? host is 9P address of a potential client\ it ma# 8e possi8le in some %ersions to use
the !7S name of the host? 8ut it is strongl# discouraged.
Suppose 1e ha%e the setup a8o%e and 1e Nust 1ant to allo1 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#. 0e could add the follo1ing entr# to OetcOhosts.allo1K
portmapK *)-.*6&.+.* ? *)-.*6&.+.-
.or recent nfs-utils %ersions? 1e 1ould also add the follo1ing <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&.+.-
/)
9f #ou intend to run 7.S on a large num8er of machines in a local net1ork?
OetcOhosts.allo1 also allo1s for net1orkOnetmask st#le entries in the same manner as
OetcOe(ports a8o%e.

)etting the serices Started
+re-re4isites
The 7.S ser%er should no1 8e configured and 1e can start it running. .irst? #ou 1ill
need to ha%e the appropriate packages installed. This consists mainl# of a ne1 enough
kernel and a ne1 enough %ersion of the nfs-utils package.
7e(t? 8efore #ou can start 7.S? #ou 1ill need to ha%e TCPO9P net1orking functioning
correctl# on #our machine. 9f #ou can use telnet? .TP? and so on? then chances are #our
TCP net1orking is fine.
That said? 1ith most recent inu( distri8utions #ou ma# 8e a8le to get 7.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 1ill start up 7.S correctl#. 9f this does not 1ork? or
if #ou are not in a position to re8oot #our machine? then the follo1ing section 1ill tell
#ou 1hich daemons need to 8e started in order to run 7.S ser%ices. 9f for some reason
nfsd 1as alread# running 1hen #ou edited #our configuration files a8o%e? #ou 1ill ha%e
to flush #our configuration\
Starting the +ortmapper
7.S depends on the portmapper daemon? either called portmap or rpc.portmap. 9t 1ill
need to 8e started first. 9t should 8e located in Os8in 8ut is sometimes in OusrOs8in. 2ost
recent inu( distri8utions start this daemon in the 8oot scripts? 8ut it is 1orth making
sure that it is running 8efore #ou 8egin 1orking 1ith 7.S <Nust t#pe ps au( W grep
portmap=.
The Daemons
7.S ser%ing is taken care of 8# fi%e daemonsK rpc.nfsd? 1hich does most of the 1ork\
rpc.lockd and rpc.statd? 1hich handle file locking\ rpc.mountd? 1hich handles the initial
mount reEuests? and rpc.rEuotad? 1hich handles user file Euotas on e(ported %olumes.
Starting 1ith -.-.*&? lockd is called 8# nfsd upon demand? so #ou do not need to 1orr#
a8out starting it #ourself. statd 1ill need to 8e started separatel#. 2ost recent inu(
distri8utions 1ill ha%e startup scripts for these daemons.
6+
The daemons are all part of the nfs-utils 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 follo1ing orderK
rpc.portmap
rpc.mountd? rpc.nfsd
rpc.statd? rpc.lockd <if necessar#=? and
rpc.rEuotad
The nfs-utils 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 1ill
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 1ith the command
rpcin!o 0p localhost
to find out 1hat ser%ices it is pro%iding. You should get something like thisK
program %ers proto port
*+++++ - tcp *** portmapper
*+++++ - udp *** portmapper
*+++** * udp "6) rEuotad
*+++** - udp "6) rEuotad
*++++5 * udp "5) mountd
*++++5 * tcp "6* mountd
*++++5 - udp "66 mountd
*++++5 - tcp "66 mountd
*++++5 / udp "6) mountd
*++++5 / tcp ""* mountd
*++++/ - udp -+6) nfs
*++++/ / udp -+6) nfs
/+++*) * tcp &/+ amd
/+++*) * udp &/* amd
*+++-6 * udp )66 status
*+++-6 * tcp )66 status
*+++-* * udp *+6- nlockmgr
*+++-* / udp *+6- nlockmgr
6*
*+++-* 6 udp *+6- nlockmgr
*+++-* * tcp *6-) nlockmgr
*+++-* / tcp *6-) nlockmgr
*+++-* 6 tcp *6-) nlockmgr
This sa#s that 1e ha%e 7.S %ersions - and /? rpc.statd %ersion *? net1ork lock manager
<the ser%ice name for rpc.lockd= %ersions *? /? and 6. There are also different ser%ice
listings depending on 1hether 7.S is tra%elling o%er TCP or U!P. inu( s#stems use
U!P 8# default unless TCP is e(plicitl# reEuested\ ho1e%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 1ill 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 7.S clients to
access files from #our ser%er.
*aking #hanges to -etc-e"ports later 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 -ra to force nfsd to re-read
the OetcOe(ports file. 9f #ou can@t find the e(portfs command? then #ou can kill nfsd 1ith
the -5UP flag <see the man pages for kill for details=.
9f that still doesn@t 1ork? don@t forget to check hosts.allo1 to make sure #ou ha%en@t
forgotten to list an# ne1 client machines there. $lso check the host listings on an#
fire1alls #ou ma# ha%e set up.
Setting up an ,$S Client
(ounting Remote Directories
,efore 8eginning? #ou should dou8le-check to make sure #our mount program is ne1
enough <%ersion -.*+m if #ou 1ant to use Version / 7.S=? and that the client machine
supports 7.S mounting? though most standard distri8utions do. 9f #ou are using a -.- or
later kernel 1ith 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 7.S has 8een compiled as a module\ other1ise? #ou 1ill
need to 8uild <or do1nload= a kernel that has 7.S support 8uilt in. 9n general? kernels
that do not ha%e 7.S compiled in 1ill gi%e a %er# specific error 1hen the mount
command 8elo1 is run.
6-
To 8egin using machine as an 7.S client? #ou 1ill need the portmapper running on that
machine? and to use 7.S file locking? #ou 1ill 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.
0ith portmap? lockd? and statd running? #ou should no1 8e a8le to mount the remote
director# from #our ser%er Nust the 1a# #ou mount a local hard dri%e? 1ith the mount
command. Continuing our e(ample from the pre%ious section? suppose our ser%er a8o%e
is called master.foo.com?and 1e 1ant to mount the Ohome director# on sla%e*.foo.com.
Then? all 1e ha%e to do? from the root prompt on sla%e*.foo.com? is t#peK
= mont master&!oo&com3-home -mnt-home
and the director# Ohome on master 1ill appear as the director# OmntOhome on sla%e*.
<7ote that this assumes 1e ha%e created the director# OmntOhome as an empt# mount
point 8eforehand.=
You can unmount the file s#stem 8# t#pingK
= mont -mnt-home
Cust like #ou 1ould for a local file s#stem.
)etting ,$S $ile Systems to -e (ounted at #oot Time
7.S file s#stems can 8e added to #our OetcOfsta8 file the same 1a# local file s#stems can?
so that the# mount 1hen #our s#stem starts up. The onl# difference is that the file s#stem
t#pe 1ill 8e set to nfs and the dump and fsck order <the last t1o entries= 1ill ha%e to 8e
set to Fero. So for our e(ample a8o%e? the entr# in OetcOfsta8 1ould look likeK
M de%ice mountpoint fs-t#pe options dump fsckorder
...
master.foo.comKOhome Omnt nfs r1 + +
...
See the man pages for fsta8 if #ou are unfamiliar 1ith 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 7.S 1orking? though a fe1 t1eaks ma# still 8e necessar#
to get it to 1ork 1ell.
6/
(ount *ptions
So!t verss Hard *onting
There are some options #ou should consider adding at once. The# go%ern the 1a# the
7.S client handles a ser%er crash or net1ork outage. 4ne of the cool things a8out 7.S is
that it can handle this gracefull#? if #ou set up the clients right. There are t1o distinct
failure modesK
soft
9f a file reEuest fails? the 7.S client 1ill report an error to the process on the client
machine reEuesting the file access. Some programs can handle this 1ith composure? most
1on@t. 0e 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 --- if #ou %alue #our mail? that is.
hard
The program accessing a file on a 7.S mounted file s#stem 1ill hang 1hen the ser%er
crashes. The process cannot 8e interrupted or killed <e(cept 8# a _sure kill_= unless #ou
also specif# intr. 0hen the 7.S ser%er is 8ack online the program 1ill continue
undistur8ed from 1here it 1as. 0e recommend using hard?intr on all 7.S mounted file
s#stems.
Picking up from the pre%ious e(ample? the fsta8 1ould no1 look likeK
M de%ice mountpoint fs-t#pe options dump fsckord
...
master.foo.comKOhome OmntOhome nfs r1?hard?intr + +
...
The rsiFe and 1siFe 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 1orks 1ell on all or most
setups. 4n the one hand? some com8inations of inu( kernels and net1ork 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 7.S ser%er in a production en%ironment.
66
,.S
7et1ork 9nformation Ser%ice? a ser%ice that pro%ides information? that has to 8e kno1n
throughout the net1ork? to all machines on the net1ork. There is support for 79S in
inu(@s standard li8c li8rar#? 1hich in the follo1ing te(t is referred to as _traditional
79S_
The ne(t four lines are Euoted from the Sun<tm= S#stem X 7et1ork $dministration
2anualK
_79S 1as formerl# kno1n as Sun Yello1 Pages <YP= 8ut
the name Yello1 Pages<tm= is a registered trademark
in the United Dingdom of ,ritish Telecom plc and ma#
not 8e used 1ithout permission._
79S stands for 7et1ork 9nformation Ser%ice. 9ts purpose is to pro%ide information? that
has to 8e kno1n throughout the net1ork? to all machines on the net1ork. 9nformation
likel# to 8e distri8uted 8# 79S isK
login namesOpass1ordsOhome directories <OetcOpass1d=
group information <OetcOgroup=
9f? for e(ample? #our pass1ord entr# is recorded in the 79S pass1d data8ase? #ou 1ill 8e
a8le to login on all machines on the net1ork 1hich ha%e the 79S client programs
running.
Sun is a trademark of Sun 2icros#stems? 9nc. licensed to SunSoft? 9nc.
79S:
7et1ork 9nformation Ser%ice <Plus K-=? essentiall# 79S on steroids. 79S: is designed 8#
Sun 2icros#stems 9nc. as a replacement for 79S 1ith 8etter securit# and 8etter handling
of ^large^ installations.
How ><S works
0ithin a net1ork there must 8e at least one machine acting as a 79S ser%er. You can ha%e
multiple 79S ser%ers? each ser%ing different 79S _domains_ - or #ou can ha%e cooperating
79S ser%ers? 1here one is the master 79S ser%er? and all the other are so-called sla%e 79S
ser%ers <for a certain 79S _domain_? that isY= - or #ou can ha%e a mi( of them...
Sla%e ser%ers onl# ha%e copies of the 79S data8ases and recei%e these copies from the
master 79S ser%er 1hene%er changes are made to the master@s data8ases. !epending on
the num8er of machines in #our net1ork and the relia8ilit# of #our net1ork? #ou might
decide to install one or more sla%e ser%ers. 0hene%er a 79S ser%er goes do1n or is too
slo1 in responding to reEuests? a 79S client connected to that ser%er 1ill tr# to find one
that is up or faster.
65
79S data8ases are in so-called !,2 format? deri%ed from $SC99 data8ases. .or e(ample?
the files OetcOpass1d and OetcOgroup can 8e directl# con%erted to !,2 format using $SC99-
to-!,2 translation soft1are <maked)m? included 1ith the ser%er soft1are=. The master
79S ser%er should ha%e 8oth? the $SC99 data8ases and the !,2 data8ases.
Sla%e ser%ers 1ill 8e notified of an# change to the 79S maps? <%ia the yppsh program=?
and automaticall# retrie%e the necessar# changes in order to s#nchroniFe their data8ases.
79S clients do not need to do this since the# al1a#s talk to the 79S ser%er to read the
information stored in it@s !,2 data8ases.
4ld #p8ind %ersions do a 8roadcast to find a running 79S ser%er. This is insecure? due the
fact that an#one ma# install a 79S ser%er and ans1er the 8roadcast Eueries. 7e1er
Versions of #p8ind <#p8ind-/./ or #p8ind-mt= are a8le to get the ser%er from a
configuration file - thus no need to 8roadcast.
How ><S$ works
79S: is a ne1 %ersion of the net1ork information nameser%ice from Sun. The 8iggest
difference 8et1een 79S and 79S: is that 79S: has support for data encr#ption and
authentication o%er secure RPC.
The naming model of 79S: is 8ased upon a tree structure. 'ach node in the tree
corresponds to an 79S: o8Nect? from 1hich 1e ha%e si( t#pesK director#? entr#? group?
link? ta8le and pri%ate.
The 79S: director# that forms the root of the 79S: namespace is called the root
director#. There are t1o special 79S: directoriesK org^dir and groups^dir. The org^dir
director# consists of all administration ta8les? such as pass1d? hosts? and mail^aliases.
The groups^dir director# consists of 79S: group o8Nects 1hich are used for access
control. The collection of org^dir? groups^dir and their parent director# is referred to as
an 79S: domain.
(anaging System Logs
The s#slogd utilit# logs %arious kinds of s#stem acti%it#? such as de8ugging output from
sendmail and 1arnings 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 1here s#slogd records information. Such a file
might look like the follo1ingK
A.info\A.notice O%arOlogOmessages
mail.de8ug O%arOlogOmaillog
A.1arn O%arOlogOs#slog
kern.emerg Ode%Oconsole
66
The first field of each line lists the kinds of messages that should 8e logged? and the
second field lists the location 1here the# should 8e logged. The first field is of the
formatK
facilit#.le%el H\ facilit#.le%el b I
1here 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? 1arning? err? crit? alert? or
emerg.
9n the pre%ious OetcOs#slog.conf? 1e 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 1arn messages are logged to O%arOlogOs#slog. $lso? an# emerg
1arnings from the kernel are sent to the console <1hich is the current %irtual console? or
an (term started 1ith the -C option=.
The messages logged 8# s#slogd usuall# include the date? an indication of 1hat process
or facilit# deli%ered the message? and the message itself--all on one line. .or e(ample? a
kernel error message indicating a pro8lem 1ith data on an e(t-fs files#stem might appear
in the log files asK
!ec * -*K+/K/5 loomer kernelK ';T--fs error <de%ice /O-=K
e(t-^check^8locks^8it mapK 0rong free 8locks count in super 8lock?
stored ` -"-+-? counted ` -"&5/
Similarl#? if an su to the root account succeeds? #ou might see a log message such asK
!ec ** *5K/*K5* loomer suK md1 on Ode%Ott#p/
og files can 8e important in tracking do1n s#stem pro8lems. 9f a log file gro1s too
large? #ou can delete it using rm\ it 1ill 8e recreated 1hen s#slogd starts up again.
Your s#stem pro8a8l# comes eEuipped 1ith a running s#slogd and an OetcOs#slog.conf
that does the right thing. 5o1e%er? it@s important to kno1 1here #our log files are and
1hat programs the# represent. 9f #ou need to log man# messages <sa#? de8ugging
messages from the kernel? 1hich can 8e %er# %er8ose= #ou can edit s#slog.conf and tell
s#slogd to reread its configuration file 1ith the commandK
kill -5UP ccat O%arOrunOs#slog.pidc
7ote the use of 8ackEuotes to o8tain the process 9! of s#slogd? contained in
O%arOrunOs#slog.pid.
4ther s#stem logs might 8e a%aila8le as 1ell. These includeK
6"
O%arOlogO1tmp
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
md1 tt#/ Sun !ec ** *5K-5 still logged in
md1 tt#/ Sun !ec ** *5K-6 - *5K-5 <++K++=
md1 tt#* Sun !ec ** **K66 still logged in
re8oot > Sun !ec ** +6K66
$ record is also logged in O%arOlogO1tmp 1hen 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 1ho? 1? and finger? use this file to produce information on
1ho is logged in. .or e(ample? the 1 command might printK
/K5&pm up 6K*-? 5 users? load a%erageK +.+*? +.+-? +.++
User tt# logina idle CCPU PCPU 1hat
md1 tt#p/ **K66am *6 -
md1 tt#p- **K66am * 1
md1 tt#p6 **K66am kermit
md1 tt#p+ **K66am *6 8ash
0e see the login times for each user <in this case? one user logged in man# times=? as 1ell
as the command currentl# 8eing used. The 1 manual page descri8es all of the fields
displa#ed.
O%arOlogOlastlog
This file is similar to 1tmp? 8ut is used 8# different programs <such as finger? to
determine 1hen a user 1as last logged in.=
7ote that the format of the 1tmp 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 information--
especiall# if the files 8ecome corrupted 8# a program that 1rites information to them in
the 1rong format.
ogfiles 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 1ant to do this? since the log files also
contain information that can 8e %alua8le in crisis situations.
6&
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-log-messages -var-log-messages-)ackp
cp -dev-nll -var-log-messages
#96%(TE?@date $ABmBdByA@
mv -var-log-messages-)ackp -var-log-messages-$#96%(TE
gCip -var-log-messages-$#96%(TE
.irst? 1e 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. 0e do this so that further logging can 8e done
1ithout pro8lems 1hile the ne(t steps are done. Then? 1e 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 1ith gFip.
You might 1ant to run this small script from cron? 8ut as it is presented here? it should not
8e run more than once a da#--other1ise the compressed 8ackup cop# 1ill 8e o%er1ritten?
8ecause the filename reflects the date 8ut not the time of da#. 9f #ou 1ant to run this
script more often? #ou must use additional num8ers to distinguish 8et1een the %arious
copies.
There are man# more impro%ements that could 8e made here. .or e(ample? #ou might
1ant 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 1ill
e%entuall# get filled. You can sol%e this pro8lem 8# keeping onl# a certain num8er of
compressed log files <sa#? *+= around. 0hen #ou ha%e created as man# log files as #ou
1ant to ha%e? #ou delete the oldest? and o%er1rite it 1ith 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 8uilt-in cron scripts that manage #our log files and are
much more sophisticated than the small one presented here.
Logrotate
4ne of the most useful tools for log management in U79; is logrotate? 1hich is part of
Nust a8out an# U79; 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.
6)
This 540T4 sho1s #ou ho1 to set up log rotation not at a s#stem le%el? 8ut for a gi%en
user
7ilesystem Layot
et@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 --:-- etc Z- 1e@re going to put logrotate.conf here
W
:-- 2ail
...
:-- %ar --:-- li8 Z- the logrotate status file goes here
W
:-- log Z- the actual log files go here
#on!igring logrotate
The first step is to create a configuration file. 5ere is a sample that rotates the log file on
a 1eekl# 8asis? compresses the old log? creates a ne1 Fero-8#te file and mails us a short
reportK
$ cat D-etc-logrotate&con!
M see _man logrotate_ for details
M rotate log files 1eekl#
1eekl#
M keep 6 1eeks 1orth of 8acklogs
rotate 6
M create ne1 <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 1ant #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 1ith
different options=.
:etting it to 6n
5+
2aking logrotate actuall# 1ork? ho1e%er? reEuires in%oking it from cron. To do that? add
it to #our cronta8 specif#ing the status file 1ith -s and the configuration file #ou createdK
$ cronta) 0l
+ + A A A OusrOs8inOlogrotate -s OhomeOuserO%arOli8Ologrotate.status f
OhomeOuserOetcOlogrotate.conf V Ode%Onull -VX*
<Take care - some s#stems do not allo1 _f_ to skip to the ne(t line? 1hich 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 1ill then look at its status file and decide 1hether or not it is
time to actuall# rotate the log files.
The difference -et&een hard and soft links
The data part is associated 1ith something called an @inode@. The inode carries the map of
1here the data is? the file permissions? etc. for the data.
.---------------V Y data Y Y data Y etc
O :------: Y------:
Y perm8its? etc Y data addresses Y
:------------inode---------------:
The filename part carries a name and an associated inode num8er.
.--------------V Y perm8its? etc Y addresses Y
O :---------inode-------------:
Y filename Y inode M Y
:--------------------:
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
:--------------------:
f
V--------------V Y perm8its? etc Y addresses Y
O :---------inode-------------:
Y othername Y inode M Y
:---------------------:
5*
4n the other hand? there@s a special file t#pe 1hose 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 1rites so that? instead of accessing the data 1ithin 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
:--------------------:
f
.-------V Y perm8its? etc Y addresses Y
:---------inode-------------:
O
O
O
.----------------------------------------------@
<
@--V Y_OpathOtoOsomeOotherOfile_Y
:---------data-------------:
O e
.> > > > > > > e-- <redirected at open<= time=
< e
@>>V Y filename Y inode M Y
:--------------------:
f
@------------V Y perm8its? etc Y addresses Y
:---------inode-------------:
O
O
.----------------------------------------------------@
<
@-V Y data Y Y data Y etc.
:------: :------:
7o1? the filename part of the file is stored in a special file of its o1n along 1ith 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.
0hen a director# is 8uilt? it is initiall# populated 1ith the filename parts of t1o special
filesK the @.@ and @..@ files. The filename part for the @.@ file is populated 1ith the inodeM of
the director# file in 1hich the entr# has 8een made\ @.@ is a hardlink to the file that
implements the current director#.
The filename part for the @..@ file is populated 1ith 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#.
5-
The @ln@ command kno1s ho1 to 8uild hardlinks and softlinks\ the @mkdir@ command
kno1s ho1 to 8uild directories <the 4S takes care of the a8o%e hardlinks=.
There are restrictions on 1hat 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 1ords? there@s tradeoffs 1ith each.
7o1? let@s demonstrate some of this...
ln in action
et@s start off 1ith an empt# director#? and create a file in it
D-directory $ ls -lia
total /
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K*6 .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*6 ..
D-directory $ echo AThis is a !ileA .)asic&!ile
D-directory $ ls -lia
total 6
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K*" .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*6 ..
"/6"& -r1-r--r-- * lpitcher users *5 2ar ** -+K*" 8asic.file
D-directory $ cat )asic&!ile
This is a file
7o1? let@s make a hardlink to the file

D-directory $ ln )asic&!ile hardlink&!ile
D-directory $ ls -lia
total 5
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K-+ .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*& ..
"/6"& -r1-r--r-- - lpitcher users *5 2ar ** -+K*" 8asic.file
"/6"& -r1-r--r-- - lpitcher users *5 2ar ** -+K*" hardlink.file
D-directory $ cat hardlink&!ile
This is a file
0e see thatK
hardlink.file shares the same inode <"/6"&= as 8asic.file
5/
hardlink.file shares the same data as 8asic.file
9f 1e change the permissions on 8asic.fileK
D-directory $ chmod a$w )asic&!ile
D-directory $ ls -lia
total 5
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K-+ .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*& ..
"/6"& -r1-r1-r1- - lpitcher users *5 2ar ** -+K*" 8asic.file
"/6"& -r1-r1-r1- - lpitcher users *5 2ar ** -+K*" hardlink.file
then the same permissions change on hardlink.file.
The t1o files <8asic.file and hardlink.file= share the same inode and data? 8ut ha%e
different file names.
et@s no1 make a softlink to the original fileK
D-directory $ ln -s )asic&!ile so!tlink&!ile
D-directory $ ls -lia
total 5
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K-6 .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*& ..
"/6"& -r1-r1-r1- - lpitcher users *5 2ar ** -+K*" 8asic.file
"/6"& -r1-r1-r1- - lpitcher users *5 2ar ** -+K*" hardlink.file
"/6") lr1(r1(r1( * lpitcher users *+ 2ar ** -+K-6 softlink.file -V 8asic.file
D-directory $ cat so!tlink&!ile
This is a file
5ere? 1e see that although softlink.file accesses the same data as 8asic.file and
hardlink.file? it does not share the same inode <"/6") %s "/6"&=? nor does it e(hi8it the
same file permissions. 9t does sho1 a ne1 permission 8itK the @l@ <softlink= 8it.
9f 1e delete 8asic.fileK
D-directory $ rm )asic&!ile
D-directory $ ls -lia
total 6
"/6"" dr1(r-(r-( - lpitcher users *+-6 2ar ** -+K-" .
)*&+6 dr1(r-(r-( -) lpitcher users -+6& 2ar ** -+K*& ..
"/6"& -r1-r1-r1- * lpitcher users *5 2ar ** -+K*" hardlink.file
"/6") lr1(r1(r1( * lpitcher users *+ 2ar ** -+K-6 softlink.file -V 8asic.file
then 1e lose the a8ilit# to access the linked data through the softlinkK
56
D-directory $ cat so!tlink&!ile
catK softlink.fileK 7o such file or director#
5o1e%er? 1e still ha%e access to the original data through the hardlinkK
D-directory $ cat hardlink&!ile
This is a file
You 1ill notice that 1hen 1e deleted the original file? the hardlink didn@t %anish.
Similarl#? if 1e had deleted the softlink? the original file 1ouldn@t ha%e %anished.
$ further note 1ith respect to hardlink files
0hen 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 ho1 man# filenames point to this file?
and that count is decremented 8# * each time one of those filenames is deleted. 0hen the
count makes it to Fero? the inode and its associated data are deleted.
,# the 1a#? the count also reflects ho1 man# times the file has 8een opened 1ithout
8eing closed <in other 1ords? ho1 man# references to the file are still acti%e=. This has
some ramifications 1hich aren@t o8%ious at firstK #ou can delete a file so that no
_filename_ part points to the inode? 1ithout 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#slog-o1ned file= has gro1n too 8ig? and #ou
rm -var-log-messages
toch -var-log-messages
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 1on@t release the space for the data until the process closes it. 9n
order to complete #our space reclamation? #ou ha%e to
kill -S<:H9+ @cat -var-rn-syslogd&pid@
to get s#slogd to close and reopen the file.
$ile Compression and Archiing
55
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
do1nload faster %ia the 9nternet.
9t is important to understand the distinction 8et1een 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 1a# 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
1ant 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.
#ompressing 7iles at the Shell +rompt
Compressed files use less disk space and do1nload faster than large? uncompressed files.
9n Red 5at inu( #ou can compress files 1ith 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 U79;-like operating s#stems. The gFip compression tool can also
8e found on most U79;-like operating s#stems. 9f #ou need to transfer files 8et1een
inu( and other operating s#stem such as 2S 0indo1s? #ou should use Fip 8ecause it is
more compati8le 1ith the compression utilities on 0indo1s.
#ompression Tool 7ile E"tension 9ncompression Tool
3Fip .gF gunFip
8Fip- .8F- 8unFip-
Fip .Fip unFip
,# con%ention? files compressed 1ith gFip are gi%en the e(tension .gF? files compressed
1ith 8Fip- are gi%en the e(tension .8F-? and files compressed 1ith Fip are gi%en the
e(tension .Fip.
.iles compressed 1ith gFip are uncompressed 1ith gunFip? files compressed 1ith 8Fip-
are uncompressed 1ith 8unFip-? and files compressed 1ith Fip are uncompressed 1ith
unFip.
8CipE and 8nCipE
56
To use 8Fip- to compress a file? t#pe the follo1ing command at a shell promptK 8Fip-
filename
The file 1ill 8e compressed and sa%ed as filename.8F-.
To e(pand the compressed file? t#pe the follo1ing commandK8unFip- filename.8F- The
filename.8F- is deleted and replaced 1ith filename.
You can use 8Fip- to compress multiple files and directories at the same time 8# listing
them 1ith a space 8et1een each oneK8Fip- filename.8F- file* file- file/ OusrO1orkOschool
The a8o%e command compresses file*? file-? file/? and the contents of the
OusrO1orkOschool 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 :nCip
To use gFip to compress a file? t#pe the follo1ing command at a shell promptK gFip
filename. The file 1ill 8e compressed and sa%ed as filename.gF.
To e(pand the compressed file? t#pe the follo1ing commandKgunFip filename.gF
The filename.gF is deleted and replaced 1ith filename.
You can use gFip to compress multiple files and directories at the same time 8# listing
them 1ith a space 8et1een each oneKgFip -r filename.gF file* file- file/ OusrO1orkOschool
The a8o%e command compresses file*? file-? file/? and the contents of the
OusrO1orkOschool 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 1ith Fip? t#pe the follo1ing commandKFip -r filename.Fip filesdir
9n this e(ample? filename.Fip represents the file #ou are creating and filesdir represents
the director# #ou 1ant to put in the ne1 Fip file. The -r option specifies that #ou 1ant to
include all files contained in the filesdir director# recursi%el#.
To e(tract the contents of a Fip file? t#pe the follo1ing commandKunFip filename.Fip
You can use Fip to compress multiple files and directories at the same time 8# listing
them 1ith a space 8et1een each oneK Fip -r filename.Fip file* file- file/ OusrO1orkOschool
The a8o%e command compresses file*? file-? file/? and the contents of the
OusrO1orkOschool director# <assuming this director# e(ists= and places them in a file
named filename.Fip.
5"

.or more information? t#pe man Fip and man unFip at a shell prompt to read the man
pages for Fip and unFip.
(rchiving 7iles at the Shell +rompt
$ tar file is a collection of se%eral files andOor directories in one file. This is a good 1a#
to create 8ackups and archi%es.
Some of the options used 1ith the tar areK
-c P create a ne1 archi%e.
-f P 1hen used 1ith the -c option? use the filename specified for the creation of the tar
file\ 1hen used 1ith the -( option? unarchi%e the specified file.
-t P sho1 the list of files in the tar file.
-% P sho1 the progress of the files 8eing archi%ed.
-( P e(tract files from an archi%e.
-F P compress the tar file 1ith gFip.
-N P compress the tar file 1ith 8Fip-.
To create a tar file? t#peKtar -c%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 1ant to put in the archi%ed file.
You can tar multiple files and directories at the same time 8# listing them 1ith a space
8et1een each oneK tar -c%f filename.tar OhomeOmineO1ork OhomeOmineOschool
The a8o%e command places all the files in the 1ork and the school su8directories of
OhomeOmine in a ne1 file called filename.tar in the current director#.
To list the contents of a tar file? t#peKtar -t%f filename.tar
To e(tract the contents of a tar file? t#peK tar -(%f filename.tar
This command does not remo%e the tar file? 8ut it places copies of its unarchi%ed contents
in the current 1orking 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 1ithin a director# called fooO?
then e(tracting the archi%e file 1ill result in the creation of the director# fooO in #our
current 1orking director# 1ith the file 8ar.t(t inside of it.
Remem8er? the tar command does not compress the files 8# default. To create a tarred
and 8Fipped compressed file? use the -N optionK tar -cN%f filename.t8F file
5&
tar files compressed 1ith 8Fip- are con%entionall# gi%en the e(tension .t8F\ ho1e%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 1ith the 8unFip- command? the
filename.t8F file is remo%ed and replaced 1ith filename.tar.
You can also e(pand and unarchi%e a 8Fip tar file in one commandKtar -(N%f filename.t8F
To create a tarred and gFipped compressed file? use the -F optionK tar -cF%f filename.tgF
file
tar files compressed 1ith 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
1ith the gunFip command? the filename.tgF file is remo%ed and replaced 1ith
filename.tar.
You can e(pand a gFip tar file in one commandKtar -(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? 1hich runs on Red 5at inu( as 1ell as other inu( and U79; s#stems. Red 5at?
9nc. encourages other %endors to use RP2 for their o1n products. RP2 is distri8uta8le
under the terms of the 3P.
.or the end user? RP2 makes s#stem updates eas#. 9nstalling? uninstalling? and upgrading
RP2 packages can 8e accomplished 1ith short commands. RP2 maintains a data8ase of
installed packages and their files? so #ou can in%oke po1erful Eueries and %erifications on
#our s#stem. 9f #ou prefer a graphical interface? #ou can use 3nome-RP2 to perform
man# RP2 commands.
!uring upgrades? RP2 handles configuration files carefull#? so that #ou ne%er lose #our
customiFations P something that #ou 1ill not accomplish 1ith regular .tar.gF files.
5)
.or the de%eloper? RP2 allo1s #ou to take soft1are 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
ne1 %ersions of the soft1are 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+* %esign :oals
9n order to understand ho1 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 1ithout completel#
reinstalling. 0hen #ou get a ne1 release of an operating s#stem 8ased on RP2 <such as
Red 5at inu(=? #ou don@t need to reinstall on #our machine <as #ou do 1ith operating
s#stems 8ased on other packaging s#stems=. RP2 allo1s intelligent? full#-automated? in-
place upgrades of #our s#stem. Configuration files in packages are preser%ed across
upgrades? so #ou 1on@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.
Po1erful Guer#ing
RP2 is designed to pro%ide po1erful Euer#ing options. You can do searches through
#our entire data8ase for packages or Nust for certain files. You can also easil# find out
1hat package a file 8elongs to and from 1here the package came. The files an RP2
package contains are in a compressed archi%e? 1ith a custom 8inar# header containing
useful information a8out the package and its contents? allo1ing #ou to Euer# indi%idual
packages Euickl# and easil#.
S#stem Verification
$nother po1erful feature is the a8ilit# to %erif# packages. 9f #ou are 1orried that #ou
deleted an important file for some package? simpl# %erif# the package. You 1ill 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.
Pristine Sources
6+
$ crucial design goal 1as to allo1 the use of _pristine_ soft1are sources? as distri8uted
8# the original authors of the soft1are. 0ith RP2? #ou ha%e the pristine sources along
1ith an# patches that 1ere used? plus complete 8uild instructions. This is an important
ad%antage for se%eral reasons. .or instance? if a ne1 %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 1hat #ou might need to do. $ll the compiled-in defaults? and all of the
changes that 1ere made to get the soft1are 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# soft1are for end users? too. 0e 1ould like to thank the folks
from the ,43US distri8ution for originating the pristine source concept.
9sing 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%ie1 of
each mode. .or complete details and options tr# rpm --help? or turn to the section called
$dditional Resources for more information on RP2.
.inding RP2s
,efore using an RP2? #ou must kno1 1here to find them. $n 9nternet search 1ill return
man# RP2 repositories? 8ut if #ou are looking for RP2 packages 8uilt 8# Red 5at? the#
can 8e found at the follo1ing locationsK
The official Red 5at inu( C!-R42s
The Red 5at 'rrata Page a%aila8le at httpKOO111.redhat.comOsupportOerrata
$ Red 5at .TP 2irror Site a%aila8le at httpKOO111.redhat.comOmirrors.html
Red 5at 7et1ork
RP2 packages t#picall# ha%e file names like foo-*.+-*.i/&6.rpm. The file name includes
the package name <foo=? %ersion <*.+=? release <*=? and architecture <i/&6=. 9nstalling a
package is as simple as t#ping the follo1ing command at a shell promptK
6*
= rpm -ivh !oo-;&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.
7ote

$lthough a command like rpm -i%h foo-*.+-*.i/&6.rpm is commonl# used to install an
RP2 package? #ou ma# 1ant to consider using rpm -U%h foo-*.+-*.i/&6.rpm instead. -U
is commonl# used for upgrading a package? 8ut it 1ill also install ne1 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 1ill seeK
= rpm -ivh !oo-;&1-;&iFGH&rpm
foo package foo-*.+-* is alread# installed
M
9f #ou 1ant to install the package an#1a# and the same %ersion #ou are tr#ing to install is
alread# installed? #ou can use the --replacepkgs option? 1hich tells RP2 to ignore the
errorK
= rpm -ivh --replacepkgs !oo-;&1-;&iFGH&rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
This option is helpful if files installed from the RP2 1ere deleted or if #ou 1ant the
original configuration files from the RP2 to 8e installed.
Conflicting .iles
9f #ou attempt to install a package that contains a file 1hich has alread# 8een installed 8#
another package or an earlier %ersion of the same package? #ou@ll seeK
= rpm -ivh !oo-;&1-;&iFGH&rpm
foo OusrO8inOfoo conflicts 1ith file from 8ar-*.+-*
M
6-
To make RP2 ignore this error? use the --replacefiles optionK
= rpm -ivh --replace!iles !oo-;&1-;&iFGH&rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
Unresol%ed !ependenc#
RP2 packages can _depend_ on other packages? 1hich means that the# reEuire other
packages to 8e installed in order to run properl#. 9f #ou tr# to install a package 1hich has
an unresol%ed dependenc#? #ou@ll seeK
= rpm -ivh !oo-;&1-;&iFGH&rpm
failed dependenciesK 8ar is needed 8# foo-*.+-*
M
To handle this error #ou should install the reEuested packages. 9f #ou 1ant to force the
installation an#1a# <a 8ad idea since the package pro8a8l# 1ill not run correctl#=? use the
--nodeps option.
Uninstalling
Uninstalling a package is Nust as simple as installing one. T#pe the follo1ing command at
a shell promptK
= rpm -e !oo
M
7ote

7otice that 1e used the package name foo? not the name of the original package file foo-
*.+-*.i/&6.rpm. To uninstall a package? #ou 1ill need to replace foo 1ith the actual
package name of the original package.
You can encounter a dependenc# error 1hen uninstalling a package if another installed
package depends on the one #ou are tr#ing to remo%e. .or e(ampleK
= rpm -e !oo
remo%ing these packages 1ould 8reak dependenciesK foo is needed 8# 8ar-*.+-*
M
6/
To cause RP2 to ignore this error and uninstall the package an#1a# <1hich is also a 8ad
idea since the package that depends on it 1ill pro8a8l# fail to 1ork properl#=? use the
--nodeps option.
Upgrading
Upgrading a package is similar to installing one. T#pe the follo1ing command at a shell
promptK
= rpm -9vh !oo-E&1-;&iFGH&rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
0hat #ou do not see a8o%e is that RP2 automaticall# uninstalled an# old %ersions of the
foo package. 9n fact? #ou ma# 1ant to al1a#s use -U to install packages? since it 1ill
1ork e%en 1hen there are no pre%ious %ersions of the package installed.
Since RP2 performs intelligent upgrading of packages 1ith configuration files? #ou ma#
see a message like the follo1ingK sa%ing OetcOfoo.conf as OetcOfoo.conf.rpmsa%e
This message means that #our changes to the configuration file ma# not 8e _for1ard
compati8le_ 1ith the ne1 configuration file in the package? so RP2 sa%ed #our original
file? and installed a ne1 one. You should in%estigate the differences 8et1een the t1o
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 1ith an older %ersion num8er? #ou 1ill seeK
= rpm -9vh !oo-;&1-;&iFGH&rpm
foo package foo--.+-* <1hich is ne1er= is alread# installed
M
To cause RP2 to _upgrade_ an#1a#? use the --oldpackage optionK
= rpm -9vh --oldpackage !oo-;&1-;&iFGH&rpm
foo MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
M
.reshening
66
.reshening a package is similar to upgrading one. T#pe the follo1ing command at a shell
promptK
= rpm -7vh !oo-;&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. 0hen a
ne1er %ersion of an alread#-installed package is processed 8# RP2@s freshen option? it
1ill 8e upgraded to the ne1er %ersion. 5o1e%er? RP2@s freshen option 1ill not install a
package if no pre%iousl#-installed package of the same name e(ists. This differs from
RP2@s upgrade option? as an upgrade 1ill install packages? 1hether or not an older
%ersion of the package 1as alread# installed.
RP2@s freshen option 1orks for single packages or a group of packages. 9f #ou ha%e Nust
do1nloaded a large num8er of different packages? and #ou onl# 1ant to upgrade those
packages that are alread# installed on #our s#stem? freshening 1ill do the No8. 9f #ou use
freshening? #ou 1ill not ha%e to deleting an# un1anted packages from the group that #ou
do1nloaded 8efore using RP2.
9n this case? #ou can simpl# issue the follo1ing commandK
= rpm -7vh I&rpm
RP2 1ill automaticall# upgrade onl# those packages that are alread# installed.
Guer#ing
Use the rpm -E command to Euer# the data8ase of installed packages. The rpm -E foo
command 1ill print the package name? %ersion? and release num8er of the installed
package fooK
= rpm -4 !oo
foo--.+-*
M
7ote

7otice that 1e used the package name foo. To Euer# a package? #ou 1ill need to replace
foo 1ith the actual package name.
65
9nstead of specif#ing the package name? #ou can use the follo1ing options 1ith -E to
specif# the package<s= #ou 1ant to Euer#. These are called Package Specification
4ptions.
-a Eueries all currentl# installed packages.
-f ZfileV 1ill Euer# the package 1hich o1ns ZfileV. 0hen specif#ing a file? #ou must
specif# the full path of the file <for e(ample? OusrO8inOls=.
-p ZpackagefileV Eueries the package ZpackagefileV.
There are a num8er of 1a#s to specif# 1hat information to displa# a8out Eueried
packages. The follo1ing options are used to select the t#pe of information for 1hich #ou
are searching. These are called 9nformation Selection 4ptions.
-i displa#s package information including name? description? release? siFe? 8uild date?
install date? %endor? and other miscellaneous information.
-l displa#s the list of files that the package contains.
-s displa#s the state of all the files in the package.
-d displa#s a list of files marked as documentation <man pages? info pages? R'$!2's?
etc.=.
-c 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? pass1d?
initta8? etc.=.
.or the options that displa# lists of files? #ou can add -% to the command to displa# the
lists in a familiar ls -l format.
Verif#ing
Verif#ing a package compares information a8out files installed from a package 1ith the
same information from the original package. $mong other things? %erif#ing compares the
siFe? 2!5 sum? permissions? t#pe? o1ner? and group of each file.
The command rpm -V %erifies a package. You can use an# of the Package Selection
4ptions listed for Euer#ing to specif# the packages #ou 1ish to %erif#. $ simple use of
%erif#ing is rpm -V foo? 1hich %erifies that all the files in the foo package are as the#
1ere 1hen the# 1ere originall# installed. .or e(ampleK
66
To %erif# a package containing a particular fileK rpm -Vf O8inO%i
To %erif# $ installed packagesK rpm -Va
To %erif# an installed package against an RP2 package fileK rpm -Vp foo-*.+-*.i/&6.rpm
This command can 8e useful if #ou suspect that #our RP2 data8ases are corrupt.
9f e%er#thing %erified properl#? there 1ill 8e no output. 9f there are an# discrepancies the#
1ill 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 follo1ing characters
denote failure of certain testsK
5 P 2!5 checksum
S P file siFe
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 1a#.
6"
Compiling from the original source
6ead docmentation
ook for files calledK 97ST$? R'$!2'? S'TUP? or similar.
Read 1ith less docfile? or Fless docfile.gF for .gF files.
The procedre
The installation procedure for soft1are that comes in tar.gF and tar.8F- packages isn@t
al1a#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 soft1are package and #ou don@t e%en ha%e to kno1 1hat #ou@re doing.
5o1e%er? it@s health# to take a closer look at the installation procedure and see 1hat these
steps mean.
9npacking
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 kno1n as a tar8all. 0hen making the package? the source code and the other
needed files 1ere 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 1as compressed 1ith gFip? hence
the gF e(tension.
Some people 1ant to compress the tar archi%e 1ith 8Fip- instead of gFip. 9n these cases
the package has a tar.8F- e(tension. You install these packages e(actl# the same 1a# as
tar.gF packages? 8ut #ou use a 8it different command 1hen unpacking.
9t doesn@t matter 1here #ou put the tar8alls #ou do1nload from the internet 8ut 9 suggest
creating a special director# for do1nloaded tar8alls. 9n this tutorial 9 assume #ou keep
tar8alls in a director# called dls that #ou@%e created under #our home director#. 5o1e%er?
the dls director# is Nust an e(ample. You can put #our do1nloaded tar.gF or tar.8F-
soft1are packages into an# director# #ou 1ant. 9n this e(ample 9 assume #our username
is me and #ou@%e do1nloaded a package called pkg.tar.gF into the dls director# #ou@%e
created <OhomeOmeOdls=.
6&
4k? finall# on to unpacking the tar8all. $fter do1nloading the package? #ou unpack it
1ith this commandK
me@pter3 D-dls$ tar "vC! pkg&tar&gC
$s #ou can see? #ou use the tar command 1ith the appropriate options <(%Ff= for
unpacking the tar8all. 9f #ou ha%e a package 1ith 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@pter3 D-dls$ tar "vj! pkg&tar&)CE
0hat happens after unpacking? depends on the package? 8ut in most cases a director#
1ith the package@s name is created. The ne1l# created director# goes under the director#
1here #ou are right no1. To 8e sure? #ou can gi%e the ls commandK
me@pter3 D-dls$ ls
pkg pkg.tar.gF
meaputerK >OdlsL
9n our e(ample unpacking our package pkg.tar.gF did 1hat e(pected and created a
director# 1ith the package@s name. 7o1 #ou must cd into that ne1l# created director#K
me@pter3 D-dls$ cd pkg
me@pter3 D-dls-pkg$
Read an# documentation #ou find in this director#? like R'$!2' or 97ST$ files?
8efore continuingY
#on!igring
7o1? after 1e@%e changed into the package@s director# <and done a little RT.2@ing=? it@s
time to configure the package. Usuall#? 8ut not al1a#s <that@s 1h# #ou need to check out
the R'$!2' and 97ST$ files= it@s done 8# running the configure script.
You run the script 1ith this commandK
me@pter3 D-dls-pkg$ &-con!igre
0hen #ou run the configure script? #ou don@t actuall# compile an#thing #et. configure
Nust checks #our s#stem and assigns %alues for s#stem-dependent %aria8les. These %alues
are used for generating a 2akefile. The 2akefile in turn is used for generating the actual
8inar#.
6)
0hen #ou run the configure script? #ou@ll see a 8unch of 1eird messages scrolling on
#our screen. This is normal and #ou shouldn@t 1orr# a8out it. 9f configure finds an error?
it complains a8out it and e(its. 5o1e%er? if e%er#thing 1orks like it should? configure
doesn@t complain a8out an#thing? e(its? and shuts up.
9f configure e(ited 1ithout errors? it@s time to mo%e on to the ne(t step.
8ilding
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@pter3 D-dls-pkg$ make
7ote that make needs the 2akefile for 8uilding the program. 4ther1ise it doesn@t kno1
1hat to do. This is 1h# it@s so important to run the configure script successfull#? or
generate the 2akefile some other 1a#.
0hen #ou run make? #ou@ll see again a 8unch of strange messages filling #our screen.
This is also perfectl# normal and nothing #ou should 1orr# a8out. This step ma# take
some time? depending on ho1 8ig the program is and ho1 fast #our computer is. 9f #ou@re
doing this on an old dementic rig 1ith 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. 7o1? the final step is to install the program.
<nstalling
7o1 it@s finall# time to install the program. 0hen doing this #ou must 8e root. 9f #ou@%e
done things as a normal user? #ou can 8ecome root 1ith the su command. 9t@ll ask #ou the
root pass1ord and then #ou@re read# for the final stepY
me@pter3 D-dls-pkg$ s
Pass1ordK
rootaputerK OhomeOmeOdlsOpkgM
7o1 1hen #ou@re root? #ou can install the program 1ith the make install commandK
root@pter3 -home-me-dls-pkg= make install
$gain? #ou@ll get some 1eird messages scrolling on the screen. $fter it@s stopped?
congratsK #ou@%e installed the soft1are and #ou@re read# to run itY
,ecause in this e(ample 1e didn@t change the 8eha%ior of the configure script? the
program 1as installed in the default place. 9n man# cases it@s OusrOlocalO8in. 9f
"+
OusrOlocalO8in <or 1hate%er place #our program 1as 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 1ith 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@pter3 -home-me-dls-pkg= e"it
e(it
meaputerK >OdlsOpkgL
#leaning p the mess
9 8et #ou 1ant to sa%e some disk space. 9f this is the case? #ou@ll 1ant to get rid of some
files #ou don@t need. 0hen #ou ran make it created all sorts of files that 1ere needed
during the 8uild process 8ut are useless no1 and are Nust taking up disk space. This is
1h# #ou@ll 1ant to make cleanK
me@pter3 D-dls-pkg$ make clean
5o1e%er? make sure #ou keep #our 2akefile. 9t@s needed if #ou later decide to uninstall
the program and 1ant to do it as painlessl# as possi8leY
9ninstalling
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 1ith a package
manager? like rpm.
9f #ou 1ant to uninstall the soft1are #ou@%e compiled #ourself? do the o8%iousK do some
old-fashioned RT.2@ig. Read the documentation that came 1ith #our soft1are 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@pter3 -home-me-dls-pkg= make ninstall
9f #ou see 1eird te(t scrolling on #our screen <8ut at this point #ou@%e pro8a8l# got used
to 1eird te(t filling the screenB K-= 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 kno1 1here the program 1as installed? #ou@ll ha%e to manuall# delete the installed
files or the director# 1here #our program is. 9f #ou ha%e no idea 1here all the files are?
#ou@ll ha%e to read the 2akefile and see 1here all the files got installed? and then delete
them.
"*
yum
()ot 6epositories
$ repositor# is a prepared director# or 0e8 site that contains soft1are packages and
inde( files. Soft1are 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 ne1 applications or updates. You ma# use a single command to
update all s#stem soft1are? or search for ne1 soft1are 8# specif#ing criteria.
$ net1ork 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 soft1are
6ed Hat %evelopment 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.
Third-part# soft1are 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 third-part# repositories add packages to these groups? or
pro%ide their packages as additional groups.
"-
(vaila)le +ackage :rops
To %ie1 a list of all of the a%aila8le package groups for #our Red 5at s#stem? run the
command su -c @#um grouplist@.
Use repositories to ensure that #ou al1a#s recei%e current %ersions of soft1are. 9f se%eral
%ersions of the same package are a%aila8le? #our management utilit# automaticall# selects
the latest %ersion.
()ot %ependencies
Some of the files installed on a Red 5at distri8ution are li8raries 1hich ma# pro%ide
functions to multiple applications. 0hen an application reEuires a specific li8rar#? the
package 1hich 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 1ithin 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 ne1 application has reEuirements
that conflict 1ith e(isting soft1are? #um a8orts 1ithout making an# changes to #our
s#stem.
9nderstanding +ackage >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
tsclient-+.*/--6.i/&6.rpm
2anagement utilities commonl# refer to packages 1ith one of three formatsK
Package nameK tsclient
Package name 1ith %ersion and release num8ersK tsclient-+.*/--6
Package name 1ith hard1are architectureK tsclient.i/&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
hard1are architecture specified for the package is the minimum t#pe of machine reEuired
to use the package.
i/&6
Suita8le for an# current 9ntel-compati8le computer
noarch
"/
Compati8le 1ith all computer architectures
ppc
Suita8le for Po1erPC s#stems? such as $pple Po1er 2acintosh
(&6^66
Suita8le for 66-8it 9ntel-compati8le processors? such as 4pterons
Some soft1are ma# 8e optimiFed for particular t#pes of 9ntel-compati8le machine.
Separate packages ma# 8e pro%ided for i/&6? i5&6? i6&6 and (&6^66 computers. $
machine 1ith at least an 9ntel Pentium? V9$ C/ or compati8le CPU ma# use i5&6
packages. Computers 1ith 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 hard1are architecture
of #our computer.
Specif# a package 1ith 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
1hen #ou kno1 that the default package selection has a 8ug or other fault that makes it
unsuita8le for installation.
*anaging So!tware with ym
Use the #um utilit# to modif# the soft1are on #our s#stem in four 1a#sK
To install ne1 soft1are from package repositories
To install ne1 soft1are from an indi%idual package file
To update e(isting soft1are on #our s#stem
To remo%e un1anted soft1are from #our s#stem
To use #um? specif# a function and one or more packages or package groups. 'ach
section 8elo1 gi%es some e(amples.
.or each operation? #um do1nloads the latest package information from the configured
repositories. 9f #our s#stem uses a slo1 net1ork connection #um ma# reEuire se%eral
seconds to do1nload 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#
"6
include the installation? update? or remo%al of additional packages? in order to resol%e
soft1are dependencies.
This is an e(ample of the transaction for installing tsclientK
```````````````````````````````````````````````````````````````
``````````````
Package $rch Version Repositor# SiFe
```````````````````````````````````````````````````````````````
``````````````
9nstallingK
tsclient i/&6 +.*/--6 8ase -6" k
9nstalling for dependenciesK
rdesktop i/&6 *.6.+-- 8ase *+" k
Transaction Summar#
```````````````````````````````````````````````````````````````
``````````````
9nstall - Package<s=
Update + Package<s=
Remo%e + Package<s=
Total do1nload siFeK /55 k
9s this ok H#O7IK
'(ample *. .ormat of #um Transaction Reports
Re%ie1 the list of changes? and then press # to accept and 8egin the process. 9f #ou press
7 or 'nter? #um does not do1nload or change an# packages.
+ackage Jersions
The #um utilit# onl# displa#s and uses the ne1est %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
1arningK rpmts^5dr.rom.dnoK 5eader V/ !S$ signatureK 74D'Y? ke# 9! 6f-a6fd-
pu8lic ke# not a%aila8le for tsclient-+.*/--6.i/&6.rpm
Retrie%ing 3P3 ke# from fileKOOOetcOpkiOrpm-gpgORP2-3P3-D'Y-fedora
9mporting 3P3 ke# +(6.-$6.!- _.edora ProNect Zfedoraaredhat.comV_
9s this ok H#O7IK
'(ample -. .ormat of #um Pu8lic De# 9mport
"5
Check the pu8lic ke#? and then press # to import the ke# and authoriFe the ke# for use. 9f
#ou press 7 or 'nter? #um stops 1ithout installing an# packages.
To ensure that do1nloaded 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# do1nloaded and %erified? #um applies them to #our
s#stem.
Transaction Log
'%er# completed transaction records the affected packages in the log file
O%arOlogO#um.log. You ma# onl# read this file 1ith root access.
<nstalling >ew So!tware with ym
To install the package tsclient? enter the commandK
s -c Kym install tsclientK
'nter the pass1ord for the root account 1hen prompted.
To install the package group 2#SG !ata8ase? enter the commandK
s -c Kym gropinstall A*ySLL %ata)aseAK
'nter the pass1ord for the root account 1hen prompted.
>ew Services 6e4ire (ctivation
0hen #ou install a ser%ice? Red 5at does not acti%ate or start it. To configure a ne1
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 command-line utilities.
9pdating So!tware with ym
To update the tsclient package to the latest %ersion? t#peK
s -c Kym pdate tsclientK
'nter the pass1ord for the root account 1hen prompted.
>ew So!tware Jersions 6e4ire 6eloading
9f a piece of soft1are is in use 1hen #ou update it? the old %ersion remains acti%e until the
application or ser%ice is restarted. Dernel updates take effect 1hen #ou re8oot the s#stem.
Mernel +ackages
"6
Dernel packages remain on the s#stem after the# ha%e 8een superseded 8# ne1er
%ersions. This ena8les #ou to 8oot #our s#stem 1ith an older kernel if an error occurs
1ith 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#SG !ata8ase? enter the
commandK
s -c Kym groppdate A*ySLL %ata)aseAK
'nter the pass1ord for the root account 1hen prompted. Updating the 'ntire S#stem
6emoving So!tware with ym
To remo%e soft1are? #um e(amines #our s#stem for 8oth the specified soft1are? and an#
soft1are 1hich claims it as a dependenc#. The transaction to remo%e the soft1are deletes
8oth the soft1are and the dependencies.
To remo%e the tsclient package from #our s#stem? use the commandK
s -c Kym remove tsclientK
'nter the pass1ord for the root account 1hen prompted.
To remo%e all of the packages in the package group 2#SG !ata8ase? enter the
commandK
s -c Kym gropremove A*ySLL %ata)aseAK
'nter the pass1ord for the root account 1hen prompted.
sysctl
S#sctl is an interface for e(amining and d#namicall# changing parameters in a ,S! Uni(
<or inu(= 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 1ill use as an 7.S client?
or the ma(imum num8er of processes on the s#stem\ or descri8e? ena8le or disa8le
8eha%iors such as 9P for1arding? securit# restrictions on the superuser <the _securele%el_=?
or de8ugging output.
3enerall#? a s#stem call or s#stem call 1rapper is pro%ided for use 8# programs? as 1ell
as an administrati%e program and a configuration file <for setting the tuna8le parameters
1hen the s#stem 8oots=.
""
This feature appeared in the _6.6,S!_ %ersion of Uni(? and is also used in the inu(
kernel. 9t has the ad%antage o%er hardcoded constants that changes to the parameters can
8e made d#namicall# 1ithout recompiling the kernel.
'(amples
0hen 9P for1arding is ena8led? the operating s#stem kernel 1ill act as a router. .or the
inu( kernel? the parameter net.ip%6.ip^for1ard can 8e set to * to ena8le this 8eha%ior. 9n
.ree,S!? 7et,S! and 4pen,S! the parameter is net.inet.ip.for1arding.
9n most s#stems? the command s#sctl -1 parameter`* 1ill ena8le the desired 8eha%ior.
This 1ill persist until the ne(t re8oot. 9f the 8eha%ior should 8e ena8led 1hene%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 inu( kernel? the proc files#stem also pro%ides an interface to the s#sctl
parameters. .or e(ample? the parameter net.ip%6.ip^for1ard corresponds 1ith the file
OprocOs#sOnetOip%6Oip^for1ard. Reading or changing this file is eEui%alent to changing the
parameter using the s#sctl command.
4racle parameters
kernel.shmma(`-/*/6&-)6/
kernel.msgmni`*+-6
kernel.sem`*-5+ -56+++ *++ *+-6
%m.ma(^map^count`/+++++
net.ip%6.ip^local^port^range ` *+-6 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 follo1 the discussion on the follo1ing pages.
9n inu(? partitions are represented 8# de%ice files. These are phone# files located in Ode%.
5ere are a fe1 entriesK
8r1-r1---- * root disk /? + 2a# 5 *))& hda
8r1-r1---- * root disk &? + 2a# 5 *))& sda
cr1------- * root tt# 6? 66 2a# 5 *))& tt#S+
"&
$ de%ice file is a file 1ith t#pe c < for _character_ de%ices? de%ices that do not use the
8uffer cache= or 8 <for _8lock_ de%ices? 1hich go through the 8uffer cache=. 9n inu(? all
disks are represented as 8lock de%ices onl#.
%evice names
>aming #onvention
,# con%ention? 9!' dri%es 1ill 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 t1o 9!' controllers? each of 1hich can ha%e t1o 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 1rite to these de%ices directl# <using cat or dd=. 5o1e%er? since these de%ices
represent the entire disk? starting at the first 8lock? #ou can mistakenl# o%er1rite the
master 8oot record and the partition ta8le? 1hich 1ill render the dri%e unusa8le.
Ta8le /. partition names
dri%e name dri%e controller dri%e num8er partition t#pe partition
num8er
Ode%Ohda* * * primar# *
Ode%Ohda- * * primar# -
Ode%Ohda/ * * primar# /
Ode%Ohda6 * * s1ap 7$
Ode%Ohd8* * - primar# *
Ode%Ohd8- * - primar# -
Ode%Ohd8/ * - primar# /
Ode%Ohd86 * - primar# 6
")
4nce a dri%e has 8een partitioned? the partitions 1ill represented as num8ers on the end
of the names. .or e(ample? the second partition on the second dri%e 1ill 8e Ode%Ohd8-.
The partition t#pe <primar#= is listed in the ta8le a8o%e for clarit#?
Ta8le 6. 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%Osda/ * 6 primar# /
SCS9 dri%es follo1 a similar pattern\ The# are represented 8# @sd@ instead of @hd@. The first
partition of the second SCS9 dri%e 1ould 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 (ssignment
Under <Sun= Solaris and <S39= 9R9;? the de%ice name gi%en to a SCS9 dri%e has some
relationship to 1here #ou plug it in. Under linu(? there is onl# 1ailing 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 1hich go from * through *5. o1er SCS9 9! num8ers are
assigned lo1er-order letters. .or e(ample? if #ou ha%e t1o dri%es num8ered - and 5? then
M- 1ill 8e Ode%Osda and M5 1ill 8e Ode%Osd8. 9f #ou remo%e either? all the higher num8ered
dri%es 1ill 8e renamed the ne(t time #ou 8oot up.
9f #ou ha%e t1o SCS9 controllers in #our linu( 8o(? #ou 1ill need to e(amine the output
of O8inOdmesg in order to see 1hat name each dri%e 1as assigned. 9f #ou remo%e one of
t1o controllers? the remaining controller might ha%e all its dri%es renamed. 3rrr...
There are t1o 1ork-arounds\ 8oth in%ol%e using a program to put a la8el on each
partition. The la8el is persistent e%en 1hen the de%ice is ph#sicall# mo%ed. You then refer
to the partition directl# or indirectl# 8# la8el.
&+
Logical +artitions
Ta8le 5. ogical Partitions
dri%e name dri%e controller dri%e num8er partition t#pe partition
num8er
Ode%Ohd8* * - primar# *
Ode%Ohd8- * - e(tended 7$
Ode%Ohda5 * - logical -
Ode%Ohd86 * - logical /
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 kno1 to deal 1ith linu( disk de%ices. .or
the sake of completeness? see Dristian@s discussion of de%ice num8ers 8elo1.
%evice nm)ers
The onl# important thing 1ith a de%ice file are its maNor and minor de%ice num8ers?
1hich are sho1n instead of the file siFeK
$ ls -l -dev-hda
Ta8le 6. !e%ice file attri8utes
8r1-r1---- * root disk /? + Cul *&
*))6
Ode%Ohda
permissions o1ner group maNor
de%ice
num8er
minor
de%ice
num8er
date de%ice
name
0hen accessing a de%ice file? the maNor num8er selects 1hich de%ice dri%er is 8eing
called to perform the inputOoutput operation. This call is 8eing done 1ith the minor
num8er as a parameter and it is entirel# up to the dri%er ho1 the minor num8er is 8eing
interpreted. The dri%er documentation usuall# descri8es ho1 the dri%er uses minor
num8ers. .or 9!' disks? this documentation is in OusrOsrcOlinu(O!ocumentationOide.t(t.
.or SCS9 disks? one 1ould e(pect such documentation in
OusrOsrcOlinu(O!ocumentationOscsi.t(t? 8ut it isn@t there. 4ne has to look at the dri%er
source to 8e sure < OusrOsrcOlinu(Odri%erOscsiOsd.cK*&6-*)6=. .ortunatel#? there is Peter
$n%in@s list of de%ice num8ers and names in OusrOsrcOlinu(O!ocumentationOde%ices.t(t\
see the entries for 8lock de%ices? maNor /? --? //? /6 for 9!' and maNor & for SCS9 disks.
&*
The maNor and minor num8ers are a 8#te each and that is 1h# 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 1ith a
%olume la8el. Such a file s#stem could 8e the linu( standard e(t- file s#stem or linu(
s1ap space? or e%en foreign file s#stems like <2icrosoft= 7T.S or <Sun= U.S. There is a
numerical code associated 1ith each partition t#pe. .or e(ample? the code for e(t- is
+(&/ and linu( s1ap is +(&-. To see a list of partition t#pes and their codes? e(ecute
Os8inOsfdisk -T
7oreign +artition Types
The partition t#pe codes ha%e 8een ar8itraril# chosen <#ou can@t figure out 1hat the#
should 8e= and the# are particular to a gi%en operating s#stem. Therefore? it is
theoreticall# possi8le that if #ou use t1o operating s#stems 1ith the same hard dri%e? the
same code might 8e used to designate t1o different partition t#pes. 4SO- marks its
partitions 1ith a +(+" t#pe and so does 0indo1s 7T@s 7T.S. 2S-!4S allocates se%eral
t#pe codes for its %arious fla%ors of .$T file s#stemsK +(+*? +(+6 and +(+6 are kno1n.
!R-!4S used +(&* to indicate protected .$T partitions? creating a t#pe clash 1ith
inu(O2ini( at that time? 8ut neither inu(O2ini( nor !R-!4S are 1idel# used an#
more.
4SO- marks its partitions 1ith a +(+" t#pe and so does 0indo1s 7T@s 7T.S. 2S-!4S
allocates se%eral t#pe codes for its %arious fla%ors of .$T file s#stemsK +(+*? +(+6 and
+(+6 are kno1n. !R-!4S used +(&* to indicate protected .$T partitions? creating a t#pe
clash 1ith inu(O2ini( at that time? 8ut neither inu(O2ini( nor !R-!4S are 1idel#
used an# more.
+rimary +artitions
The num8er of partitions on an 9ntel-8ased s#stem 1as limited from the %er# 8eginningK
The original partition ta8le 1as installed as part of the 8oot sector and held space for onl#
four partition entries. These partitions are no1 called primar# partitions.
Logical +artitions
4ne primar# partition of a hard dri%e ma# 8e su8partitioned. These are logical partitions.
This effecti%el# allo1s 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 o1n 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?
1hich implies that the num8er of logical partitions is unlimited. 5o1e%er? linu( imposes
&-
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 6/
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 in-memor# pages 1hich 1ill 8e referenced 8# the
processor in the %er# near future is called a _1orking set._ inu( tries to predict these
memor# accesses <assuming that recentl# used pages 1ill 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 1ill tr# to free up R$2
8# 1riting pages to disk. This is 1hat s1ap space is for. 9t effecti%el# increases the
amount of memor# #ou ha%e a%aila8le. 5o1e%er? disk 9O4 is a8out a hundred times
slo1er than reading from and 1riting to R$2. Consider this emergenc# memor# and not
e(tra memor#.
9f memor# 8ecomes so scarce that the kernel pages out from the 1orking 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 a1a# like craF#? 8ut
the computer is slo1 to the point of 8eing unusa8le. S1ap space is something #ou need to
ha%e? 8ut it is no su8stitute for sufficient R$2.
+artitioning with !disk
This section sho1s #ou ho1 to actuall# partition #our hard dri%e 1ith the fdisk utilit#.
inu( allo1s onl# 6 primar# partitions. You can ha%e a much larger num8er of logical
partitions 8# su8-di%iding one of the primar# partitions. 4nl# one of the primar#
partitions can 8e su8-di%ided.
'(amplesK
.our primar# partitions
2i(ed primar# and logical partitions
!disk sage
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 ne1 partition
&/
d delete a partition
E Euit 1ithout sa%ing changes
1 1rite the ne1 partition ta8le and e(it
Changes #ou make to the partition ta8le do not take effect until #ou issue the 1rite <1=
command. 5ere is a sample partition ta8leK
!isk Ode%Ohd8K 66 heads? 6/ sectors? 6-* c#linders
Units ` c#linders of 6+/- A 5*- 8#tes

!e%ice ,oot Start 'nd ,locks 9d S#stem
Ode%Ohd8* A * *&6 /"+)*-: &/ inu(
Ode%Ohd8- *&5 /6& /"+)66 &/ inu(
Ode%Ohd8/ /6) 55- /"+)66 &/ inu(
Ode%Ohd86 55/ 6-* */)*+6 &- inu( s1ap
The first line sho1s the geometr# of #our hard dri%e. 9t ma# not 8e ph#sicall# accurate?
8ut #ou can accept it as though it 1ere. The hard dri%e in this e(ample is made of /-
dou8le-sided platters 1ith one head on each side <pro8a8l# not true=. 'ach platter has 6-*
concentric tracks. $ /-dimensional track <the same track on all disks= is called a c#linder.
'ach track is di%ided into 6/ sectors. 'ach sector contains 5*- 8#tes of data. Therefore
the 8lock siFe in the partition ta8le is 66 heads A 6/ sectors A 5*- 8#tes er...di%ided 8#
*+-6. <See 6 for discussion on pro8lems 1ith this calculation.= The start and end %alues
are c#linders.
7or primary partitions
The o%er%ie1K
!ecide on the siFe of #our s1ap space and 1here it ought to go. !i%ide up the remaining
space for the three other partitions.
'(ampleK
9 start fdisk from the shell promptK
= !disk -dev-hd)
1hich indicates that 9 am using the second dri%e on m# 9!' controller. 0hen 9 print the
<empt#= partition ta8le? 9 Nust get configuration information.
Command <m for help=K p
!isk Ode%Ohd8K 66 heads? 6/ sectors? 6-* c#linders
Units ` c#linders of 6+/- A 5*- 8#tes
&6
9 kne1 that 9 had a *.-38 dri%e? 8ut no1 9 reall# kno1K 66 A 6/ A 5*- A 6-* `
*-&*)&-666 8#tes. 9 decide to reser%e *-&28 of that space for s1ap? lea%ing
**5/)&-666. 9f 9 use one of m# primar# partitions for s1ap? that means 9 ha%e three left
for e(t- partitions. !i%ided eEuall#? that makes for /&628 per partition. 7o1 9 get to
1ork.
Command <m for help=K n
Command action
e e(tended
p primar# partition <*-6=
p
Partition num8er <*-6=K *
.irst c#linder <*-6-*? default *=KZR'TUR7V
Using default %alue *
ast c#linder or :siFe or :siFe2 or :siFeD <*-6-*? default 6-*=K :/&62
7e(t? 9 set up the partition 9 1ant to use for s1apK
Command <m for help=K n
Command action
e e(tended
p primar# partition <*-6=
p
Partition num8er <*-6=K -
.irst c#linder <*)"-6-*? default *)"=KZR'TUR7V
Using default %alue *)"
ast c#linder or :siFe or :siFe2 or :siFeD <*)"-6-*? default 6-*=K :*-&2
7o1 the partition ta8le looks like thisK
!e%ice ,oot Start 'nd ,locks 9d S#stem
Ode%Ohd8* * *)6 /)5*+6 &/ inu(
Ode%Ohd8- *)" -6- *//+56 &/ inu(
9 set up the remaining t1o partitions the same 1a# 9 did the first. .inall#? 9 make the first
partition 8oota8leK
Command <m for help=K a
Partition num8er <*-6=K *
$nd 9 make the second partition of t#pe s1apK
Command <m for help=K t
Partition num8er <*-6=K -
5e( code <t#pe to list codes=K &-
Changed s#stem t#pe of partition - to &- <inu( s1ap=
Command <m for help=K p
The end resultK
!isk Ode%Ohd8K 66 heads? 6/ sectors? 6-* c#linders
Units ` c#linders of 6+/- A 5*- 8#tes
&5

!e%ice ,oot Start 'nd ,locks 9d S#stem
Ode%Ohd8* A * *)6 /)5*+6: &/ inu(
Ode%Ohd8- *)" -6- *//+56 &- inu( s1ap
Ode%Ohd8/ -6/ 65& /)5*/6 &/ inu(
Ode%Ohd86 65) 6-* /-&6+& &/ inu(
.inall#? 9 issue the 1rite command <1= to 1rite the ta8le on the disk.
*i"ed primary and logical partitions
The o%er%ie1K create one use one of the primar# partitions to house all the e(tra
partitions. Then create logical partitions 1ithin it. Create the other primar# partitions
8efore or after creating the logical partitions.
'(ampleK
9 start fdisk from the shell promptK
= !disk -dev-sda
1hich indicates that 9 am using the first dri%e on m# SCS9 chain.
.irst 9 figure out ho1 man# partitions 9 1ant. 9 kno1 m# dri%e has a *&/38 capacit# and
9 1ant -638 partitions <8ecause 9 happen to ha%e 8ack-up tapes that are a8out that siFe=.
*&/38 O -638 ` >"
so 9 1ill need " partitions. '%en though fdisk accepts partition siFes e(pressed in 28 and
D8? 9 decide to calculate the num8er of c#linders that 1ill end up in each partition
8ecause fdisk reports start and stop points in c#linders. 9 see 1hen 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 1rong 1ith that? 8ut this is larger than *+-6? and could in
V certain setups cause pro8lems 1ithK *= soft1are that runs at 8oot
V time <e.g.? 94= -= 8ooting and partitioning soft1are from other
V 4Ss <e.g.? !4S .!9SD? 4SO- .!9SD=
So? --&++ total c#linders di%ided 8# se%en partitions is /-5& c#linders. 'ach partition
1ill 8e a8out /-5& c#linders long. 9 ignore the 1arning msg 8ecause this is not m# 8oot
dri%e.
Since 9 ha%e 6 primar# partitions? / of them can 8e /-5& long. The e(tended partition 1ill
ha%e to 8e <6 A /-5&=? or */+/-? c#linders long in order to contain the 6 logical partitions.
&6
9 enter the follo1ing commands to set up the first of the / primar# partitions <stuff 9 t#pe
is 8old =K
Command <m for help=K n
Command action
e e(tended
p primar# partition <*-6=
p
Partition num8er <*-6=K *
.irst c#linder <*---&++? default *=K ZR'TUR7V
Using default %alue *
ast c#linder or :siFe or :siFe2 or :siFeD <*---&++? default --&++=K /-5&
The last partition is the e(tended partitionK
Partition num8er <*-6=K 6
.irst c#linder <)""5---&++? default )""5=K ZR'TUR7V
Using default %alue )""5
ast c#linder or :siFe or :siFe2 or :siFeD <)""5---&++? default --&++=K ZR'TUR7V
Using default %alue --&++
The result? 1hen 9 issue the print ta8le command isK
Ode%Osda* * /-5& -6*6)&5/: &/ inu(
Ode%Osda- /-5) 65*6 -6*6)&&5 &/ inu(
Ode%Osda/ 65*" )""6 -6*6)&&5 &/ inu(
Ode%Osda6 )""5 --&++ *+66/*/65 5 '(tended
7e(t 9 segment the e(tended partition into 6 logical partitions? starting 1ith the first
logical partition? into /-5&-c#linder segments. The logical partitions automaticall# start
from Ode%Osda5.
Command <m for help=K n
.irst c#linder <)""5---&++? default )""5=K ZR'TUR7V
Using default %alue )""5
ast c#linder or :siFe or :siFe2 or :siFeD <)""5---&++? default --&++=K */+/-
The end result isK
!e%ice ,oot Start 'nd ,locks 9d S#stem
Ode%Osda* * /-5& -6*6)&5/: &/ inu(
Ode%Osda- /-5) 65*6 -6*6)&&5 &/ inu(
Ode%Osda/ 65*" )""6 -6*6)&&5 &/ inu(
Ode%Osda6 )""5 --&++ *+66/*/65 5 '(tended
Ode%Osda5 )""5 */+/- -6*6)&5/: &/ inu(
Ode%Osda6 */+// *6-)+ -6*6)&5/: &/ inu(
Ode%Osda" *6-)* *)5&6 -665)+-/: &/ inu(
Ode%Osda& *)5&5 --&++ -5&/-6&&: &/ inu(
&"
.inall#? 9 issue the 1rite command <1= to 1rite the ta8le on the disk. To make the
partitions usa8le? 9 1ill ha%e to format each partition and then mount it.
S)mitted E"amples
9@d like to su8mit m# partition la#out? 8ecause it 1orks 1ell 1ith an# distri8ution of
inu( <e%en 8ig RP2 8ased ones=. 9 ha%e one hard dri%e that ... is *+ gigs? e(actl#.
0indo1s can@t see a8o%e )./ gigs of it? 8ut inu( can see it all? and use it all. 9t also has
much more than *+-6 c#lenders.
Ta8le ". Partition la#out e(ample
Partition 2ount point SiFe
Ode%Ohda* O8oot <*5 megs=
Ode%Ohda- 1indo1s )& partition <- gigs=
Ode%Ohda/ e(tended <7O$=
Ode%Ohda5 s1ap space <66 megs=
Ode%Ohda6 Otmp <5+ megs=
Ode%Ohda" O <*5+ megs=
Ode%Ohda& Ousr <*.5 gigs=
Ode%Ohda) Ohome <rest of dri%e=
LV(
V2 is a logical %olume manager for the inu( kernel. 9t 1as originall# 1ritten in *))&
8# 5einF 2auelshagen? 1ho 8ased its design on that of the V2 in 5P-U;.
The installers for the Red 5at? 2ontaVista inu(? S'!? !e8ian 37UOinu(? and
U8untu distri8utions are V2-a1are and can install a 8oota8le s#stem 1ith a root
files#stem on a logical %olume.
7eatres
The V2 canK
ResiFe %olume groups online 8# a8sor8ing ne1 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 read-onl# snapshots of logical %olumes <V2*=.
Create read-1rite snapshots of logical %olumes <V2-=.
Stripe 1hole or parts of logical %olumes across multiple PVs? in a fashion similar to
R$9!+.
2irror 1hole or parts of logical %olumes? in a fashion similar to R$9!*
2o%e online logical %olumes 8et1een PVs.
&&
Split or merge %olume groups in situ <as long as no logical %olumes span the split=. This
can 8e useful 1hen migrating 1hole logical %olumes to or from offline storage.
*issing !eatres
V2 cannot pro%ide parit# 8ased redundanc# similar to R$9!6? R$9!5? or R$9!6.
<mplementation
V2 keeps a metadata header at the start of e%er# PV? each of 1hich is uniEuel#
identified 8# a UU9!. 'ach PV@s header is a complete cop# of the entire %olume group@s
la#out? including the UU9!s of all other PV? the UU9!s of all logical %olumes and an
allocation map of P's to 's.
9n the -.6-series inu( kernels? the V2 is implemented in terms of the de%ice mapper? a
8lock-le%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# hard-to-de8ug kernel
code needed to implement the V2 and also allo1s its 9O4 redirection ser%ices to 8e
shared 1ith other %olume managers <such as 'V2S=.
$n# V2-specific code is pushed out into its user-space 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.
oops 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 ne1? empt# de%ice mapping.
2aps it <1ith the _linear_ target= onto the data areas of the PVs the logical %olume
8elongs to.
To mo%e an online logical %olume 8et1een PVs? the _p%mo%e_ toolK
Creates a ne1? empt# de%ice mapping for the destination.
$pplies the _mirror_ target to the original and destination maps. The kernel 1ill 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 1ith the destination 1hen the mirror comes into s#nc? then
destro#s the original.
These de%ice mapper operations take place transparentl#? 1ithout applications or
files#stems 8eing a1are that their underl#ing storage is mo%ing.
E"ample3 ( 8asic 7ile Server
$ simple? practical e(ample of V2 use is a traditional file ser%er? 1hich 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\ 1ho kno1s 1hat storage
challenges ne(t #ear@s technolog# 1ill 8ringB
.or e(ample? suppose #our reEuirements areK
6++3 - arge media file storage
5+3 - 4nline 8ackups of t1o laptops and three desktops <*+3 each=
*+3 - Shared files
Ultimatel#? these reEuirements ma# increase a great deal o%er the ne(t #ear or t1o? 8ut
e(actl# ho1 much and 1hich partition 1ill gro1 the most are still unkno1n.
%isk Hardware
Traditionall#? a file ser%er uses SCS9 disks? 8ut toda# S$T$ disks offer an attracti%e
com8ination of speed and lo1 cost. $t the time of this 1riting? -5+ 3, S$T$ dri%es are
commonl# a%aila8le for around L*++\ for a tera8#te? the cost is around L6++.
S$T$ dri%es are not named like $T$ dri%es <hda? hd8=? 8ut like SCS9 <sda? sd8=. 4nce the
s#stem has 8ooted 1ith S$T$ support? it has four ph#sical de%ices to 1ork 1ithK
Ode%Osda -5*.+ 3,
Ode%Osd8 -5*.+ 3,
Ode%Osdc -5*.+ 3,
Ode%Osdd -5*.+ 3,
7e(t? partition these for use 1ith V2. You can do this 1ith fdisk 8# specif#ing the
_inu( V2_ partition t#pe &e. The finished product looks like thisK
= !disk -l -dev-sdd
!isk Ode%OsddK -5*.+ 3,? -5*+++*)/+-6 8#tes
-55 heads? 6/ sectorsOtrack? /+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* * /+5*5 -65***"+6 &e inu( V2
7otice the partition t#pe is &e? or _inu( V2._
#reating a Jirtal Jolme
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 V2? allo1ing creation of
%olume groups. To e(amine a%aila8le PVs? use the p%displa# command. This s#stem 1ill
use a single-%olume group named data%gK
= vgcreate datavg -dev-sda; -dev-sd); -dev-sdc; -dev-sdd;
Use %gdispla# to see the ne1l# created data%g V3 1ith the four dri%es stitched together.
7o1 create the logical %olumes 1ithin themK
= lvcreate --name medialv --siCe N11:
= lvcreate --name )ackplv --siCe O1:
= lvcreate --name sharelv --siCe ;1:
0ithout V2? #ou might allocate all a%aila8le disk space to the partitions #ou@re
creating? 8ut 1ith V2? it is 1orth1hile to 8e conser%ati%e? allocating onl# half the
a%aila8le space to the current reEuirements. $s a general rule? it@s easier to gro1 a
files#stem than to shrink it? so it@s a good strateg# to allocate e(actl# 1hat #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 ne1 %olumes 1hen ne1 needs arise <such as
a separate encr#pted file share for sensiti%e data=. To e(amine these %olumes? use the
l%displa# command.
7o1 #ou ha%e se%eral nicel# named logical %olumes at #our disposalK
Ode%Odata%gO8ackupl% <also Ode%OmapperOdata%g-8ackupl%=
Ode%Odata%gOmedial% <also Ode%OmapperOdata%g-medial%=
Ode%Odata%gOsharel% <also Ode%OmapperOdata%g-sharel%=
)*
U,./ Sumary
Typographical conventions
9n 1hat follo1s? 1e shall use the follo1ing t#pographical con%entionsK
Characters 1ritten in bold typewriter font are commands to 8e t#ped into the
computer as the# stand.
Characters 1ritten in italic typewriter font indicate non-specific file or
director# names.
0ords inserted 1ithin sEuare 8rackets [Ctrl] indicate ke#s to 8e pressed.
So? for e(ample?
T ls anydirectory H'nterI
means _at the U79; prompt T? t#pe ls follo1ed 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.
7oteK U79; is case-sensit%e? so S is not the same as ls.
The same applies to filenames? so m#file.t(t? 2#.ile.t(t and 2Y.9'.T;T are three
seperate files. ,e1are if cop#ing files to a PC? since !4S and 0indo1s do not make this
distinction.
.ntroduction
This session concerns U79;? 1hich is a common operating s#stem. ,# operating s#stem?
1e mean the suite of programs 1hich make the computer 1ork. U79; is used 8# the
1orkstations and multi-user ser%ers 1ithin the school.
4n ; terminals and the 1orkstations? ; 0indo1s pro%ide a graphical interface 8et1een
the user and U79;. 5o1e%er? kno1ledge of U79; is reEuired for operations 1hich aren@t
co%ered 8# a graphical program? or for 1hen there is no ; 1indo1s s#stem? for e(ample?
in a telnet session.
)-
The 9><P operating system
The U79; operating s#stem is made up of three parts\ the kernel? the shell and the
programs.
The kernel
The kernel of U79; 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 1a# that the shell and the kernel 1ork together? suppose a user
t#pes rm myfile <1hich has the effect of remo%ing the file my!ile=. 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. 0hen the process rm myfile has
finished running? the shell then returns the U79; prompt T to the user? indicating that it
is 1aiting for further commands.
The shell
The shell acts as an interface 8et1een the user and the kernel. 0hen a user logs in? the
login program checks the username and pass1ord? and then starts another program called
the shell. The shell is a command line interpreter <C9=. 9t interprets the commands the
user t#pes in and arranges for them to 8e carried out. The commands are themsel%es
programsK 1hen the# terminate? the shell gi%es the user another prompt <T on our
s#stems=.
The adept user can customise hisOher o1n 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 - ,# t#ping part of the name of a command? filename or director#
and pressing the HTa8I ke#? the tcsh shell 1ill complete the rest of the name automaticall#.
9f the shell finds more than one name 8eginning 1ith those letters #ou ha%e t#ped? it 1ill
8eep? prompting #ou to t#pe a fe1 more letters 8efore pressing the ta8 ke# again.
5istor# - 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 do1n the list or t#pe histor# for a list of
pre%ious commands.
)/
7iles and processes
'%er#thing in U79; 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 1ritten in some high-le%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? 1hich ma# 8e a mi(ture of
other directories <su8directories= and ordinar# files.
The %irectory Strctre
$ll the files are grouped together in the director# structure. The file-s#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? 1e see that the director# ee5*a8 contains the su8director# uni(stuff
and a file proN.t(t
Starting an Pterminal session
)6
To start an ;term session? click on the Uni( Terminal icon on #our desktop? or from the
drop-do1n menus

$n ;terminal 1indo1 1ill appear 1ith a Uni( prompt? 1aiting for #ou to start entering
commands.
)5
Part *ne
;&; Listing !iles and directories
ls 0list1
0hen #ou first login? #our current 1orking director# is #our home director#. Your home
director# has the same name as #our user-name? for e(ample? ee)*a8? and it is 1here
#our personal files and su8directories are sa%ed.
To find out 1hat is in #our home director#? t#pe
T ls <short for list=
The ls command lists the contents of #our current 1orking director#.
There ma# 8e no files %isi8le in #our home director#? in 1hich case? the U79; prompt
1ill 8e returned. $lternati%el#? there ma# alread# 8e some files inserted 8# the S#stem
$dministrator 1hen #our account 1as created.
ls does not? in fact? cause all the files in #our home director# to 8e listed? 8ut onl# those
ones 1hose name does not 8egin 1ith a dot <.= .iles 8eginning 1ith a dot <.= are kno1n as
hidden files and usuall# contain important program configuration information. The# are
hidden 8ecause #ou should not change them unless #ou are %er# familiar 1ith U79;YYY
To list all files in #our home director# including those 1hose names 8egin 1ith a dot?
t#pe
)6
T ls -a
ls is an e(ample of a command 1hich 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 1hich options a particular command can take? and ho1 each option modifies the
8eha%iour of the command. <See later in this tutorial=
;&E *aking %irectories
mkdir 0make directory1
0e 1ill no1 make a su8director# in #our home director# to hold the files #ou 1ill 8e
creating and using in the course of this tutorial. To make a su8director# called
unixstuff in #our current 1orking director# t#pe
T mkdir uni(stuff
To see the director# #ou ha%e Nust created? t#pe
T ls
;&F #hanging to a di!!erent directory
cd 0change directory1
The command cd directory means change the current 1orking director# to
@director#@. The current 1orking director# ma# 8e thought of as the director# #ou are in?
i.e. #our current position in the file-s#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 <1hich should 8e empt#=
!"ercise 2a
2ake another director# inside the ni"st!! director# called )ackps
;&N The directories & and &&
Still in the ni"st!! director#? t#pe
)"
T ls -a
$s #ou can see? in the ni"st!! director# <and in all other directories=? there are t1o
special directories called <&= and <&&=
9n U79;? <&= means the current director#? so t#ping
T cd .
74T'K there is a space 8et1een cd and the dot
means sta# 1here #ou are <the ni"st!! director#=.
This ma# not seem %er# useful at first? 8ut using <&= as the name of the current director#
1ill sa%e a lot of t#ping? as 1e shall see later in the tutorial.

<&&= means the parent of the current director#? so t#ping
T cd ..
1ill take #ou one director# up the hierarch# <8ack to #our home director#=. Tr# it no1.
7oteK t#ping cd 1ith no argument al1a#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 1ork out 1here #ou are in relation to the 1hole file-s#stem. .or
e(ample? to find out the a8solute pathname of #our home-director#? t#pe cd to get 8ack to
#our home-director# and then t#pe
T p1d
The full pathname 1ill look something like this -
-a-!serv)-!serv)-!serv)EE-ee)engQQ-eeQ;a)
1hich means that ee)*a8 <#our home director#= is in the director# ee8eng)) <the group
director#=?1hich is located on the fser%8 file-ser%er.
7oteK
)&
-a-!serv)-!serv)-!serv)EE-ee)engQQ-eeQ;a)
can 8e shortened to
-ser-ee)engQQ-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 home-director#=
;&H *ore a)ot home directories and pathnames
Understanding pathnames
.irst t#pe cd to get 8ack to #our home-director#? then t#pe
T ls uni(stuff
to list the conents of #our uni(stuff director#.

7o1 t#pe
T ls 8ackups
You 1ill get a message like this -
8ackupsK 7o such file or director#
The reason is? )ackps is not in #our current 1orking director#. To use a command on a
file <or director#= not in the current 1orking 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
1ill list the contents of #our uni(stuff director#? no matter 1here #ou currentl# are in the
file s#stem.
0hat do #ou think
T ls >
1ould listB
0hat do #ou think
T ls >O..
1ould listB
Smmary
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 home-director#
cd ~
change to home-director#
cd ..
change to parent director#
pwd
displa# the path of the current director#
*++
Part T&o
E&; #opying 7iles
cp 0copy1
cp file1 file2 is the command 1hich makes a cop# of !ile; in the current 1orking
director# and calls it !ileE
0hat 1e are going to do no1? 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 U79; prompt? t#pe?
T cp O%olOe(amplesOtutorialOscience.t(t .
<7oteK !on@t forget the dot <.= at the end. Remem8er? in U79;? the dot means the current
director#.=
The a8o%e command means cop# the file science&t"t to the current director#? keeping the
name the same.
<7oteK The director# -vol-e"amples-ttorial- is an area to 1hich 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
ni"st!! director#.=

!"ercise 4a
Create a 8ackup of #our science&t"t file 8# cop#ing it to a file called science&)ak
E&E *oving !iles
m 0moe1
mv file1 file2 mo%es <or renames= !ile; to !ileE
*+*
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 1ith onl# one file rather than t1o.
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.
0e are no1 going to mo%e the file science.8ak to #our 8ackup director#.
.irst? change directories to #our uni(stuff director# <can #ou remem8er ho1B=. Then?
inside the ni"st!! director#? t#pe
T m% science.8ak 8ackupsO.
T#pe ls and ls backups to see if it has 1orked.
E&F 6emoving !iles and directories
rm 0remoe15 rmdir 0remoe directory1
To delete <remo%e= a file? use the rm command. $s an e(ample? 1e are going to create a
cop# of the science&t"t file then delete it.
9nside #our ni"st!! 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 )ackps director#. You 1ill not 8e a8le to since U79; 1ill not let #ou
remo%e a non-empt# director#.

!"ercise 4-
Create a director# called tempst!! using mkdir ? then remo%e it using the rmdir
command.
*+-
E&N %isplaying the contents o! a !ile on the screen
clear 0clear screen1
,efore #ou start the ne(t section? #ou ma# like to clear the terminal 1indo1 of the
pre%ious commands so the output of the follo1ing commands can 8e clearl# understood.
$t the prompt? t#pe
T clear
This 1ill clear all te(t and lea%e #ou 1ith the T prompt at the top of the 1indo1.

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 1indo1? so it scrolls past
making it unreada8le.

less
The command less 1rites 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 1ant to see another page? t#pe [q] if #ou 1ant to Euit
reading. $s #ou can see? less is used in preference to cat for long files.

head
The head command 1rites the first ten lines of a file to the screen.
.irst clear the screen then t#pe
T head science.t(t
*+/
Then t#pe
T head -5 science.t(t
0hat difference did the -5 do to the head commandB

tail
The tail command 1rites the last ten lines of a file to the screen.
Clear the screen and t#pe
T tail science.t(t
5o1 can #ou %ie1 the last *5 lines of the fileB

E&O Searching the contents o! a !ile
Simple searching using less
Using less? #ou can search though a te(t file for a ke#1ord <pattern=. .or e(ample? to
search through science&t"t for the 1ord @science@? t#pe
T less science.t(t
then? still in less <i.e. don@t press HEI to Euit=? t#pe a for1ard slash [/] follo1ed 8# the
1ord to search
Oscience
$s #ou can see? less finds and highlights the ke#1ord. T#pe [n] to search for the ne(t
occurrence of the 1ord.

grep 0don6t ask &hy it is called grep1
rep is one of man# standard U79; utilities. 9t searches files for specified 1ords or
patterns. .irst clear the screen? then t#pe
T grep science science.t(t
*+6
$s #ou can see? rep has printed out each line containg the 1ord science.
4r has itBBBB
Tr# t#ping
T grep Science science.t(t
The rep command is case sensiti%e\ it distinguishes 8et1een Science and science.
To ignore upperOlo1er case distinctions? use the -i option? i.e. t#pe
T grep -i 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 -i @spinning top@ science.t(t
Some of the other options of grep areK
-% displa# those lines that do 74T match
-n precede each maching line 1ith the line num8er
-c 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 1ithout the 1ords science or Science is
T grep -i%c science science.t(t

&c 0&ord count1
$ hand# little utilit# is the wc command? short for 1ord count. To do a 1ord count on
science&t"t? t#pe
T 1c -1 science.t(t
To find out ho1 man# lines the file has? t#pe
T 1c -l science.t(t
*+5
Smmary
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 fe1 lines of a file
tail file
displa# the last fe1 lines of a file
rep 'keyword' file
search a file for ke#1ords
wc file
count num8er of linesO1ordsOcharacters in file
Part Three
F&; 6edirection
2ost processes initiated 8# U79; commands 1rite to the standard output <that is? the#
1rite 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? 1here processes 1rite
their error messages? 8# default? to the terminal screen.
0e ha%e alread# seen one use of the cat command to 1rite the contents of a file to the
screen.
7o1 t#pe cat 1ithout specifing a file to read
T cat
Then t#pe a fe1 1ords on the ke#8oard and press the [!eturn] ke#.
.inall# hold the [Ctrl] ke# do1n and press [d] <1ritten as ]! for short= to end the
input.
*+6
0hat has happenedB
9f #ou run the cat command 1ithout 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 U79;? 1e can redirect 8oth the input and the output of commands.
F&E 6edirecting the 'tpt
0e use the V s#m8ol to redirect the output of a command. .or e(ample? to create a file
called list; 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=
0hat happens is the cat command reads the standard input <the ke#8oard= and the V
redirects the output? 1hich normall# goes to the screen? into a file called list;
To read the contents of the file? t#pe
T cat list*
!"ercise 7a
Using the a8o%e method? create another file called listE containing the follo1ing fruitK
orange? plum? mango? grapefruit. Read the contents of listE
The form VV appends standard output to a file. So to add more items to the file list;? 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 no1 ha%e t1o files. 4ne contains si( fruit? the other contains four fruit. 0e
1ill no1 use the cat command to Noin <concatenate= list; and listE into a ne1 file called
)iglist. T#pe
T cat list* list- V 8iglist
0hat this is doing is reading the contents of list; and listE in turn? then outputing the te(t
to the file )iglist
To read the contents of the ne1 file? t#pe
T cat 8iglist
F&F 6edirecting the <npt
0e 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 1ill 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 1ill 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 slist
F&N +ipes
To see 1ho is on the s#stem 1ith #ou? t#pe
T 1ho
4ne method to get a sorted list of names is to t#pe?
T 1ho V names.t(t
T sort Z names.t(t
This is a 8it slo1 and #ou ha%e to remem8er to remo%e the temporar# file called names
1hen #ou ha%e finished. 0hat #ou reall# 1ant to do is connect the output of the who
command directl# to the input of the sort command. This is e(actl# 1hat pipes do. The
s#m8ol for a pipe is the %ertical 8ar W
.or e(ample? t#ping
T 1ho W sort
1ill gi%e the same result as a8o%e? 8ut Euicker and cleaner.
To find out ho1 man# users are logged on? t#pe
T 1ho W 1c -l
!"ercise 7-
a"ps -#hockney textfile is the command to print a postscript file to the printer
hockne#.
Using pipes? print all lines of list; and listE containing the letter @p@? sort the result? and
print to the printer hockne#.
*+)
Smmary
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&; Rildcards
The characters 8 and 9
The character A is called a 1ildcard? and 1ill match against none or more character<s= in
a file <or director#= name. .or e(ample? in #our ni"st!! director#? t#pe
T ls listA
This 1ill list all files in the current director# starting 1ith list&&&&
Tr# t#ping
T ls Alist
This 1ill list all files in the current director# ending 1ith &&&&list
The character B 1ill match e(actl# one character.
So ls 'ouse 1ill match files like hose and mose? 8ut not grose.
Tr# t#ping
T ls Blist
**+
N&E 7ilename conventions
0e 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 1ith special meanings such as - I / B ? should 8e a%oided.
$lso? a%oid using spaces 1ithin names. The safest 1a# to name a file is to use onl#
alphanumeric characters? that is? letters and num8ers? together 1ith ^ <underscore= and .
<dot=.
.ile names con%entionall# start 1ith a lo1er-case letter? and ma# end 1ith a dot follo1ed
8# a group of letters indicating the contents of the file. .or e(ample? all files consisting of
C code ma# 8e named 1ith 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&ot. Should #ou forget to use that option? #ou are ad%ised to rename the
compiled file immediatel#? other1ise the ne(t such file 1ill o%er1rite it and it 1ill 8e
lost.
N&F :etting Help
*n'line (anuals
There are on-line manuals 1hich gi%es information a8out most commands. The manual
pages tell #ou 1hich options a particular command can take? and ho1 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 <1ord count= command? t#pe
T man 1c
$lternati%el#
T 1hatis 1c
gi%es a one-line description of the command? 8ut omits an# information a8out options
etc.
***
Apropos
0hen #ou are not sure of the e(act name of a command?
T apropos ke#1ord
1ill gi%e #ou the commands 1ith ke#1ord in their manual page header. .or e(ample? tr#
t#ping
T apropos cop#
Smmary
(
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 1ith ke#1ord in their man pages
Part $ie
O&; 7ile system secrity Saccess rightsT
9n #our uni(stuff director#? t#pe
T ls -l <l for long listingY=
You 1ill see that #ou no1 get lots of details a8out the contents of #our director#? similar
to the e(ample 8elo1.
**-
'ach file <and director#= has associated access rights? 1hich ma# 8e found 8# t#ping ls
-l. $lso? ls -l gi%es additional information as to 1hich group o1ns the file <8eng)5
in the follo1ing e(ample=K
-r1(r1-r-- * ee5*a8 8eng)5 -65+ Sept-) **K5- file*
9n the left-hand column is a *+ s#m8ol string consisting of the s#m8ols d? r? 1? (? -? and?
occasionall#? s or S. 9f d is present? it 1ill 8e at the left hand end of the string? and
indicates a director#K other1ise - 1ill 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 /.
The left group of / gi%es the file permissions for the user that o1ns 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 1hom 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? 1? etc.? ha%e slightl# different meanings depending on 1hether the# refer
to a simple file or to a director#.
Access rights on files:
r <or -=? indicates read permission <or other1ise=? that is? the presence or a8sence
of permission to read and cop# the file
1 <or -=? indicates 1rite permission <or other1ise=? that is? the permission <or
other1ise= to change a file
( <or -=? indicates e(ecution permission <or other1ise=? that is? the permission to
e(ecute a file? 1here appropriate
**/
Access rights on directories:
r allo1s users to list files in the director#\
1 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.
**6
Some e"amples
-rwxrwxrwx
a file that e%er#one can read? 1rite and e(ecute <and delete=.
-rw-------
a file that onl# the o1ner can read and 1rite - no-one else
can read or 1rite and no-one has e(ecution rights <e.g. #our
mail8o( file=.
O&E #hanging access rights
chmod 0changing a file mode1
4nl# the o1ner of a file can use chmod to change the permissions of a file. The options
of chmod are as follo1s
Sym)ol *eaning
u user
g group
o other
a all
r read
1 1rite <and delete=
( e(ecute <and access director#=
: add permission
- take a1a# permission
.or e(ample? to remo%e read 1rite and e(ecute permissions on the file )iglist for the
group and others? t#pe
T chmod go-r1( 8iglist
This 1ill lea%e the other permissions unaffected.
To gi%e read and 1rite permissions on the file )iglist to all?
T chmod a:r1 8iglist
**5
!"ercise ;a
Tr# changing access permissions on the file science&t"t and on the director# )ackps
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? 1ith 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 U79; 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 U79; prompt is returned immediatel#? and other tasks can
8e carried out 1hile 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 1aits a gi%en num8er of seconds 8efore continuing. T#pe
T sleep *+
This 1ill 1ait *+ seconds 8efore returning the command prompt T. Until the command
prompt is returned? #ou can do nothing e(cept 1ait.
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 a1a#? allo1ing #ou
do run other programs 1hile 1aiting 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 1ith a P9! and is notified 1hen
a 8ackground process is finished. ,ackgrounding is useful for No8s 1hich 1ill take a long
time to complete.
**6
#ackgrounding a current foreground process
$t the prompt? t#pe
T sleep *++
You can suspend the process running in the foreground 8# holding do1n the [control]
ke# and t#ping [)] <1ritten as V2= Then to put it in the 8ackground? t#pe
T 8g
7oteK do not 8ackground programs that reEuire user interaction e.g. pine
O&N Listing sspended and )ackgrond processes
0hen a process is running? 8ackgrounded or suspended? it 1ill 8e entered onto a list
along 1ith 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
H/I 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 1ith no No8 num8er foregrounds the last suspended process.
O&O Milling a process
kill 0terminate or signal a process1
9t is sometimes necessar# to kill a process <for e(ample? 1hen 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 6? t#pe
T kill T6
To check 1hether this has 1orked? 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 <P9!s= and using
kill PID_number
T sleep *++ X
T ps
P9! TT S T92' C422$7!
-++"" ptsO5 S +K+5 sleep *++
-*56/ ptsO5 T +K++ netscape
-*&"/ 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 -) -++""
7oteK 9t is not possi8le to kill off other users@ processes YYY
Smmary
**&
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 se!l 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 ho1 much of it #ou ha%e used? t#pe
T Euota -%
df
The df command reports on the space left on the file s#stem. .or e(ample? to find out
ho1 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 1ant to find out 1hich director# has the most files. 9n
#our home-director#? t#pe
T du
compress
This reduces the siFe of a file? thus freeing %alua8le disk space. .or e(ample? t#pe
T ls -l science.t(t
and note the siFe of the file. Then to compress science.t(t? t#pe
T compress science.t(t
This 1ill compress the file and place it in a file called science.t(t.U
To see the change in siFe? t#pe ls -l 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 1ill 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 1as entered.
T histor# <sho1 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 Y-/ <recall third most recent command=
T Y5 <recall 5th command in list=
T Ygrep <recall last command starting 1ith grep=
You can increase the siFe of the histor# 8uffer 8# t#ping
T set histor#`*++
Part Seen
W&; #ompiling 9><P so!tware packages
0e ha%e man# pu8lic domain and commercial soft1are packages installed on our
s#stems? 1hich are a%aila8le to all users. 5o1e%er? students are allo1ed to do1nload and
install small soft1are packages in their o1n home director#? soft1are usuall# onl# useful
to them personall#.
There are a num8er of steps needed to install the soft1are.
ocate and do1nload the source code <1hich 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 high-le%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 lo1er-le%el language called
assem8l# language. The assem8l# language code made 8# the pre%ious stage is then
con%erted into o8Nect code 1hich are fragments of code 1hich the computer understands
directl#. The final stage in compiling a program in%ol%es linking the o8Nect code to code
li8raries 1hich contain certain 8uilt-in 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 end-users
to simplif# these steps.
make and the (akefile
The make command allo1s programmers to manage large programs or groups of
programs. 9t aids in de%eloping large programs 8# keeping track of 1hich portions of the
entire program ha%e 8een changed? compiling onl# those parts of the program 1hich ha%e
changed since the last compile.
The make program gets its set of compile rules from a te(t file called *ake!ile 1hich
resides in the same director# as the source files. 9t contains information on ho1 to
compile the soft1are? e.g. the optimisation le%el? 1hether to include de8ugging info in
the e(ecuta8le. 9t also contains information on 1here 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. 5o1e%er? man# packages are no1 8eing distri8uted
1ith the 37U configure utilit#.
configure
$s the num8er of U79; %ariants increased? it 8ecame harder to 1rite programs 1hich
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 37U configure
and 8uild s#stem simplifies the 8uilding of programs distri8uted as source code. $ll
programs are 8uilt using a simple? standardised? t1o 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#stem-
dependent %aria8les used during compilation. 9t uses those %alues to create a *ake!ile in
each director# of the package.
The simplest 1a# to compile a package isK
*--
*. cd to the director# containing the package@s source code.
-. T#pe ./confiure to configure the package for #our s#stem.
/. T#pe make to compile the package.
6. 4ptionall#? t#pe make check to run an# self-tests that come 1ith 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 1ide %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 1ill hold machine independent files
such as documentation? data and configuration files.
The director# named 8# the --exec-prefix option? <1hich is normall# a
su8director# of the --prefi( director#=? 1ill hold machine dependent files such as
e(ecuta8les.
W&E %ownloading sorce code
.or this e(ample? 1e 1ill do1nload a piece of free soft1are that con%erts 8et1een
different units of measurements.
.irst create a do1nload director#
T mkdir do1nload
!o1nload the soft1are here and sa%e it to #our ne1 do1nload director#.
W&F E"tracting the sorce code
3o into #our download director# and list the contents.
T cd do1nload
T ls -l
$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=.
*-/
.irst unFip the file using the un)ip command. This 1ill create a .tar file.
T gunFip units-*."6.tar.gF
Then e(tract the contents of the tar file.
T tar -(%f units-*."6.tar
$gain? list the contents of the download director#? then go to the nits-;&WN su8-
director#.
T cd units-*."6
W&N #on!igring and creating the *ake!ile
The first thing to do is carefull# read the 6E(%*E and <>ST(LL te(t files <use the
less command=. These contain important information on ho1 to compile and run the
soft1are.
The units package uses the 37U configure s#stem to compile the source code. 0e 1ill
need to specif# the installation director#? since the default 1ill 8e the main s#stem area
1hich #ou 1ill not ha%e 1rite permissions for. 0e need to create an install director# in
#our home director#.
T mkdir >Ounits*"6
Then run the configure utilit# setting the installation path to this.
T .Oconfigure --prefi(`L542'Ounits*"6
74T'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 sho1 the contents of this %aria8le. 0e 1ill learn more a8out en%ironment %aria8les in a
later chapter.
9f configure has run correctl#? it 1ill ha%e created a 2akefile 1ith all necessar# options.
You can %ie1 the 2akefile if #ou 1ish <use the less command=? 8ut do not edit the
contents of this.
W&O 8ilding the package
*-6
7o1 #ou can go ahead and 8uild the package 8# running the make command.
T make
$fter a minute or t1o <depending on the speed of the computer=? the e(ecuta8les 1ill 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 no1 install the package.
T make install
This 1ill install the files into the D-nits;WN director# #ou created earlier.
W&H 6nning the so!tware
You are no1 read# to run the soft1are <assuming e%er#thing 1orked=.
T cd >Ounits*"6
9f #ou list the contents of the units director#? #ou 1ill see a num8er of su8directories.
8in The 8inar# e(ecuta8les
info 37U 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 1antK metres
A *.&-&&
9f #ou get the ans1er *.&-&&? congratulations? it 1orked.
*-5
To %ie1 1hat units it can con%ert 8et1een? %ie1 the data file in the share director# <the
list is Euite comprehensi%e=.
To read the full documentation? change into the in!o director# and t#pe
T info --file`units.info
W&W Stripping nnecessary code
0hen a piece of soft1are is 8eing de%eloped? it is useful for the programmer to include
de8ugging information into the resulting e(ecuta8le. This 1a#? if there are pro8lems
encountered 1hen running the e(ecuta8le? the programmer can load the e(ecuta8le into a
de8ugging soft1are package and track do1n an# soft1are 8ugs.
This is useful for the programmer? 8ut unnecessar# for the user. 0e can assume that the
package? once finished and a%aila8le for do1nload has alread# 8een tested and de8ugged.
5o1e%er? 1hen 1e compiled the soft1are a8o%e? de8ugging information 1as still
compiled into the final e(ecuta8le. Since it is unlike# that 1e are going to need this
de8ugging information? 1e can strip it out of the final e(ecuta8le. 4ne of the ad%antages
of this is a much smaller e(ecuta8le? 1hich should run slightl# faster.
0hat 1e 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*"6O8in
T ls -l
$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 '. /--8it S, e(ecuta8le? 9ntel &+/&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 -l
$s #ou can see? the file is no1 /6 k8#tes - a third of its original siFe. T1o thirds of the
8inar# file 1as de8ug code YYY
*-6
Check the file information again.
T file units
unitsK '. /--8it S, e(ecuta8le? 9ntel &+/&6? %ersion *? d#namicall# linked <uses
shared li8s=? stripped
597TK You can use the make command to install pre-stripped copies of all the 8inar# files
1hen #ou install the package.
9nstead of t#ping make install? simpl# t#pe make install-strip
Part !ight
G&; 9><P Jaria)les
Varia8les are a 1a# of passing information from the shell to programs 1hen #ou run
them. Programs look _in the en%ironment_ for particular %aria8les and if the# are found
1ill 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 U79; %aria8les are split into t1o 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 short-term 1orking 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 lo1er
case names.
G&E Environment Jaria)les
$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=
!9SP$Y <the name of the computer screen to displa# ; 1indo1s=
PR97T'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:
'7V9R472'7T %aria8les are set using the setenv command? displa#ed using the
printenv or env commands? and unset using the unsetenv command.
To sho1 all %alues of these %aria8les? t#pe
T printen% W less
G&F Shell Jaria)les
$n e(ample of a shell %aria8le is the histor# %aria8le. The %alue of this is ho1 man# shell
commands to sa%e? allo1 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
c1d <#our current 1orking 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' %aria8les are 8oth set and displa#ed using the set command. The# can 8e unset
8# using the unset command.
To sho1 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? ho1e%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. 5o1e%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
al1a#s represent the same director# list? and altering either automaticall# causes the other
to 8e changed.
G&N 9sing and setting varia)les
'ach time #ou login to a U79; host? the s#stem looks in #our home director# for
initialisation files. 9nformation in these files is used to set up #our 1orking en%ironment.
The C and TC shells uses t1o files called .login and .cshrc <note that 8oth file names
8egin 1ith a dot=.
$t login the C shell first reads &cshrc follo1ed 8# &login
&login is to set conditions 1hich 1ill appl# to the 1hole 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 '7V9R472'7T %aria8les in the &login file and S5'
%aria8les in the &cshrc file.
R(6><>:3 7'V'R put commands that run graphical displa#s <e.g. a 1e8 8ro1ser= in
#our .cshrc or .login file.
G&O Setting shell varia)les in the &cshrc !ile
.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
1ish.
T set histor# ` -++
Check this has 1orked 8# t#ping
T echo Lhistor#
5o1e%er? this has onl# set the %aria8le for the lifetime of the current shell. 9f #ou open a
ne1 (term 1indo1? it 1ill onl# ha%e the default histor# %alue set. To P'R2$7'7TY
set the %alue of histor#? #ou 1ill need to add the set command to the .cshrc file.
.irst open the &cshrc file in a te(t editor. $n eas#? user-friendl# editor to use is nedit.
T nedit >O.cshrc
*-)
$dd the follo1ing 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 1orked 8# t#ping
T echo Lhistor#
G&H Setting the path
0hen #ou t#pe a command? #our path <or P$T5= %aria8le defines in 1hich directories the
shell 1ill 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-nits;WN-)in-nits=? or #ou need to ha%e the director# D-nits;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*"6O8in=
Test that this 1orked 8# tr#ing to run units in an# director# other that 1here units is
actuall# located.
T cd\ units
H<>T3 You can run multiple commands on one line 8# separating them 1ith a semicolon.
To add this path P'R2$7'7TY? add the follo1ing line to #our .cshrc $.T'R the list
of other commands.
set path ` <Lpath >Ounits*"6O8in=
*/+
Uni" ' $re<uently Asked >uestions 021 ?$re<uent
posting@
These articles are di%ided appro(imatel# as follo1sK
*.A= 3eneral Euestions.
-.A= Relati%el# 8asic Euestions? likel# to 8e asked 8# 8eginners.
/.A= 9ntermediate Euestions.
6.A= $d%anced Euestions? likel# to 8e asked 8# people 1ho thought
the# alread# kne1 all of the ans1ers.
5.A= Guestions pertaining to the %arious shells? and the differences.
This article includes ans1ers toK
*.*= 0ho helped #ou put this list togetherB
*.-= 0hen someone refers to [rn<*=J or [ctime</=J? 1hat does the num8er in parentheses
meanB
*./= 0hat does dsome strange uni( command namee stand forB
*.6= 5o1 does the gate1a# 8et1een Rcomp.uni(.EuestionsS and the Rinfo-uni(S mailing list
1orkB
*.5= 0hat are some useful Uni( or C 8ooksB
*.6= 0hat happened to the pronunciation list that used to 8e part of this documentB
9f #ouJre looking for the ans1er to? sa#? Euestion *.5? and 1ant to skip e%er#thing else? #ou can
search ahead for the regular e(pression R]*.5=S.
0hile these are all legitimate Euestions? the# seem to crop up in comp.uni(.Euestions or
comp.uni(.shell on an annual 8asis? usuall# follo1ed 8# plent# of replies <onl# some of 1hich are
correct= and then a period of griping a8out ho1 the same Euestions keep coming up. You ma#
also like to read the monthl# article R$ns1ers to .reEuentl# $sked GuestionsS in the ne1sgroup
Rne1s.announce.ne1usersS? 1hich 1ill tell #ou 1hat RU79;S stands for.
0ith the %ariet# of Uni( s#stems in the 1orld? itJs hard to guarantee that these ans1ers 1ill 1ork
e%er#1here. Read #our local manual pages 8efore tr#ing an#thing suggested here. 9f #ou ha%e
suggestions or corrections for an# of these ans1ers? please send them to to tmatimaraisgtec.com.
*.-= 0hen someone refers to [rn<*=J or [ctime</=J? 1hat 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 1here the appropriate documentation can 8e found. You could t#pe Rman /
ctimeS to look up the manual page for RctimeS in section / of the manual.
The traditional manual sections areK
*/*
* User-le%el commands
- S#stem calls
/ i8rar# functions
6 !e%ices and de%ice dri%ers
5 .ile formats
6 3ames
" Various miscellaneous stuff - macro packages etc.
& S#stem maintenance and operation commands
Some Uni( %ersions use non-numeric section names. .or instance? ;eni( uses RCS for
commands and RSS for functions. Some ne1er %ersions of Uni( reEuire Rman -sM titleS
instead of Rman M titleS.
'ach section has an introduction? 1hich #ou can read 1ith Rman M introS 1here M is the
section num8er.
Sometimes the num8er is necessar# to differentiate 8et1een 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</=S? a manual page a8out
the [timeJ su8routine for determining the current time. You can use Rman * timeS or Rman /
timeS to specif# 1hich RtimeS man page #ouJre interested in.
YouJll often find other sections for local programs or e%en su8sections of the sections a8o%e -
Ultri( has sections /m? /n? /( and /#p among others.
*./= 0hat does dsome strange uni( command namee stand forB
a1k ` R$ho 0ein8erger and DernighanS
This language 1as named 8# its authors? $l $ho? Peter 0ein8erger and ,rian Dernighan.
grep ` R3lo8al Regular '(pression PrintS
grep comes from the ed command to print all lines matching a
certain pattern
gOreOp
1here 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 - in fact? Rfgrep foo8ar
A.cS is usuall# slo1er than Regrep foo8ar A.cS <Yes? this is kind of surprising. Tr# it.=
.grep still has its uses though? and ma# 8e useful 1hen 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
catenate is an o8scure 1ord meaning Rto connect in a seriesS? 1hich is 1hat the RcatS
command does to one or more files. 7ot to 8e confused 1ith CO$OT? the Computer $ided
T#pesetter.
gecos ` R3eneral 'lectric Comprehensi%e 4perating Super%isorS
0hen 3'Js large s#stems di%ision 1as sold to 5one#1ell? 5one#1ell dropped the R'S from
R3'C4SS.
Uni(Js pass1ord file has a Rp1^gecosS field. The name is a real holdo%er from the earl#
da#s. !ennis Ritchie has reportedK
RSometimes 1e sent printer output or 8atch No8s to the 3C4S machine. The gcos field in the
pass1ord file 1as a place to stash the information for the L9!'7T card. 7ot elegant.S
nroff ` R7e1 R4..S
troff ` RT#pesetter ne1 R4..S

These are descendants of RroffS? 1hich 1as a re-implementation 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 T-shaped pipe splitter.
8ss ` R,lock Started 8# S#m8olS
!ennis Ritchie sa#sK
$ctuall# the acron#m <in the sense 1e took it up\ it ma# ha%e other credi8le et#mologies= is
R,lock Started 8# S#m8ol.S 9t 1as a pseudo-op in .$P <.ortran $ssem8l# H-erBI Program=?
an assem8ler for the 9,2 "+6-"+)-"+)+-"+)6 machines. 9t defined its la8el and set aside
space for a gi%en num8er of 1ords. There 1as another pseudo-op? ,'S? R,lock 'nded 8#
S#m8olS that did the same e(cept that the la8el 1as defined 8# the last assigned 1ord : *.
<4n these machines .ortran arra#s 1ere stored 8ack1ards in storage and 1ere *-origin.=
The usage is reasona8l# appropriate? 8ecause Nust as 1ith standard Uni( loaders? the space
assigned didnJt ha%e to 8e punched literall# into the o8Nect deck 8ut 1as represented 8# a
count some1here.
8iff ` R,9..S
This command? 1hich turns on as#nchronous mail notification? 1as actuall# named after a
dog at ,erkele#.
9 can confirm the origin of 8iff? if #ouJre interested. ,iff 1as 5eidi StettnerJs dog? 8ack 1hen
5eidi <and 9? and ,ill Co#= 1ere all grad students at U.C. ,erkele# and the earl# %ersions of
,S! 1ere 8eing de%eloped. ,iff 1as popular among the residents of '%ans 5all? and 1as
kno1n 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 1as a facilit# that 1ould e(ecute a 8unch of commands stored in a file\ it 1as 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
,rian Dernighan X !ennis Ritchie? as told to Vicki ,ro1n
RrcS is also the name of the shell from the ne1 Plan ) operating s#stem.
Perl ` RPractical '(traction and Report anguageS
Perl ` RPathologicall# 'clectic Ru88ish isterS
The Perl language is arr# 0allJs highl# popular
freel#-a%aila8le completel# porta8le te(t? process? and file
manipulation tool that 8ridges the gap 8et1een shell and C
programming <or 8et1een doing it on the command line and
pulling #our hair out=. .or further information? see the
Usenet ne1sgroup comp.lang.perl.misc.
!on i8esJ 8ook Rife 1ith Uni(S contains lots more of these tid8its.
*.6= 5o1 does the gate1a# 8et1een Rcomp.uni(.EuestionsS and the
Rinfo-uni(S mailing list 1orkB
Rinfo-uni(S and Runi(-1iFardsS are mailing list %ersions of comp.uni(.Euestions and
comp.uni(.1iFards respecti%el#. There should 8e no difference in content 8et1een the
mailing list and the ne1sgroup.
To get on or off either of these lists? send mail to
info-uni(-reEuesta8rl.mil or uni(-1iFards-reEuesta8rl.mil.
,e sure to use the [-ReEuestJ. !onJt e(pect an immediate response.
5ere are the gor# details? courtes# of the listJs maintainer? ,o8 Reschl#.
```` postings to info-U79; and U79;-1iFards lists ````
$n#thing su8mitted to the list is posted\ 9 do not moderate incoming trafficP,R functions
as a reflector. Postings su8mitted 8# 9nternet su8scri8ers should 8e addressed to the list
address <info-U79; or U79;- 1iFards=\ the [-reEuestJ addresses are for correspondence 1ith
the list maintainer HmeI. Postings su8mitted 8# US'7'T readers should 8e addressed to the
appropriate ne1s group <comp.uni(.Euestions or comp.uni(.1iFards=.
.or 9nternet su8scri8ers? recei%ed traffic 1ill 8e of t1o t#pes\ indi%idual messages? and
digests. Traffic 1hich comes to ,R from the 9nternet and ,9T7'T <%ia the ,9T7'T-
9nternet gate1a#= is immediatel# resent to all addressees on the mailing list. Traffic
originating on US'7'T is gathered up into digests 1hich are sent to all list mem8ers dail#.
,9T7'T traffic is much like 9nternet traffic. The main difference is that 9 maintain onl# one
address for traffic destined to all ,9T7'T su8scri8ers. That address points to a list e(ploder
1hich then sends copies to indi%idual ,9T7'T su8scri8ers. This 1a# onl# one cop# of a
gi%en message has to cross the ,9T7'T-9nternet gate1a# in either direction.
US'7'T su8scri8ers see onl# indi%idual messages. $ll messages originating on the 9nternet
side are for1arded to our US'7'T machine. The# are then posted to the appropriate
ne1sgroup. Unfortunatel#? for gate1a#ed messages? the sender 8ecomes Rne1sa8rl-admS.
This is currentl# an una%oida8le side-effect of the soft1are 1hich performs the gate1a#
function.
*/6
$s for readership? US'7'T has an e(tremel# large readership - 9 1ould guess se%eral
thousand hosts and tens of thousands of readers. The master list maintained here at ,R runs
a8out t1o hundred fift# entries 1ith roughl# ten percent of those 8eing local redistri8ution
lists. 9 donJt ha%e a good feel for the siFe of the ,9T7'T redistri8ution? 8ut 9 1ould guess it
is roughl# the same siFe and composition as the master list. Traffic runs *5+D to 6++D 8#tes
per list per 1eek on a%erage.
*.5= 0hat are some useful Uni( or C 8ooksB
2itch 0right <mitchacirrus.com= maintains a useful list of Uni( and C 8ooks? 1ith
descriptions and some mini-re%ie1s. 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+.*/.*=? 1here
itJs Rpu8OmitchOY$,O#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 some1hat shorter. This list is also a classified list? 1ith
8ooks grouped into categories? 1hich 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? 1here itJs
Rpu8OusenetOne1s.ans1ersO8ooksOuni(S. Send additions or suggestions to kkoasfu.ca.
9f #ou canJt use anon#mous ftp? email the line RhelpS to Rftpmailadec1rl.dec.comS for
instructions on retrie%ing things %ia email.
*.6= 0hat 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 itmaath
<thanks? 2aartenY=. 9t 1as originall# created 8# Carl Paukstis
Zcarlpafrigg.isc-8r.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 1idel#-distri8uted RCargonS file <maintained 8# 'ric S. Ra#mond?
ericasnark.th#rsus.com= 1hich seems like a much more appropriate forum for the topic of
R5o1 do #ou pronounce OA BS
9f #ouJd like a cop#? #ou can ftp one from ftp.1g.omron.co.Np <*//.-*+.6.6=? itJs Rpu8Ouni(-
faEOdocsOPronunciation-3uideS.
*/5
Uni" ' $re<uently Asked >uestions 041 ?$re<uent
posting@
This article includes ans1ers toK
-.*= 5o1 do 9 remo%e a file 1hose name 8egins 1ith a R-R B
-.-= 5o1 do 9 remo%e a file 1ith funn# characters in the filename B
-./= 5o1 do 9 get a recursi%e director# listingB
-.6= 5o1 do 9 get the current director# into m# promptB
-.5= 5o1 do 9 read characters from the terminal in a shell scriptB
-.6= 5o1 do 9 rename RA.fooS to RA.8arS? or change file names to lo1ercaseB
-."= 0h# do 9 get Hsome strange error messageI 1hen 9 Rrsh host commandS B
-.&= 5o1 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
-.)= 5o1 do 9 redirect stdout and stderr separatel# in cshB
-.*+= 5o1 do 9 tell inside .cshrc if 9Jm a login shellB
-.**= 5o1 do 9 construct a shell glo8-pattern that matches all files e(cept R.S and R..S B
-.*-= 5o1 do 9 find the last argument in a ,ourne shell scriptB
-.*/= 0hatJs 1rong 1ith ha%ing [.J in #our LP$T5 B
-.*6= 5o1 do 9 ring the terminal 8ell during a shell scriptB
-.*5= 0h# canJt 9 use RtalkS to talk 1ith m# friend on machine ;B
-.*6= 0h# does calendar produce the 1rong outputB
9f #ouJre looking for the ans1er to? sa#? Euestion -.5? and 1ant to skip e%er#thing else? #ou can
search ahead for the regular e(pression R]-.5=S.
0hile these are all legitimate Euestions? the# seem to crop up in comp.uni(.Euestions or
comp.uni(.shell on an annual 8asis? usuall# follo1ed 8# plent# of replies <onl# some of 1hich are
correct= and then a period of griping a8out ho1 the same Euestions keep coming up. You ma#
also like to read the monthl# article R$ns1ers to .reEuentl# $sked GuestionsS in the ne1sgroup
Rne1s.announce.ne1usersS? 1hich 1ill tell #ou 1hat RU79;S stands for.
0ith the %ariet# of Uni( s#stems in the 1orld? itJs hard to guarantee that these ans1ers 1ill 1ork
e%er#1here. Read #our local manual pages 8efore tr#ing an#thing suggested here. 9f #ou ha%e
suggestions or corrections for an# of these ans1ers? please send them to to tmatimaraisgtec.com.
-.*= 5o1 do 9 remo%e a file 1hose name 8egins 1ith a R-R B
.igure out some 1a# to name the file so that it doesnJt 8egin
1ith a dash. The simplest ans1er is to use
*/6
rm .O-filename
<assuming R-filenameS is in the current director#? of course.= This method of a%oiding the
interpretation of the R-R 1orks 1ith other commands too.
2an# commands? particularl# those that ha%e 8een 1ritten to use the Rgetopt</=S argument
parsing routine? accept a RPR argument 1hich means Rthis is the last option? an#thing after
this is not an optionS? so #our %ersion of rm might handle Rrm -- -filenameS. Some %ersions
of rm that donJt use getopt<= treat a single R-R in the same 1a#? so #ou can also tr# Rrm -
-filenameS.
-.-= 5o1 do 9 remo%e a file 1ith funn# characters in the filename B
9f the [funn# characterJ is a [OJ? skip to the last part of this ans1er. 9f the funn# character is
something else? such as a [ [ or control character or character 1ith the &
th
8it set? keep reading.
The classic ans1ers are
rm -i someApatternAthatAmatchesAonl#AtheAfileA#ouA1ant
1hich asks #ou 1hether #ou 1ant to remo%e each file matching the indicated pattern\
depending on #our shell? this ma# not 1ork if the filename has a character 1ith the &
th
8it set
<the shell ma# strip that off=\
and
rm -ri .
1hich asks #ou 1hether to remo%e each file in the director#.
$ns1er R#S to the pro8lem file and RnS to e%er#thing else. Unfortunatel# this doesnJt 1ork
1ith man# %ersions of rm. $lso unfortunatel#? this 1ill 1alk through e%er# su8director# of
R.S? so #ou might 1ant to Rchmod a-(S those directories temporaril# to make them
unsearcha8le.
$l1a#s take a deep 8reath and think a8out 1hat #ouJre doing and dou8le check 1hat #ou
t#ped 1hen #ou use rmJs R-rS flag or a 1ildcard on the command line\
and
find . -t#pe f ... -ok rm [deJ f\
1here 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 -i .S= and then use
find . -inum *-/65 -ok rm [deJ f\
or
find . -inum *-/65 -ok m% [deJ ne1-file-name f\
R-okS is a safet# check - it 1ill prompt #ou for confirmation of the command itJs a8out to
e(ecute. You can use R-e(ecS instead to a%oid the prompting? if #ou 1ant to li%e dangerousl#?
or if #ou suspect that the filename ma# contain a funn# character seEuence that 1ill mess up
#our screen 1hen printed.
0hat 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 7.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# 1h# this pro8lem is so
strange.
Recall that Uni( directories are simpl# pairs of filenames and inode num8ers. $ director#
essentiall# contains information like thisK
filename inode
file* *-/65
file-.c *-/6)
file/ *-/6"
Theoreticall#? [OJ and [f+J are the onl# t1o characters that cannot appear in a filename - [OJ
8ecause itJs used to separate directories and files? and [f+J 8ecause it terminates a filename.
Unfortunatel# some implementations of 7.S 1ill 8lithel# create filenames 1ith em8edded
slashes in response to reEuests from remote machines. .or instance? this could happen 1hen
someone on a 2ac or other non-Uni( machine decides to create a remote 7.S file on #our
Uni( machine 1ith the date in the filename. Your Uni( director# then has this in itK
filename inode
)*O+-O+" *-/5"
7o amount of messing around 1ith [findJ or [rmJ as descri8ed a8o%e 1ill delete this file?
since those utilities and all other Uni( programs? are forced to interpret the [OJ in the normal
1a#.
$n# ordinar# program 1ill e%entuall# tr# to do unlink<R)*O+-O+"S=? 1hich as far as the kernel
is concerned means Runlink the file +" in the su8director# +- of director# )*S? 8ut thatJs not
1hat 1e ha%e - 1e ha%e a 7<LE named R)*O+-O+"S in the current director#. This is a su8tle
8ut crucial distinction.
0hat 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 7.S daemon to rename the file
to something 1ithout slashes.
9f that doesnJt 1ork or isnJt possi8le? #ouJll need help from #our s#stem manager? 1ho 1ill
ha%e to tr# the one of the follo1ing. Use Rls -iS 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 1ith
#our fingers crossed. This destro#s the information in the file. 9f #ou 1ant to keep it? #ou
can tr#K
create a ne1 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 1ith the 8ad name= from the old
director# to the ne1 one\
do Rls -idS on the director# containing the file 1ith the 8ad name to get its inum8er\
umount the file s#stem\
RclriS the director# containing the file 1ith 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 8lo1n a1a# 8# RfsckS=
mo%e the file out of Rlost:foundS into the director# 1ith a 8etter name.
$lternati%el#? #ou can patch the director# the hard 1a# 8# cra1ling around in the ra1 file
s#stem. Use Rfsd8S? if #ou ha%e it.
-./= 5o1 do 9 get a recursi%e director# listingB
4ne of the follo1ing ma# do 1hat #ou 1antK
ls -R <not all %ersions of RlsS ha%e -R=
find . -print <should 1ork e%er#1here=
du -a . <sho1s #ou 8oth the name and siFe=
9f #ouJre looking for a 1ildcard pattern that 1ill match all R.cS
files in this director# and 8elo1? #ou 1onJt find one? 8ut #ou
can use
T some-command [find . -name [A.cJ -print[
RfindS is a po1erful program. earn a8out it.
-.6= 5o1 do 9 get the current director# into m# promptB
9t depends 1hich shell #ou are using. 9tJs eas# 1ith some shells? hard or impossi8le 1ith
others.
C Shell <csh=K
Put this in #our .cshrc - customiFe the prompt %aria8le the 1a# #ou 1ant.
alias setprompt [set prompt`SLdc1deT 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 Lc1d %aria8le - #ou can use [p1d[ instead.
9f #ou Nust 1ant the last component of the current director#
in #our prompt <RmailT R instead of ROusrOspoolOmailT R=
#ou can use
alias setprompt [set prompt`SLc1dKtT 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 s1itch XX and WW <and get a 8etter %ersion of csh.=
,ourne Shell <sh=K
9f #ou ha%e a ne1er %ersion of the ,ourne Shell <SVR- or ne1er= #ou can use a shell
function to make #our o1n command? R(cdS sa#K
(cd<= d cd LA \ PS*`S[p1d[ L R\ e
9f #ou ha%e an older ,ourne shell? itJs complicated 8ut not impossi8le. 5ereJs one 1a#. $dd
this to #our .profile fileK
4397^S5'`LL e(port 4397^S5'
C2!.9'`OtmpOcd.LL e(port C2!.9'
M *6 is S93UR3? pick a signal thatJs not likel# to 8e used
PR42PTS93`*6 e(port PR42PTS93
trap [. LC2!.9'J LPR42PTS93
and then put this e(ecuta8le script <1ithout the indentationY=?
letJs call it R(cdS? some1here in #our P$T5
K (cd director# - change director# and set prompt
K 8# signalling the login shell to read a command file
cat VLdC2!.9'BSnot setSe ZZ'4.
cd L*
PS*`Sf[p1df[L R
'4.
kill -LdPR42PTS93BSnot setSe Ld4397^S5'BSnot setSe
7o1 change directories 1ith R(cd OsomeOdirS.
Dorn Shell <ksh=K
Put this in #our .profile fileK
PS*`JLP0! L [
9f #ou Nust 1ant the last component of the director#? use
PS*`JLdP0!MMAOe L [
T C shell <tcsh=
Tcsh is a popular enhanced %ersion of csh 1ith 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=
f1 in LPS* gi%es the full pathname of the current director#?
1ith > e(pansion for L542'\ f0 gi%es the 8asename of
the current director#. So? in addition to the a8o%e sh and
*6+
ksh solutions? #ou could use
PS*`Jf1 L [
or
PS*`Jf0 L [
-.5= 5o1 do 9 read characters from the terminal in a shell scriptB
9n sh? use read. 9t is most common to use a loop like
1hile read line
do
...
done
9n csh? use LZ like thisK
1hile < * =
set line ` RLZS
if < RLlineS `` RS = 8reak
...
end
Unfortunatel# csh has no 1a# of distinguishing 8et1een a 8lank line and an end-of-file.
9f #ouJre using sh and 1ant to read a single character from the
terminal? #ou can tr# something like
echo -n R'nter a characterK R
stt# c8reak M or stt# ra1
readchar`[dd if`Ode%Ott# 8s`* count`* -VOde%Onull[
stt# -c8reak
echo RThank #ou for t#ping a Lreadchar .S
-.6= 5o1 do 9 rename RA.fooS to RA.8arS? or change file names to lo1ercaseB
0h# doesnJt Rm% A.foo A.8arS 1orkB Think a8out ho1 the shell e(pands 1ildcards. 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 1a#s. CS5 prints R7o match.S 8ecause it canJt match
RA.8arS. S5 e(ecutes Rm% a.foo 8.foo c.foo A.8arS? 1hich 1ill onl# succeed if #ou happen to
ha%e a single director# named RA.8arS? 1hich is %er# unlikel# and almost certainl# not 1hat
#ou had in mind.
!epending on #our shell? #ou can do it 1ith 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
*6*
end
,ourne ShellK
for f in A.foo\ do
8ase`[8asename Lf .foo[
m% Lf L8ase.8ar
done
Some shells ha%e their o1n %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 1ant 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 1a#s? 8ut the general
looping idea is the same. You can also con%ert file names into
Rm%S commands 1ith [sedJ? and hand the commands off to RshS for
e(ecution. Tr#
ls -d A.foo W sed -e [sO.AOm% X XOJ -e [sOfooLO8arOJ W sh
$ program 8# Vladimir anin called Rmm%S that does this No8
nicel# 1as 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 lo1er case or
%ice %ersa. You could use something like this to rename uppercase files to lo1ercaseK
C ShellK
foreach f < A =
m% Lf [echo Lf W tr [H$-UIJ [Ha-FIJ[ end ,ourne ShellK
for f in A\ do
m% Lf [echo Lf W tr [H$-UIJ [Ha-FIJ[ done Dorn ShellK
t#peset -l l
for f in A\ do
l`SLfS
m% Lf Ll
done
9f #ou 1anted to 8e reall# thorough and handle files 1ith [funn#J
names <em8edded 8lanks or 1hate%er= #ouJd need to use
,ourne ShellK
*6-
for f in A\ do
g`[e(pr R(((LfS K [(((f<.Af=J W tr [H$-UIJ [Ha-FIJ[
m% RLfS RLgS
done
The [e(prJ command 1ill al1a#s print the filename? e%en if it eEuals [-nJ or if it contains a
S#stem V escape seEuence like [fcJ.
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 1ant the HI 1ill 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# arr# 0all %er#
useful. 9t can 8e used to accomplish a 1ide %ariet# of filename changes.
MYOusrO8inOperl
M
M rename script e(amples from l1allK
M rename [sOf.origLOOJ A.orig
M rename [#O$-UOa-FO 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
L1as ` L^\
e%al Lop\
die La if La\
rename<L1as?L^= unless L1as eE L^\
e
-."= 0h# do 9 get Hsome strange error messageI 1hen 9 Rrsh host commandS B
<0eJre talking a8out the remote shell program RrshS or sometimes RremshS or RremoteS\ on
some machines? there is a restricted shell called RrshS? 1hich is a different thing.=
9f #our remote account uses the C shell? the remote host 1ill fire up a C shell to e(ecute
[commandJ for #ou? and that shell 1ill read #our remote .cshrc file. Perhaps #our .cshrc
contains a Rstt#S? R8iffS or some other command that isnJt appropriate for a non-interacti%e
shell. The une(pected output or error message from these commands can scre1 up #our rsh
in odd 1a#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 some-machine date
stt#K K CanJt assign reEuested address
0here are #ouB
Tue 4ct * +)K-6K65 'ST *))*
*6/
You might also get similar errors 1hen running certain RatS or RcronS No8s that also read
#our .cshrc file.
.ortunatel#? the fi( is simple. There are? Euite possi8l#? a 1hole )nch of operations in #our
R.cshrcS <e.g.? Rset histor#`7S= that are simpl# not 1orth doing e(cept in interacti%e shells.
0hat #ou do is surround them in #our R.cshrcS 1ithK
if < LBprompt = then
operations....
endif
and? since in a non-interacti%e shell RpromptS 1onJt 8e set? the operations in Euestion 1ill
onl# 8e done in interacti%e shells.
You ma# also 1ish to mo%e some commands to #our .login file\ if those commands onl# need
to 8e done 1hen a login session starts up <checking for ne1 mail? unread ne1s and so on= itJs
8etter to ha%e them in the .login file.
-.&= 5o1 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 1ithout making special arrangements. 0hen a child process
is created? it inherits a cop# of its parentJs %aria8les <and current director#=. The child can
change these %alues all it 1ants 8ut the changes 1onJt affect the parent shell? since the child
is changing a cop# of the original data.
Some special arrangements are possi8le. Your child process could 1rite out the changed
%aria8les? if the parent 1as prepared to read the output and interpret it as commands to set its
o1n %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 1ill affect the original shell.
.or instance? if #ou ha%e a C shell script named Rm#scriptSK
cd O%er#OlongOpath
seten% P$T5 OsomethingKOsomething-else
or the eEui%alent ,ourne or Dorn shell script
cd O%er#OlongOpath
P$T5`OsomethingKOsomething-else e(port P$T5
and tr# to run Rm#scriptS from #our shell? #our shell 1ill fork and run the shell script in a
su8process. The su8process is also running the shell\ 1hen it sees the RcdS command it
changes its current director#? and 1hen it sees the Rseten%S command it changes its
en%ironment? 8ut neither has an# effect on the current director# of the shell at 1hich #ouJre
t#ping <#our login shell? letJs sa#=.
9n order to get #our login shell to e(ecute the script <1ithout
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
*66
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 1ill pro8a8l#
8e simpler to use a C shell alias or ,ourneODorn shell function. See the Rho1 do 9 get the
current director# into m# promptS section of this article for some e(amples.
$ much more detailed ans1er prepared 8#
(tmatelelogic.se <Thomas 2ichanek= can 8e found at
ftp.1g.omron.co.Np in Opu8Ouni(-faEOdocsOscript-%s-en%.
-.)= 5o1 do 9 redirect stdout and stderr separatel# in cshB
9n csh? #ou can redirect stdout 1ith RVS? or stdout and stderr
together 1ith RVXS 8ut there is no direct 1a# to redirect stderr
onl#. The 8est #ou can do is
< command Vstdout^file = VXstderr^file
1hich 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# 1inds up in stderr^file.
9f 1hat #ou 1ant is to a%oid redirecting stdout at all? let sh do it for #ou.
sh -c [command -Vstderr^fileJ
-.*+= 5o1 do 9 tell inside .cshrc if 9Jm a login shellB
0hen people ask this? the# usuall# mean either
5o1 can 9 tell if itJs an interacti%e shellB or
5o1 can 9 tell if itJs a top-le%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 1ith R.cshrcS= 8# fooling around 1ith RpsS and RLLS. ogin shells generall#
ha%e names that 8egin 1ith a [-[. 9f #ouJre reall# interested in the other t1o Euestions? hereJs
one 1a# #ou can organiFe #our .cshrc to find out.
if <Y LBCS5'V'= then
M
M This is a Rtop-le%elS shell?
M perhaps a login shell? perhaps a shell started up 8# M [rsh machine some-commandJ M This is
1here 1e should set P$T5 and an#thing else 1e M 1ant to appl# to e%er# one of our shells.
M
seten% CS5'V' +
set home ` >username M Nust to 8e sure
source >O.en% M en%ironment stuff 1e al1a#s 1ant
else
*65
M
M This shell is a child of one of our other shells so M 1e donJt need to set all the en%ironment
%aria8les again.
M
set tmp ` LCS5'V'
a tmp::
seten% CS5'V' Ltmp
endif
M '(it from .cshrc if not interacti%e? e.g. under rsh
if <Y LBprompt= e(it
M 5ere 1e could set the prompt or aliases that 1ould 8e useful M for interacti%e shells onl#.
source >O.aliases
-.**= 5o1 do 9 construct a shell glo8-pattern that matches all files
e(cept R.S and R..S B
YouJd think this 1ould 8e eas#.
2atches all files that donJt 8egin 1ith a R.S\
.A 2atches all files that do 8egin 1ith a R.S? 8ut
this includes the special entries R.S and R..S?
1hich often #ou donJt 1ant\
.HY.IA <7e1er 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 1ell\ all porta8le applications shall
not use an unEuoted R]S immediatel# follo1ing the RHS=
2atches all files that 8egin 1ith a R.S and are
follo1ed 8# a non-R.S\ unfortunatel# this 1ill miss
R..fooS\
.BBA 2atches files that 8egin 1ith a R.S and 1hich are
at least / 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 / 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 t1o and use 8ackEuote su8stitution.
This is prett# goodK
[ls -a W sed -e [O]f.LOdJ -e [O]f.f.LOdJ[
<or [ls -$[ in some Uni( %ersions=
*66
8ut e%en it 1ill mess up on files 1ith ne1lines? 9.S characters or 1ildcards in their names.
9n ksh? #ou can useK .Y<.W= A
-.*-= 5o1 do 9 find the last argument in a ,ourne shell scriptB
$ns1er 8#K
2artin 0eitFel Zamikros.s#stem1are.deKmartinam1tech.uucpV
2aarten itmaath Zmaartanat.%u.nlV
9f #ou are sure the num8er of arguments is at most )? #ou can useK
e%al last`fLdLMe
9n P4S9;-compati8le shells it 1orks for $7Y num8er of arguments.
The follo1ing 1orks al1a#s tooK
for last
do
K
done
This can 8e generaliFed as follo1sK
for i
do
third^last`Lsecond^last
second^last`Llast
last`Li
done
7o1 suppose #ou 1ant to R'24V' the last argument from the list? or R'V'RS' the
argument list? or $CC'SS the 7-th argument directl#? 1hate%er 7 ma# 8e. 5ere is a 8asis of
ho1 to do it? using onl# 8uilt-in shell constructs? 1ithout creating su8processesK
t+` u+` rest`J* - / 6 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 /
esac
e%al arg%LhLtLu`fL*
arg%`RLarg% fRfLarg%LhLtLufRR M <*=
shift
done
u+`+
done
*6"
t+`+
done
M no1 restore the arguments
e%al set ( RLarg%S M <-=
shift
This e(ample 1orks 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 1hat funn# characters the# containY
To find the 7-th argument no1 #ou can use thisK
e%al arg7`fLarg%L7
To re%erse the arguments the line marked <*= must 8e changed toK
arg%`RfRfLarg%LhLtLufR Larg%R
5o1 to remo%e the last argument is left as an e(ercise.
9f #ou allo1 su8processes as 1ell? possi8l# e(ecuting non8uilt-in commands? the [arg%7J
%aria8les can 8e set up more easil#K
7`*
for i
do
e%al arg%L7`fLi
7`[e(pr L7 : *[
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 1ant to delete e.g. the last argument?
8ut in that case #ou cannot refer directl# to the 7-th argument an# more? 8ecause the [arg%7J
%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
-.*/= 0hatJs 1rong 1ith 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. 0hen #ou t#pe a command name 1ithout 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 1ill run the first matching program it finds.
4ne of the directories in the P$T5 list can 8e the current
*6&
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
5a%ing R.S some1here in the P$T5 is con%enient - #ou can t#pe Ra.outS instead of R.Oa.outS
to run programs in the current director#. ,ut thereJs a catch.
Consider 1hat happens in the case 1here R.S is the first entr# in the P$T5. Suppose #our
current director# is a pu8licall#-1rita8le 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 1ill instead run R.OlsS? the other userJs
program. 7eedless to sa#? the results of running an unkno1n 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.
7o1 if #ouJre in Otmp and #ou t#pe RlsS? the shell 1ill
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 - if #ouJre
a clums# t#pist and some da# t#pe Rsl -lS instead of Rls -lS? #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. ,e1are.
2an# seasoned Uni( users get 8# Nust fine 1ithout 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# 1orth it.
-.*6= 5o1 do 9 ring the terminal 8ell during a shell scriptB
The ans1er depends on #our Uni( %ersion <or rather on the kind of RechoS program that is
a%aila8le on #our machine=.
$ ,S!-like RechoS uses the R-nS option for suppressing the final
ne1line and does not understand the octal fnnn notation. Thus
the command is
echo -n []3J
*6)
1here ]3 means a literal ,'-character <#ou can produce this in emacs using RCtrl-G Ctrl-
3S and in %i using RCtrl-V Ctrl-3S=.
$ S#sV-like RechoS understands the fnnn notation and uses fc to suppress the final ne1line? so
the ans1er isK
echo [f++"fcJ
-.*5= 0h# canJt 9 use RtalkS to talk 1ith m# friend on machine ;B
Uni( has three common RtalkS programs? none of 1hich can talk 1ith an# of the others. The
RoldS talk accounts for the first t1o t#pes. This %ersion <often called otalk= did not take
RendianS order into account 1hen talking to other machines. $s a conseEuence? the Va(
%ersion of otalk cannot talk 1ith the Sun %ersion of otalk. These %ersions of talk use port
5*".
$round *)&"? most %endors <e(cept Sun? 1ho took 6 #ears longer than an# of their
competitors= standardiFed on a ne1 talk <often called ntalk= 1hich kno1s a8out net1ork 8#te
order. This talk 1orks 8et1een all machines that ha%e it. This %ersion of talk uses port 5*&.
There are no1 a fe1 talk programs that speak 8oth ntalk and one %ersion of otalk. The most
common of these is called R#talkS.
-.*6= 0h# does calendar produce the 1rong 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 0 Th . S
* - *6 *5 *6 *" *& *) -+ -* -- -/ -6 -5 -6 -" -& -) /+
This is the month in 1hich the US <the entire ,ritish 'mpire actuall#= s1itched from the
Culian to the 3regorian calendar.
The other common pro8lem people ha%e 1ith the calendar program is that the# pass it
arguments like [cal ) )6J. This gi%es the calendar for Septem8er of $! )6? 74T *))6.
Uni" ' $re<uently Asked >uestions 071 ?$re<uent
posting@
*5+
This article includes ans1ers toK
/.*= 5o1 do 9 find the creation time of a fileB
/.-= 5o1 do 9 use RrshS 1ithout ha%ing the rsh hang around until the remote command
has completedB
/./= 5o1 do 9 truncate a fileB
/.6= 0h# doesnJt findJs RdeS s#m8ol do 1hat 9 1antB
/.5= 5o1 do 9 set the permissions on a s#m8olic linkB
/.6= 5o1 do 9 RundeleteS a fileB
/."= 5o1 can a process detect if itJs running in the 8ackgroundB
/.&= 0h# doesnJt redirecting a loop 1ork as intendedB <,ourne shell=
/.)= 5o1 do 9 run [pass1dJ? [ftpJ? [telnetJ? [tipJ and other interacti%e programs from a
shell script or in the 8ackgroundB
/.*+= 5o1 do 9 find the process 9! of a program 1ith a particular name from inside a shell script
or C programB
/.**= 5o1 do 9 check the e(it status of a remote command e(ecuted %ia RrshS B
/.*-= 9s it possi8le to pass shell %aria8le settings into an a1k programB
/.*/= 5o1 do 9 get rid of Fom8ie processes that perse%ereB
/.*6= 5o1 do 9 get lines from a pipe as the# are 1ritten instead of onl# in larger 8locksB
/.*5= 5o1 do 9 get the date into a filenameB
/.*6= 0h# do some scripts start 1ith MY ... B
9f #ouJre looking for the ans1er to? sa#? Euestion /.5? and 1ant to skip e%er#thing else? #ou can
search ahead for the regular e(pression R]/.5=S.
0hile these are all legitimate Euestions? the# seem to crop up in comp.uni(.Euestions or
comp.uni(.shell on an annual 8asis? usuall# follo1ed 8# plent# of replies <onl# some of 1hich are
correct= and then a period of griping a8out ho1 the same Euestions keep coming up. You ma#
also like to read the monthl# article R$ns1ers to .reEuentl# $sked GuestionsS in the ne1sgroup
Rne1s.announce.ne1usersS? 1hich 1ill tell #ou 1hat RU79;S stands for.
0ith the %ariet# of Uni( s#stems in the 1orld? itJs hard to guarantee that these ans1ers 1ill 1ork
e%er#1here. Read #our local manual pages 8efore tr#ing an#thing suggested here. 9f #ou ha%e
suggestions or corrections for an# of these ans1ers? please send them to to tmatimaraisgtec.com.
/.*= 5o1 do 9 find the creation time of a fileB
You canJt - it isnJt stored an#1here. .iles ha%e a last-modified time <sho1n 8# Rls -lS=? a last-
accessed time <sho1n 8# Rls -luS= and an inode change time <sho1n 8# Rls -lcS=. The latter is
often referred to as the Rcreation timeS - e%en in some man pages - 8ut thatJs 1rong\ itJs also
set 8# such operations as m%? ln? chmod? cho1n and chgrp.
The man page for Rstat<-=S discusses this.
*5*
/.-= 5o1 do 9 use RrshS 1ithout ha%ing the rsh hang around until the
remote command has completedB
<See note in Euestion -." a8out 1hat RrshS 1eJre talking a8out.=
The o8%ious ans1ers 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 1onJt e(it right a1a#. 9t 1ill 1ait 6+ seconds until the remote [sleepJ command
finishes? e%en though that command 1as started in the 8ackground on the remote machine.
So ho1 do #ou get the [rshJ to e(it immediatel# after the [sleepJ is startedB
The solution - if #ou use csh on the remote machineK
rsh machine -n [command VXOde%Onull ZOde%Onull XJ
9f #ou use sh on the remote machineK
rsh machine -n [command VOde%Onull -VX* ZOde%Onull XJ
0h#B R-nS attaches rshJs stdin to Ode%Onull so #ou could run the complete rsh command in
the 8ackground on the 4C$ machine. Thus R-nS 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 flo1 an#
more.=
7oteK The file that #ou redirect toOfrom on the remote machine doesnJt ha%e to 8e Ode%Onull\
an# ordinar# file 1ill do.
9n man# cases? %arious parts of these complicated commands arenJt necessar#.
/./= 5o1 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 1ell.
.or s#stems 1hich support the ftruncate function? there are three kno1n 8eha%ioursK
,S! 6.- - Ultri(? S39? #n(4S
truncation doesnJt gro1 file
truncation doesnJt mo%e file pointer
,S! 6./ - Sun4S? Solaris? 4S.O*? 5POU;? $miga
truncation can gro1 file
truncation doesnJt mo%e file pointer
*5-
Cra# - UniC4S "? UniC4S &
truncation doesnJt gro1 file
truncation changes file pointer
4ther s#stems come in four %arietiesK
.^C5S9U' - 4nl# SC4
some s#stems define .^C5S9U' 8ut donJt support it
8eha%es like ,S! 6./
.^.R''SP - 4nl# 9nterati%e Uni(
some s#stems <eg. 9nteracti%e Uni(= define .^.R''SP 8ut donJt support it
8eha%es like ,S! 6./
chsiFe<= - G7; and SC4
some s#stems <eg. 9nteracti%e Uni(= ha%e chsiFe<= 8ut donJt support it
8eha%es like ,S! 6./
nothing - no kno1n s#stems
there 1ill 8e s#stems that donJt support truncate at all
2oderatorJs 7oteK 9 gra88ed the functions 8elo1 a fe1 #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 non-nati%e ftruncate follo1
OA ftruncate emulations that 1ork 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 follo1ing function 1as 1ritten 8#
kucharskaSol8ourne.com <0illiam Ducharski= AO
Minclude
Minclude
Minclude
*5/
int
ftruncate <fd? length=
int fd\
off^t length\
d
struct flock fl\
struct stat file8uf\
if <fstat <fd? Xfile8uf= Z +=
return -*\
if <file8uf.st^siFe Z length=
d
OA '(tend file length. AO
if <lseek <fd? <length - *=? S''D^S'T= Z +=
return -*\
OA 0rite a R+S 8#te. AO
if <1rite <fd? RS? *= Y` *=
return -*\
e
else
d
OA Truncate length. AO
fl.l^1hence ` +\
fl.l^len ` +\
fl.l^start ` length\
fl.l^t#pe ` .^0RCD\ OA 0rite lock on file space. AO
OA This relies on the U7!4CU2'7T'! .^.R''SP argument to fcntl? 1hich truncates the
file so that it ends at the position indicated 8# fl.l^start.
0ill minor miracles ne%er ceaseB AO
if <fcntl <fd? .^.R''SP? Xfl= Z +=
return -*\
e
return +\
e
Melse
int
ftruncate <fd? length=
int fd\
off^t length\
d
return chsiFe <fd? length=\
e
Mendif
Mendif
*56
/.6= 0h# doesnJt findJs RdeS s#m8ol do 1hat 9 1antB
RfindS has a -e(ec option that 1ill e(ecute a particular command on all the selected files. .ind
1ill replace an# RdeS it sees 1ith 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 -t#pe d -e(ec command deOfA f\
hoping that find 1ill e(ecute? in turn
command director#*OA
command director#-OA
...
Unfortunatel#? find onl# e(pands the RdeS token 1hen it appears
8# itself. .ind 1ill lea%e an#thing else like RdeOAS alone? so
instead of doing 1hat #ou 1ant? it 1ill do
command deOA
command deOA
...
once for each director#. This might 8e a 8ug? it might 8e a feature? 8ut 1eJre stuck 1ith the
current 8eha%iour.
So ho1 do #ou get around thisB 4ne 1a# 1ould 8e to 1rite a
tri%ial little shell script? letJs sa# R.OdoitS? that consists of
command RL*SOA
You could then use
find Opath -t#pe d -e(ec .Odoit de f\
4r if #ou 1ant to a%oid the R.OdoitS shell script? #ou can use
find Opath -t#pe d -e(ec sh -c [command L+OAJ de f\
<This 1orks 8ecause 1ithin the [commandJ of Rsh -c [commandJ $ , C ...S?
L+ e(pands to $? L* to ,? and so on.=
or #ou can use the construct-a-command-1ith-sed trick
find Opath -t#pe d -print W sed [sK.AKcommand XOAKJ W sh
9f all #ouJre tr#ing to do is cut do1n 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 1ill fit into
one command line. You could use
find Opath -print W (args command
1hich 1ould result in one or more e(ecutions of
command file* file- file/ file6 dir*Ofile* dir*Ofile-
*55
Unfortunatel# this is not a perfectl# ro8ust or secure solution. ;args e(pects its input lines to
8e terminated 1ith ne1lines? so it 1ill 8e confused 8# files 1ith odd characters such as
ne1lines in their names.
/.5= 5o1 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.
/.6= 5o1 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.
4f course? an# decent s#stems administrator should 8e doing regular 8ackups. Check 1ith
#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? 1hen #ou delete a file 1ith RrmS it is gone. 4nce #ou RrmS a
file? the s#stem totall# forgets 1hich 8locks scattered around the disk 1ere part of #our file.
'%en 1orse? the 8locks from the file #ou Nust deleted are going to 8e the first ones taken and
scri88led upon 1hen the s#stem needs more disk space. 5o1e%er? ne%er sa# ne%er. 9t is
theoreticall# possi8le i! #ou shut do1n the s#stem immediatel# after the RrmS to reco%er
portions of the data. 5o1e%er? #ou had 8etter ha%e a %er# 1iFardl# t#pe person at hand 1ith
hours or da#s to spare to get it all 8ack.
Your first reaction 1hen #ou RrmS a file 8# mistake is 1h# not make a shell alias or
procedure 1hich changes RrmS to mo%e files into a trash 8in rather than delete themB That
1a# #ou can reco%er them if #ou make a mistake? and periodicall# clean out #our trash 8in.
T1o pointsK first? this is generall# accepted as a )ad idea. You 1ill 8ecome dependent upon
this 8eha%iour of RrmS? and #ou 1ill find #ourself someda# on a normal s#stem 1here RrmS
is reall# RrmS? and #ou 1ill get #ourself in trou8le. Second? #ou 1ill e%entuall# find that the
hassle of dealing 1ith the disk space and time in%ol%ed in maintaining the trash 8in? it might
8e easier Nust to 8e a 8it more careful 1ith RrmS. .or starters? #ou should look up the R-iS
option to RrmS in #our manual.
9f #ou are still undaunted? then here is a possi8le simple ans1er. You can create #ourself a
RcanS command 1hich mo%es files into a trashcan director#. 9n csh<*= #ou can place the
follo1ing 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 -f >O.trashcanOAJ M irretrie%a8l# empt# trash
if < Y -d >O.trashcan = mkdir >O.trashcan M ensure trashcan e(ists
You might also 1ant to put aK
rm -f >O.trashcanOA
in the R.logoutS file in #our home director# to automaticall# empt# the trash 1hen #ou log
out. <sh and ksh %ersions are left as an e(ercise for the reader.=
*56
29TJs ProNect $thena has produced a comprehensi%e deleteOundeleteOe(pungeOpurge package?
1hich can ser%e as a complete replacement for rm 1hich allo1s file reco%er#. This package
1as posted to comp.sources.misc <%olume *"? issue +-/-+-6=
/."= 5o1 can a process detect if itJs running in the 8ackgroundB
.irst of allK do #ou 1ant to kno1 if #ouJre running in the 8ackground? or if #ouJre running
interacti%el#B 9f #ouJre deciding 1hether or not #ou should print prompts and the like? thatJs
pro8a8l# a 8etter criterion. Check if standard input is a terminalK
shK if H -t + 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 U79; ha%e different notions of 1hat RforegroundS and
R8ackgroundS mean - and on the most common t#pe of s#stem 1ith a 8etter-defined notion of
1hat the# mean? programs can 8e mo%ed ar8itraril# 8et1een foreground and 8ackgroundY
U79; s#stems 1ithout No8 control t#picall# put a process into the 8ackground 8# ignoring
S9397T and S93GU9T and redirecting the standard input to ROde%OnullS\ this is done 8# the
shell.
Shells that support No8 control? on U79; 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 1hich 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 U79; s#stems that
support No8 control? t#picall# do 1hat shells do on s#stems that donJt support No8 control.
/.&= 0h# doesnJt redirecting a loop 1ork as intendedB <,ourne shell=
Take the follo1ing e(ampleK
foo`8ar
1hile read line
do
M do something 1ith Lline
foo`8letch
done Z OetcOpass1d
echo Rfoo is no1K LfooS
!espite the assignment Rfoo`8letchS this 1ill print Rfoo is no1K 8arS in man#
implementations of the ,ourne shell. 0h#B ,ecause of the follo1ing? 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 1hich the structure is e(ecuted\ %aria8les set in
that su8shell <like the Rfoo`8letchS assignment= donJt affect the current shell? of course.
*5"
The P4S9; *++/.- Shell and Tools 9nterface standardiFation committee for8ids the 8eha%iour
descri8ed a8o%e? i.e. in P*++/.- conformant ,ourne shells the e(ample 1ill print Rfoo is no1K
8letchS.
9n historic <and P*++/.- conformant= implementations #ou can use the follo1ing [trickJ to get
around the redirection pro8lemK
foo`8ar
M make file descriptor ) a duplicate of file descriptor + <stdin=\
M then connect stdin to OetcOpass1d\ the original stdin is no1
M [remem8eredJ in file descriptor )\ see dup<-= and sh<*=
e(ec )ZX+ Z OetcOpass1d
1hile read line
do
M do something 1ith 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) )ZX-
echo Rfoo is no1K LfooS
This should al1a#s print Rfoo is no1K 8letchS.
Right? take the ne(t e(ampleK
foo`8ar
echo 8letch W read foo
echo Rfoo is no1K LfooS
This 1ill print Rfoo is no1K 8arS in man# implementations? Rfoo is no1K 8letchS in some
others. 0h#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 1ill e(ecute it? else another su8shell is created.
P4S9; *++/.- allo1s 8oth 8eha%iours so porta8le scripts cannot depend on an# of them.
/.)= 5o1 do 9 run [pass1dJ? [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
1ith such programs. The follo1ing e(pect script is an e(ample of a non-interacti%e %ersion
of pass1d<*=.
M username is passed as *
st
arg? pass1ord as -
nd
set pass1ord Hinde( Larg% -I
spa1n pass1d Hinde( Larg% *I
*5&
e(pect RApass1ordKS
send RLpass1ordfrS
e(pect RApass1ordKS
send RLpass1ordfrS
e(pect eof
e(pect can partiall# automate interaction 1hich is especiall# useful for telnet? rlogin?
de8uggers or other programs that ha%e no 8uilt-in 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 1ritten to manage the connection to a pseudo-tt# 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# 6.+ program? 1hich runs a program under a pseudo-
tt# session and 1as posted to comp.sources.uni(? %olume -5. $ pt#-8ased solution using
named pipes to do the same as the a8o%e might look like thisK
MYO8inOsh
OetcOmknod out.LL p\ e(ec -VX*
< e(ec 6Zout.LL\ rm -f out.LL
ZX6 1aitfor [pass1ordKJ
echo RL-S
ZX6 1aitfor [pass1ordKJ
echo RL-S
ZX6 cat VOde%Onull
= W < pt# pass1d RL*S Vout.LL =
5ere? [1aitforJ is a simple C program that searches for
its argument in the input? character 8# character.
$ simpler pt# solution <1hich has the dra18ack of not
s#nchroniFing properl# 1ith the pass1d program= is
MYO8inOsh
< sleep 5\ echo RL-S\ sleep 5\ echo RL-S= W pt# pass1d RL*S
/.*+= 5o1 do 9 find the process 9! of a program 1ith a particular name
from inside a shell script or C programB
9n a shell scriptK
There is no utilit# specificall# designed to map 8et1een program names and process 9!s.
.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
*5)
starts running. 5o1e%er? a pipeline like this can often 8e used to get a list of processes
<o1ned 8# #ou= 1ith a particular nameK
ps u( W a1k [OnameO XX YOa1kO dprint L-eJ
You replace RnameS 1ith the name of the process for 1hich #ou are searching.
The general idea is to parse the output of ps? using a1k or grep or other utilities? to search for
the lines 1ith the specified name on them? and print the P9!Js for those lines. 7ote that the
RYOa1kOS a8o%e pre%ents the a1k process for 8eing listed.
You ma# ha%e to change the arguments to ps? depending on 1hat kind of Uni( #ou are using.
9n a C programK
Cust as there is no utilit# specificall# designed to map 8et1een program names and process
9!s? there are no <porta8le= C li8rar# functions to do it either.
5o1e%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 super-user <or a user in
group RkmemS= if read-access 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? 1hich appears as a director# 1ith a 8unch of
filenames in it. 'ach filename is a num8er? corresponding to a process 9!? and #ou can open
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 %endor-specific li8rar# functions? and #ou donJt ha%e Oproc? and #ou still
1ant 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 ho1 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
1ith kernel rummaging 1as posted to alt.sources in 2a# *))* and is also a%aila8le %ia
anon#mous ftp as usenetOalt.sourcesOarticlesOd/-)d6?"?&?)e?//+d+?*ee.U from
1uarchi%e.1ustl.edu.=
/.**= 5o1 do 9 check the e(it status of a remote command
e(ecuted %ia RrshS B
This doesnJt 1orkK
rsh some-machine some-crumm#-command WW echo RCommand failedS
The e(it status of [rshJ is + <success= if the rsh program itself completed successfull#? 1hich
pro8a8l# isnJt 1hat #ou 1anted.
9f #ou 1ant to check on the e(it status of the remote program? #ou can tr# using 2aarten
itmaathJs [ershJ script? 1hich 1as posted to alt.sources in 4cto8er *))6. 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 1ith that status.
*6+
/.*-= 9s it possi8le to pass shell %aria8le settings into an a1k programB
There are t1o different 1a#s to do this. The first in%ol%es simpl# e(panding the %aria8le
1here it is needed in the program.
.or e(ample? to get a list of all tt#s #ouJre usingK
1ho W a1k [O]JSLUS'RSJO d print L- eJ <*=
Single Euotes are usuall# used to enclose a1k programs 8ecause the character [LJ is often
used in them? and [LJ 1ill 8e interpreted 8# the shell if enclosed inside dou8le Euotes? 8ut not
if enclosed inside single Euotes. 9n this case? 1e want the [LJ in RLUS'RS to 8e interpreted
8# the shell? so 1e close the single Euotes and then put the RLUS'RS inside dou8le Euotes.
7ote that there are no spaces in an# of that? so the shell 1ill
see it all as one argument. 7ote? further? that the dou8le
Euotes pro8a8l# arenJt necessar# in this particular case <i.e. 1e
could ha%e done
1ho W a1k [O]JLUS'RJO d print L- eJ <-=
=? 8ut the# should 8e included ne%ertheless 8ecause the# are necessar# 1hen the shell %aria8le
in Euestion contains special characters or spaces.
The second 1a# to pass %aria8le settings into a1k is to use an often undocumented feature of
a1k 1hich allo1s %aria8le settings to 8e specified as Rfake file namesS on the command line.
.or e(ampleK
1ho W a1k [L* `` user d print L- eJ user`SLUS'RS - </=
Varia8le settings take effect 1hen the# are encountered on the command line? so? for
e(ample? #ou could instruct a1k on ho1 to 8eha%e for different files using this techniEue.
.or e(ampleK
a1k [d program that depends on s eJ s`* file* s`+ file- <6=
7ote that some %ersions of a1k 1ill cause %aria8le settings encountered 8efore an# real
filenames to take effect 8efore the ,'397 8lock is e(ecuted? 8ut some 1onJt so neither 1a#
should 8e relied upon.
7ote? further? that 1hen #ou specif# a %aria8le setting? a1k 1onJt automaticall# read from
stdin if no real files are specified? so #ou need to add a R-R argument to the end of #our
command? as 9 did at </= a8o%e.
$ third option is to use a ne1er %ersion of a1k <na1k=? 1hich allo1s direct access to
en%ironment %aira8les. 'g.
na1k ['7! d print RYour path %aria8le is R '7V9R47HSP$T5SI eJ Ode%Onull
/.*/= 5o1 do 9 get rid of Fom8ie processes that perse%ereB
Unfortunatel#? itJs impossi8le to generaliFe ho1 the death of child processes should 8eha%e?
8ecause the e(act mechanism %aries o%er the %arious fla%ors of Uni(.
*6*
.irst of all? 8# default? #ou ha%e to do a 1ait<= for child processes under $ fla%ors of Uni(.
That is? there is no fla%or of Uni( that 9 kno1 of that 1ill 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#sV-deri%ed s#stems? if #ou do Rsignal<S93C5!? S93^937=S <1ell?
actuall#? it ma# 8e S93C! instead of S93C5!? 8ut most of the ne1er S#sV s#stems ha%e
RMdefine S93C5! S93C!S in the header files=? then child processes 1ill 8e cleaned up
automaticall#? 1ith no further effort in #our part. The 8est 1a# to find out if it 1orks at #our
site is to tr# it? although if #ou are tr#ing to 1rite porta8le code? itJs a 8ad idea to rel# on this
in an# case.
Unfortunatel#? P4S9; doesnJt allo1 #ou to do this\ the 8eha%ior
of setting the S93C5! to S93^937 under P4S9; is undefined? so
#ou canJt do it if #our program is supposed to 8e
P4S9;-compliant.
So? 1hatJs the P4S9; 1a#B $s mentioned earlier? #ou must install a signal handler and 1ait.
Under P4S9; signal handlers are installed 1ith sigaction. Since #ou are not interested in
RstoppedS children? onl# in terminated children? add S$^74C!ST4P to sa^flags. 0aiting
1ithout 8locking is done 1ith 1aitpid<=. The first argument to 1aitpid should 8e -* <1ait for
an# pid=? the third should 8e 0745$73. This is the most porta8le 1a# and is likel# to
8ecome more porta8le in future.
9f #our s#stems doesnJt support P4S9;? thereJs a num8er of 1a#s.
The easiest 1a# is signal<S93C5!? S93^937=? if it 1orks. 9f S93^937 cannot 8e used to
force automatic clean-up? then #ouJ%e got to 1rite a signal handler to do it. 9t isnJt eas# at all
to 1rite a signal handler that does things right on all fla%ors of Uni(? 8ecause of the follo1ing
inconsistenciesK
4n some fla%ors of Uni(? the S93C5! signal handler is called if one or more children ha%e
died. This means that if #our signal handler onl# does one 1ait<= call? then it 1onJt clean up
all of the children. .ortunatel#? 9 8elie%e that all Uni( fla%ors for 1hich this is the case ha%e
a%aila8le to the programmer the 1ait/<= or 1aitpid<= call? 1hich allo1s the 0745$73
option to check 1hether or not there are an# children 1aiting to 8e cleaned up. Therefore? on
an# s#stem that has 1ait/<=O1aitpid<=? #our signal handler should call 1ait/<=O1aitpid<= o%er
and o%er again 1ith the 0745$73 option until there are no children left to clean up.
0aitpid<= is the preferred interface? as it is in P4S9;.
4n S#sV-deri%ed s#stems? S93C5! signals are regenerated if there are child processes still
1aiting to 8e cleaned up after #ou e(it the S93C5! signal handler. Therefore? itJs safe on
most S#sV s#stems to assume 1hen the signal handler gets called that #ou onl# ha%e to clean
up one signal? and assume that the handler 1ill get called again if there are more to clean up
after it e(its.
4n older s#stems? there is no 1a# to pre%ent signal handlers from 8eing automaticall# reset to
S93^!. 1hen the signal handler gets called. 4n such s#stems? #ou ha%e to put
Rsignal<S93C59!? catcher^func=S <1here Rcatcher^funcS is the name of the handler
function= as the last thing in the signal handler? so that it gets reset.
.ortunatel#? ne1er implementations allo1 signal handlers to 8e installed 1ithout 8eing reset
to S93^!. 1hen the handler function is called. To get around this pro8lem? on s#stems that
do not ha%e 1ait/<=O1aitpid<= 8ut do ha%e S93C!? #ou need to reset the signal handler 1ith
a call to signal<= after doing at least one 1ait<= 1ithin the handler? each time it is called. .or
*6-
8ack1ard compati8ilit# reasons? S#stem V 1ill keep the old semantics <reset handler on call=
of signal<=. Signal handlers that stick can 8e installed 1ith sigaction<= or sigset<=.
The summar# of all this is that on s#stems that ha%e 1aitpid<= <P4S9;= or 1ait/<=? #ou
should use that and #our signal handler should loop? and on s#stems that donJt? #ou should
ha%e one call to 1ait<= per in%ocation of the signal handler.
4ne more thingPif #ou donJt 1ant to go through all of this trou8le? there is a porta8le 1a# to
a%oid this pro8lem? although it is some1hat less efficient. Your parent process should fork?
and then 1ait 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 1aiting for it notices its death and continues to 1ork=? and the grandchild does
1hate%er the child 1as originall# supposed to. Since its parent died? it is inherited 8# init?
1hich 1ill do 1hate%er 1aiting is needed. This method is inefficient 8ecause it reEuires an
e(tra fork? 8ut is prett# much completel# porta8le.
/.*6= 5o1 do 9 get lines from a pipe as the# are 1ritten instead of onl# in
larger 8locksB
The stdio li8rar# does 8uffering differentl# depending on 1hether it thinks itJs running on a
tt#. 9f it thinks itJs on a tt#? it does 8uffering on a per-line 8asis\ if not? it uses a larger 8uffer
than one line.
9f #ou ha%e the source code to the client 1hose 8uffering #ou 1ant 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 /.).
/.*5= 5o1 do 9 get the date into a filenameB
This isnJt hard? 8ut it is a 8it cr#ptic at first sight. etJs 8egin 1ith the date command itselfK
date can take a formatting string? to modif# the 1a# in 1hich 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 */+))6. 9f #ou 1ant to punctuate this? Nust put the
characters #ou 1ould like to use in the formatting string <74 S$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.
7o1? getting this into a file name. etJs sa# that 1e 1ant to create files called report.*/+))6
<or 1hate%er the date is toda#=K
*6/
.9'7$2'`report.[date [:TdTmT#J[
7otice that 1e are using t1o 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=.
/.*6= 0h# do some scripts start 1ith MY ... B
Chip Rosenthal has ans1ered a closel# related Euestion in comp.uni(.(eni( in the past.
9 think 1hat confuses people is that there e(ist t1o different mechanisms? 8oth spelled 1ith
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. 0hen the U79; 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 1ith an '74';'C error. 9t 1ill e(ecute the program onl# if the
magic num8er is recogniFa8le.
Second? as time 1ent on and different e(ecuta8le file formats 1ere introduced? the magic
num8er not onl# told the kernel i! 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 ;'79;O/&6 s#stem and carr# the 8inar#
o%er to a S#sVO/&6 U79; s#stem? the kernel 1ill recogniFe the magic num8er and sa# [$haY
This is an (.out 8inar#YJ and configure itself to run 1ith ;'79; compati8le s#stem calls.
7ote that the kernel can onl# run 8inar# e(ecuta8le images. So ho1? #ou might ask? do
scripts get runB $fter all? 9 can t#pe [m#.scriptJ at a shell prompt and 9 donJt get an
'74';'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 `` '74';'C=
e(ecl <RO8inOshS? RshS? R-cS? program? <char A=+=\
OA oh no mr 8illYY AO
perror<program=\
return -*\
<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 1e 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.
*66
9f? ho1e%er? the first e(ecl<= failed then this h#pothetical shell looks at 1h# it failed. 9f the
e(ecl<= failed 8ecause [programJ 1as 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 ho1 the kernel starts up programs. The# hacked
the kernel to recogniFe the magic num8er [MYJ. <2agic num8ers are *6-8its and t1o &-8it
characters makes *6 8its? rightB= 0hen the [MYJ magic num8er 1as recogniFed? the kernel
1ould read in the rest of the line and treat it as a command to run upon the contents of the
file. 0ith this hack #ou could no1 do things likeK
MY O8inOsh
MY O8inOcsh
MY O8inOa1k -.K
This hack has e(isted solel# in the ,erkele# 1orld? and has migrated to US3 kernels as part
of S#stem V Release 6. Prior to V.6? 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.
7o1? lets re1ind a fe1 #ears? to the time 1hen more and more folks running US3 8ased
unices 1ere sa#ing [O8inOsh sucks as an interacti%e user interfaceY 9 1ant cshYJ. Se%eral
%endors did some %alue added magic and put csh in their distri8ution? e%en though csh 1as
not a part of the US3 U79; distri8ution.
This? ho1e%er? presented a pro8lem. etJs sa# #ou s1itch #our login shell to O8inOcsh. etJs
further suppose that #ou are a cretin and insist upon programming csh scripts. YouJd
certainl# 1ant 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 1ant the script to 8e started 8# runningK
e(ecl <RO8inOcshS? RcshS? R-cS? Rm#.scriptS? <char A=+=\
,ut 1hat a8out all those e(isting scriptsPsome of 1hich are part of the s#stem distri8utionB
9f the# started getting run 8# csh then things 1ould 8reak. So #ou needed a 1a# to run some
scripts through csh? and others through sh.
The solution introduced 1as to hack csh to take a look at the first character of the script #ou
are tr#ing to run. 9f it 1as a [MJ then csh 1ould tr# to run the script through O8inOcsh?
other1ise it 1ould run the script through O8inOsh. The e(ample code from the a8o%e might
no1 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 `` '74';'C XX <fp ` fopen<program? RrS== Y` 7U= d
i ` getc<fp=\
<%oid= fclose<fp=\ if <i `` [MJ=
e(ecl <RO8inOcshS? RcshS? R-cS? program? <char A=+=\
else
e(ecl <RO8inOshS? RshS? R-cS? program? <char A=+=\
e
OA oh no mr 8illYY AO
*65
perror<program=\
return -*\
T1o important points. .irst? this is a [cshJ hack. 7othing has 8een changed in the kernel and
nothing has 8een changed in the other shells. 9f #ou tr# to e(ecl<= a script? 1hether or not it
8egins 1ith [MJ? #ou 1ill still get an '74';'C failure. 9f #ou tr# to run a script 8eginning
1ith [MJ from something other than csh <e.g. O8inOsh=? then it 1ill 8e run 8# sh and not csh.
Second? the magic is that either the script 8egins 1ith [MJ or it doesnJt 8egin 1ith [MJ. 0hat
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 follo1ing are identical at the start of a scriptK
K
K O8inOsh
Z--- a 8lank line
K OusrOgamesOrogue
echo R3ee...9 1onder 1hat shell 9 am running underBBBS
9n all these cases? all shells 1ill tr# to run the script 1ith O8inOsh.
Similarl#? all of the follo1ing are identical at the start of a scriptK
M
M O8inOcsh
MY O8inOcsh
MY O8inOsh
M 3ee...9 1onder 1hat shell 9 am running underBBB
$ll of these start 1ith a [MJ. This means that the script 1ill 8e run 8# csh only if #ou tr# to
start it from csh? other1ise it 1ill 8e run 8# O8inOsh.
<7oteK if #ou are running ksh? su8stitute [kshJ for
[shJ in the a8o%e. The Dorn shell is theoreticall#
compati8le 1ith ,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 1hat should happen 1ith [MY
O8inOshJ.
The [MYJ handling is 8ecoming more and more pre%elant. S#stem V Release 6 picks up a
num8er of the ,erkele# features? including this. Some S#stem V Release /.- %endors are
hacking in some of the more %isi8le V.6 features such as this and tr#ing to con%ince #ou this
is sufficient and #ou donJt need things like real? 1orking streams or d#namicall# adNusta8le
kernel parameters.
;'79; does not support [MYJ. The ;'79; O8inOcsh does ha%e the [MJ hack. Support for [MYJ
in ;'79; 1ould 8e nice? 8ut 9 1ouldnJt hold m# 8reath 1aiting for it.
*66
Uni" ' $re<uently Asked >uestions 0A1 ?$re<uent
posting@
This article includes ans1ers toK
6.*= 5o1 do 9 read characters from a terminal 1ithout reEuiring the user to hit R'TUR7B
6.-= 5o1 do 9 check to see if there are characters to 8e read 1ithout actuall# readingB
6./= 5o1 do 9 find the name of an open fileB
6.6= 5o1 can an e(ecuting program determine its o1n pathnameB
6.5= 5o1 do 9 use popen<= to open a process for reading $7! 1ritingB
6.6= 5o1 do 9 sleep<= in a C program for less than one secondB
6."= 5o1 can 9 get setuid shell scripts to 1orkB
6.&= 5o1 can 9 find out 1hich user or process has a file open or is using a particular file
s#stem <so that 9 can unmount itB=
6.)= 5o1 do 9 keep track of people 1ho are fingering meB
6.*+= 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
6.**= 9s it possi8le to Rsp#S on a terminal? displa#ing the output thatJs appearing on it on
another terminalB
9f #ouJre looking for the ans1er to? sa#? Euestion 6.5? and 1ant to skip e%er#thing else? #ou can
search ahead for the regular e(pression R]6.5=S.
0hile these are all legitimate Euestions? the# seem to crop up in comp.uni(.Euestions or
comp.uni(.shell on an annual 8asis? usuall# follo1ed 8# plent# of replies <onl# some of 1hich are
correct= and then a period of griping a8out ho1 the same Euestions keep coming up. You ma#
also like to read the monthl# article R$ns1ers to .reEuentl# $sked GuestionsS in the ne1sgroup
Rne1s.announce.ne1usersS? 1hich 1ill tell #ou 1hat RU79;S stands for.
0ith the %ariet# of Uni( s#stems in the 1orld? itJs hard to guarantee that these ans1ers 1ill 1ork
e%er#1here. Read #our local manual pages 8efore tr#ing an#thing suggested here. 9f #ou ha%e
suggestions or corrections for an# of these ans1ers? please send them to to tmatimaraisgtec.com.
6.*= 5o1 do 9 read characters from a terminal 1ithout reEuiring the user
to hit R'TUR7B
Check out c8reak mode in ,S!? >9C$747 mode in S#sV.
9f #ou donJt 1ant to tackle setting the terminal parameters #ourself <using the Rioctl<-=S
s#stem call= #ou can let the stt# program do the 1ork - 8ut this is slo1 and inefficient? and
#ou should change the code to do it right some timeK
Minclude Zstdio.hV
main<=
*6"
d
int c\
printf<R5it an# character to continuefnS=\
OA
ioctl<= 1ould 8e 8etter here\ onl# laF#
programmers do it this 1a#K
AO
s#stem<RO8inOstt# c8reakS=\ OA or Rstt# ra1S AO
c ` getchar<=\
s#stem<RO8inOstt# -c8reakS=\
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 single-character 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.
6.-= 5o1 do 9 check to see if there are characters to 8e read 1ithout
actuall# readingB
Certain %ersions of U79; pro%ide 1a#s to check 1hether characters
are currentl# a%aila8le to 8e read from a file descriptor. 9n
,S!? #ou can use select<-=. You can also use the .947R'$! ioctl?
1hich returns the num8er of characters 1aiting to 8e read? 8ut
onl# 1orks on terminals? pipes and sockets. 9n S#stem V Release
/? #ou can use poll<-=? 8ut that onl# 1orks on streams. 9n ;eni(
and therefore Uni( S#sV r/.- and later - the rdchk<= s#stem call reports 1hether a read<=
call on a gi%en file descriptor 1ill 8lock.
There is no 1a# to check 1hether characters are a%aila8le to 8e read from a .9' pointer.
<You could poke around inside stdio data structures to see if the input 8uffer is nonempt#?
8ut that 1ouldnJt 1ork since #ouJd ha%e no 1a# of kno1ing 1hat 1ill happen the ne(t
time #ou tr# to fill the 8uffer.=
Sometimes people ask this Euestion 1ith the intention of 1riting
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 1a# to do this? 8ecause it is possi8le that characters 1ill 8e a%aila8le 1hen #ou test for
a%aila8ilit#? 8ut 1ill no longer 8e a%aila8le 1hen #ou call read. 9nstead? set the 4^7!'$Y
flag <1hich is also called .7!'$Y under ,S!= using the .^S'T. option of fcntl<-=.
*6&
4lder s#stems <Version "? 6.* ,S!= donJt ha%e 4^7!'$Y\ on these s#stems the closest
#ou can get to a non8locking read is to use alarm<-= to time out the read.
6./= 5o1 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 1hich
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 1ith the -inum and possi8l# -(de% option? or #ou
can use ncheck? or #ou can recreate the functionalit# of one of these 1ithin #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.
6.6= 5o1 can an e(ecuting program determine its o1n pathnameB
Your program can look at arg%H+I\ if it 8egins 1ith a ROS? it is pro8a8l# the a8solute pathname
to #our program? other1ise #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 1hose
name matches #our programJs arg%H+I <1hich 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 1ith
an# %alue of arg%H+I it desires. 9t is merel# a con%ention that ne1 programs are e(ecJd 1ith
the e(ecuta8le file name in arg%H+I.
.or instance? purel# a h#pothetical e(ampleK
Minclude Zstdio.hV
main<=
d
e(ecl<ROusrOgamesOrogueS? R%i ThesisS? <char A=7U=\
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 K-=
6.5= 5o1 do 9 use popen<= to open a process for reading $7! 1ritingB
*6)
The pro8lem 1ith tr#ing to pipe 8oth input and output to an ar8itrar# sla%e process is that
deadlock can occur? if 8oth processes are 1aiting for not-#et-generated input at the same
time. !eadlock can 8e a%oided onl# 8# ha%ing ,4T5 sides follo1 a strict deadlock-free
protocol? 8ut since that reEuires cooperation from the processes it is inappropriate for a
popen<=-like 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 1riting. 9t uses pt#s
rather than pipes? and has no deadlock pro8lem. 9tJs porta8le to 8oth ,S! and SV. See
Euestion /.) for more a8out [e(pectJ.
6.6= 5o1 do 9 sleep<= in a C program for less than one secondB
The first thing #ou need to 8e a1are of is that all #ou can specif# is a 29792U2 amount of
dela#\ the actual dela# 1ill 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 1hich
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 follo1ing code is adapted from !oug 31#nJs S#stem V emulation support for 6,S! and
e(ploits the 6,S! select<= s#stem call. !oug originall# called it [nap<=J\ #ou pro8a8l# 1ant
to call it Rusleep<=S\
OA
usleepPsupport routine for 6.-,S! s#stem call emulations
last editK -)-4ct-*)&6 ! $ 31#n
AO
e(tern int select<=\
int
usleep< usec = OA returns + if ok? else -* 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 *++++++\
dela#.t%^usec ` usec T *++++++\
return select< +? <long A=+? <long A=+? <long A=+? Xdela# =\
e
*"+
4n S#stem V #ou might do it this 1a#K
OA
su8seconds sleeps for S#stem V - or an#thing that has poll<=
!on i8es? 6O*O*))*
The ,S! analog to this function is defined in terms of microseconds 1hile 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
1ith the idea that #ou are pro8a8l# calling it in a tight loop? and that o%er the long run? the
error 1ill e%en out.
9f #ou arenJt calling it in a tight loop? then #ou almost certainl# arenJt making microsecond-
resolution reEuests an#1a#? in 1hich case #ou donJt care a8out microseconds. $nd if #ou
did? #ou 1ouldnJt 8e using U79; an#1a# 8ecause random s#stem indigestion <i.e.?
scheduling= can make mincemeat out of an# timing code.
Returns + if successful timeout? -* 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./ ha%e
a 8ug 1here 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 non-,S! Unices is Con
UeeffJs s5nap package? posted to comp.sources.misc? %olume 6. 9t does reEuire a installing a
de%ice dri%er? 8ut 1orks fla1lessl# once installed. <9ts resolution is limited to the kernel 5U
%alue? since it uses the kernel dela#<= routine.=
2an# ne1er %ersions of Uni( ha%e a nanosleep function.
6."= 5o1 can 9 get setuid shell scripts to 1orkB
*"*
H This is a long ans1er? 8ut itJs a complicated and freEuentl#-asked
Euestion. Thanks to 2aarten itmaath for this ans1er? and for the RindirS program
mentioned 8elo1. I
et us first assume #ou are on a U79; %ariant <e.g. 6./,S! or Sun4S= that kno1s a8out so-
called [e(ecuta8le shell scriptsJ.
Such a script must start 1ith a line likeK
MYO8inOsh
The script is called [e(ecuta8leJ 8ecause Nust like a real <8inar#= e(ecuta8le it starts 1ith a so-
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# follo1ed 8#
* initial option likeK
MYO8inOsed -f
Suppose this script is called [fooJ and is found in O8in?
then if #ou t#peK
foo arg* arg- arg/
the 4S 1ill rearrange things as though #ou had t#pedK
O8inOsed -f O8inOfoo arg* arg- arg/
There is one difference thoughK if the setuid permission 8it for [fooJ is set? it 1ill 8e honored
in the first form of the command\ if #ou reall# t#pe the second form? the 4S 1ill honor the
permission 8its of O8inOsed? 1hich is not setuid? of course.
4D? 8ut 1hat if m# shell script does 74T start 1ith such a [MYJ line or m# 4S does not kno1
a8out itB
0ell? if the shell <or an#8od# else= tries to e(ecute it? the 4S 1ill return an error indication? as
the file does not start 1ith 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 1e ha%e alread# seen that a setuid 8it on [shell^scriptJ 1ill 74T 8e honored in this caseY
Right? 8ut 1hat a8out the securit# risks of setuid shell scriptsB
0ell? suppose the script is called [OetcOsetuid^scriptJ? starting 1ithK
MYO8inOsh
7o1 let us see 1hat happens if 1e issue the follo1ing commandsK
L cd Otmp
L ln OetcOsetuid^script -i
L P$T5`.
L -i
0e kno1 the last command 1ill 8e rearranged toK
*"-
O8inOsh -i
,ut this command 1ill gi%e us an interacti%e shell? setuid to the o1ner of the scriptY
.ortunatel# this securit# hole can easil# 8e closed 8# making the first lineK
MYO8inOsh -
The [-[ signals the end of the option listK the ne(t argument [-iJ 1ill 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 --+ temp X
L m% m#^script temp
The third command 1ill 8e rearranged toK
nice --+ O8inOsh - temp
$s this command runs so slo1l#? the fourth command might 8e a8le to replace the original
[tempJ 1ith [m#^scriptJ ,'.4R' [tempJ is opened 8# the shellY There are 6 1a#s to fi( this
securit# holeK
*= let the 4S start setuid scripts in a different? secure 1a#
S#stem V R6 and 6.6,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 - if #ou use the [indirJ program from
comp.sources.uni( the setuid script 1ill look like thisK
MYO8inOindir -u
MBO8inOsh OetcOsetuid^script
/= make a [8inar# 1rapperJK a real e(ecuta8le that is setuid and 1hose onl# task is to
e(ecute the interpreter 1ith the name of the script as an argument
6= make a general [setuid script ser%erJ that tries to locate the reEuested [ser%iceJ in a
data8ase of %alid scripts and upon success 1ill start the right interpreter 1ith the right
arguments.
7o1 that 1e 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 1h#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 1ell?
e.g. S5'... .urthermore #ou must make sure the commands in the script do not allo1
interacti%e shell escapesY Then there is the umask 1hich 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 1e get the impression setuid shell scripts are Euite a risk# 8usinessY You ma# 8e
8etter off 1riting a C program insteadY
6.&= 5o1 can 9 find out 1hich 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 1ill tell #ou %arious things a8out processes using particular files.
$ port of the 6./ ,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 1orks on Euite a fe1 s#stems.
9nstructions for o8taining kstuff are pro%ided in Euestion /.*+.
9J%e 8een informed that there is also a program called lsof. 9 donJt kno1 1here it can 8e
o8tained.
2ichael .ink Z2ichael..inkaui8k.ac.atV addsK
9f #ou are una8le to unmount a file s#stem for 1hich 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<*==.
6.)= 5o1 do 9 keep track of people 1ho are fingering meB
3enerall#? #ou canJt find out the userid of someone 1ho is fingering #ou from a remote
machine. You ma# 8e a8le to find out 1hich 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 1ill open #our .plan file for 1riting\ the open 1ill 8lock
until some other process <namel# fingerd= opens the .plan for reading. 7o1 #ou can feed
1hate%er #ou 1ant through this pipe? 1hich lets #ou sho1 different .plan information e%er#
time someone fingers #ou. 4ne program for doing this is the RplannerS package in %olume
6* of the comp.sources.misc archi%es.
4f course? this ma# not 1ork 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 1here
an incoming finger connection is coming from? 8ut this 1onJt get #ou the remote user.
3etting the remote userid 1ould reEuire that the remote site 8e running an identit# ser%ice
such as R.C )/*. There are no1 three R.C )/* implementations for popular ,S! machines?
and se%eral applications <such as the 1uarchi%e ftpd= supporting the ser%er.
.or more information Noin the rfc)/*-users mailing list?
Vrfc)/*-users-reEuestakramden.acf.n#u.edu.
*"6
There are three ca%eats relating to this ans1er. The first is that man# 7.S s#stems 1onJt
recogniFe the named pipe correctl#. This means that tr#ing to read the pipe on another
machine 1ill either 8lock until it times out? or see it as a Fero-length 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 1ill 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 - check the kernel config file and
.9.4C7T option. 9f the num8er of pipes on the s#stem e(ceeds the .9.4C7T %alue? the
s#stem 8locks ne1 pipes until some8od# frees the resources. The reason for this is that
8uffers are allocated in a non-paged memor#.
6.*+= 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. 5o1e%er? there are three freel# redistri8uta8le
packages 1hich can 8e used to start processes in such a 1a# that the# can 8e later reattached
to a terminal.
The first is Rscreen?S 1hich is descri8ed in the comp.sources.uni( archi%es as RScreen?
multiple 1indo1s on a CRTS <see the Rscreen-/.-S package in comp.sources.misc? %olume
-&.= This package 1ill run on at least ,S!? S#stem V r/.- and SC4 U79;.
The second is Rpt#?S 1hich 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 -/=. pt# is designed for use under
,S!-like s#stem onl#.
The third is Rdislocate?S 1hich is a script that comes 1ith the
e(pect distri8ution. Unlike the pre%ious t1o? this should run on all U79; %ersions. !etails
on getting e(pect can 8e found in Euestion /.) .
7one 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.
6.**= 9s it possi8le to Rsp#S on a terminal? displa#ing the output
thatJs appearing on it on another terminalB
There are a fe1 different 1a#s #ou can do this? although none of them is perfectK
ki8itF allo1s t1o <or more= people to interact 1ith a shell <or an# ar8itar# program=.
Uses includeK
1atching or aiding another personJs terminal session\
recording a con%ersation 1hile retaining the a8ilit# to scroll 8ack1ards? sa%e the
con%ersation? or e%en edit it 1hile in progress\
teaming up on games? document editing? or other cooperati%e tasks 1here each
person has strengths and 1eakness that complement one another.
*"5
ki8itF comes as part of the e(pect distri8ution. See Euestion /.).
ki8itF reEuires permission from the person to 8e sp#ed upon. To sp# 1ithout permission
reEuires less pleasant approachesK
You can 1rite a program that rummages through Dernel structures and 1atches 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 1ho does not ha%e
e(perience 1orking 1ith the Uni( kernel. .urthermore? 1hate%er method #ou come up
1ith 1ill pro8a8l# 8e Euite non-porta8le.
9f #ou 1ant to do this to a particular hard-1ired terminal all the time <e.g. if #ou 1ant
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 some1here 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 1ire? although if #ou splice onl# into the computer-
Vterminal 1ires? this isnJt much of a pro8lem. This is not something that should 8e
attempted 8# an#one 1ho is not %er# familiar 1ith terminal 1iring and such.
The latest %ersion of screen includes a multi-user mode.
Some details a8out screen can 8e found in Euestion 6.*+.
9f the s#stem 8eing used has streams <Sun4S? SVR6=? the ad%ise program that 1as posted
in %olume -& of comp.sources.misc can 8e used. $7! 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 1hene%er a tt# or pt# is opened=.
Uni" ' $re<uently Asked >uestions 0;1 ?$re<uent
posting@
This article includes ans1ers toK
5.*= Can shells 8e classified into categoriesB
5.-= 5o1 do 9 RincludeS one shell script from 1ithin another shell scriptB
5./= !o all shells ha%e aliasesB 9s there something else that can 8e usedB
5.6= 5o1 are shell %aria8les assignedB
5.5= 5o1 can 9 tell if 9 am running an interacti%e shellB
5.6= 0hat RdotS files do the %arious shells useB
5."= 9 1ould like to kno1 more a8out the differences 8et1een the %arious shells. 9s this
information a%aila8le some placeB
9f #ouJre looking for the ans1er to? sa#? Euestion 5.5? and 1ant to skip e%er#thing else? #ou can
search ahead for the regular e(pression R]5.5=S.
0hile these are all legitimate Euestions? the# seem to crop up in comp.uni(.Euestions or
comp.uni(.shell on an annual 8asis? usuall# follo1ed 8# plent# of replies <onl# some of 1hich are
correct= and then a period of griping a8out ho1 the same Euestions keep coming up. You ma#
*"6
also like to read the monthl# article R$ns1ers to .reEuentl# $sked GuestionsS in the ne1sgroup
Rne1s.announce.ne1usersS? 1hich 1ill tell #ou 1hat RU79;S stands for.
0ith the %ariet# of Uni( s#stems in the 1orld? itJs hard to guarantee that these ans1ers 1ill 1ork
e%er#1here. Read #our local manual pages 8efore tr#ing an#thing suggested here. 9f #ou ha%e
suggestions or corrections for an# of these ans1ers? please send them to to tmatimaraisgtec.com.
5.*= Can shells 8e classified into categoriesB
9n general there are t1o main class of shells. The first class are those shells deri%ed from the
,ourne shell 1hich 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 1hich most people consider to
8e in a Rclass 8# itselfS although some people might argue that rc 8elongs in the ,ourne shell
class.
0ith the classification a8o%e? using care? it is possi8le to 1rite scripts that 1ill 1ork for all
the shells from the ,ourne shell categor#? and 1rite other scripts that 1ill 1ork for all of the
shells from the C shell categor#.
5.-= 5o1 do 9 RincludeS one shell script from 1ithin 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./= !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 1ork the same 1a#. .or
e(ample? some donJt accept arguments.
$lthough not strictl# eEui%alent? shell functions <1hich 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(? 1hich includes S#stem 999 and ,S! 6.-. ,S! 6./ and S#stem V shells do support shell
functions.
Use unalias to remo%e aliases and unset to remo%e functions.
5.6= 5o1 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$,'^7$2'S to place the %aria8le into the en%ironment. To
get rid of the %aria8les use unset.
*""
5.5= 5o1 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*? ho1e%er? it is 8etter to
check the %aria8le L-. 9f L- contains an [iJ? the shell is interacti%e. Test like soK
case L- in
i= M do things for interacti%e shell
\\
A= M do things for non-interacti%e shell
\\
esac
5.6= 0hat 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#stem-1ide .cshrc and .login files. '%er# %ersion puts them in different
places.
Start-up <in this order=K
.cshrc - al1a#s\ unless the -f option is used.
.login - login shells.
Upon terminationK
.logout - login shells.
4thersK
.histor# - sa%es the histor# <8ased on Lsa%ehist=.
tcsh
Start-up <in this order=K
OetcOcsh.cshrc - al1a#s.
OetcOcsh.login - login shells.
.tcshrc - al1a#s.
.cshrc - if no .tcshrc 1as present.
.login - login shells
Upon terminationK
.logout - login shells.
4thersK
.histor# - sa%es the histor# <8ased on Lsa%ehist=.
.cshdirs - sa%es the director# stack.
sh
*"&
Start-up <in this order=K
OetcOprofile - login shells.
.profile - login shells.
Upon terminationK
an# command <or script= specified using the commandK
trap RcommandS +
ksh
Start-up <in this order=K
OetcOprofile - login shells.
.profile - login shells\ unless the -p option is used.
L'7V - al1a#s? if it is set\ unless the -p option is used.
OetcOsuid^profile - 1hen the -p option is used.
Upon terminationK
an# command <or script= specified using the commandK
trap RcommandS +
8ash
Start-up <in this order=K
OetcOprofile - login shells.
.8ash^profile - login shells.
.profile - login if no .8ash^profile is present.
.8ashrc - interacti%e non-login shells.
L'7V - al1a#s? if it is set.
Upon terminationK
.8ash^logout - login shells.
4thersK
.inputrc - Readline initialiFation.
Fsh
Start-up <in this order=K
.Fshen% - al1a#s? unless -f is specified.
.Fprofile - login shells.
.Fshrc - interacti%e shells? unless -f is specified.
.Flogin - login shells.
Upon terminationK
.Flogout - login shells.
rc
Start-upK
.rcrc - login shells
*")
5."= 9 1ould like to kno1 more a8out the differences 8et1een 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.u1p.edu <-+6.)5.*6-.*)+=Kpu8O%iOdocsOshell-*++.,eta$.U
utsun.s.u-tok#o.ac.NpKmiscO%i-archi%eOdocsOshell-*++.,eta$.U
This file compares the flags? the programming s#nta(? inputOoutput redirection? and
parametersOshell en%ironment %aria8les. 9t doesnJt discuss 1hat dot files are used and the
inheritance for en%ironment %aria8les and functions.

*&+

Anda mungkin juga menyukai