Unix Systems: Some More Useful Commands
Unix Systems: Some More Useful Commands
Bob Dowling
[email protected]
20 July 2006
This is a one afternoon course given by the University Computing Service as part of its Unix
Systems series of courses. It presumes that the reader has either attended the Unix
Systems: Introduction course or has equivalent experience. The introductory course teaches
the use of the commands ls, mv, mkdir etc. This course introduces a further set of
commands.
Table of Contents
The locate command...................................................................................................................2
Your own locate database.......................................................................................................2
The find command.......................................................................................................................4
Types of node..........................................................................................................................4
Names of nodes......................................................................................................................4
Combining tests......................................................................................................................5
Sizes of files............................................................................................................................5
Running commands................................................................................................................6
Summary of options covered..................................................................................................6
The du command....................................................................................................................7
The ps program...........................................................................................................................9
Process selection options........................................................................................................9
Simple column selection options..........................................................................................10
Detailed column selection.....................................................................................................11
The kill command......................................................................................................................12
The top command......................................................................................................................15
The watch command.................................................................................................................17
The terminal window................................................................................................................19
Changing the size of text......................................................................................................19
More terminals.....................................................................................................................20
Scrolling...............................................................................................................................20
Changing the prompt................................................................................................................21
Terminal names.........................................................................................................................22
Page 1 of 22
Page 3 of 22
./OpenOffice.org1.1/setup
./OpenOffice.org1.1/soffice
./OpenOffice.org1.1/spadmin
$
Types of node
A simple test that find offers is on the type of the file. If we run the command
find . -type d -print then find runs through every file and directory under the current
working directory and on each one it runs the test -type d which means is it a directory?
If the object in question is not a directory then the processing of that node in the filesystem
stops immediately and find moves on to the next in its list. It is is a directory then find
moves on to its next test, which is the pseudo-test -print in this case, so it prints the name
of the node. What this does it to find and print the names of all directories at or below the
current one.
$ find . -type d -print
.
./.pwf-linux
./.gconfd
./.gconfd/lock
./.gconf
./.gconf/desktop
./OpenOffice.org1.1/share/uno_packages
./OpenOffice.org1.1/program
./OpenOffice.org1.1/program/addin
$
Names of nodes
A similar test checks the name of a node in the file system against a shell-style regular
expression. If we run the command find . -name '[A-Z]*' -print then any node below
the current working directory will be checked to see if its name starts with a capital letter. If
it does then it will be printed. We can combine tests to find just the directories below the
Page 4 of 22
./OpenOffice.org1.1/user/registry/data/org/openoffice/ucb/Store.xcu
./OpenOffice.org1.1/user/registry/data/org/openoffice/ucb/Hierarchy.xcu
./OpenOffice.org1.1/THIRDPARTYLICENSEREADME.html
$
Case-insensitive searching can be done by using the option -iname in place of -name.
Combining tests
Now that we have two distinct real tests we can illustrate combining them. The following test
checks every node to see whether it is a directory and, if it is, whether it starts with a letter
between A and Z.
$ find . -type d -name '[A-Z]*' -print
./.Trash/OpenOffice.org1.1
./.Trash/OpenOffice.org1.1/user/basic/Standard
./.Trash/OpenOffice.org1.1/user/registry/data/org/openoffice/Office
./OpenOffice.org1.1
./OpenOffice.org1.1/user/basic/Standard
./OpenOffice.org1.1/user/registry/data/org/openoffice/Office
$
Sizes of files
Another very useful test is to be able to identify files by size and the -size option does this. A
slightly subtlety is required, though. The option -size 100k will match files that are exactly
100KB in size and it's the option -size +100k that will find those larger than 100KB, which is
probably what was wanted. Finally, the option -find -100k will find those smaller than
100KB.
$ find . -type f -size +500k -print
./.adobe/AdobeFnt06.lst.pcphxtr01
./.adobe/AdobeFnt06.lst.smaug
./.mozilla/default/25p8sbwm.slt/XUL.mfasl
./.mozilla/firefox/dy8ua6ci.default/XUL.mfasl
./.openoffice/instdb.ins
./.OpenOffice.org/instdb.ins
./chile/sshot1.ps
./chile/sshot2.ps
./chile/sshot3.ps
./DiscMaths.ps
./FortranNag/nag_lib_support.mod
$
The k at the end of the size stands for KB (kilobytes). Similarly you can use 500M for
500MB and 500G for 500GB. If you want to specify an exact size then use c to mean
characters as b is already taken to mean the quite useless blocks. A block is a 512-byte
Page 5 of 22
Running commands
In addition to just printing a node's name it is also possible to get find to run a command on
the matching node with the -exec option. Its syntax is rather baroque, though. Suppose we
want to run wc -l on every file that ends in .html. The command we need to run is
find . -type f -name '*.html' -exec wc -l {} \;.
$ find . -type f -name '*.html' -exec wc -l {} \; | more
72 ./.mozilla/firefox/dy8ua6ci.default/bookmarks.html
114 ./.openoffice/LICENSE.html
368 ./.openoffice/README.html
157 ./.OpenOffice.org/LICENSE.html
100 ./.OpenOffice.org/README.html
496 ./.OpenOffice.org/THIRDPARTYLICENSEREADME.html
38 ./PWF-Linux talk/swrules.html
32 ./PWF-Linux talk/wstncost.html
27 ./PWF-Linux talk/wstnfor.html
27 ./PWF-Linux talk/wstnis.html
$
The bizarre hieroglyphs after the -exec demand some explanation. Immediately following
the option is the command to be run, with any options it might have itself. Within these
options the token {} is expanded into the full filename that has been matched and the \;
marks the end of the command. Note that the space before the \; must be there.
-type
-print
-exec
reg.exp.
reg.exp.
XX
+XX
-XX
Xc
Xk
XM
XG
f
d
l
command
{}
\;
Page 6 of 22
The du command
A very common file to look for is the big one that's eating up my quota. We have seen the
-size option on find to help us here but there are other commands available to us also.
On the subject of subject of file sizes the other common question is how much space does
this directory take up? Both these questions can be answered with the du command.
The du command (du stands for disc use) indicates how much space is taken up by a file
or, more typically, a directory's contents. To see what it does change directory to
/ux/Lessons/FurtherUnix/dudemo and run the du command.
$ cd /ux/Lessons/FurtherUnix/dudemo
$ pwd
/ux/Lessons/FurtherUnix/dudemo
$ du
18
21
18
19
17
17
443
17
18
19
18
18
18
453
10643
./alpha/alpha
./alpha/beta
./alpha/gamma
./alpha/delta
./alpha/psi
./alpha/omega
./alpha
./beta/alpha
./beta/beta
./beta/gamma
./beta/delta
./omega/psi
./omega/omega
./omega
.
$
The command runs through the directory tree quoting each low level directory (alpha/alpha,
alpha/beta, alpha/gamma, etc.) first and then giving a total for the mid-level directory (alpha,
beta, etc.) and finally the the top-level directory, . (the current working directory).
If we just want to know the intermediate sizes (for example to find the directory tree
containing the large file) we can ask for just the summary information for the quoted directory
with the -s (for summary) option:
$ du -s *
443
alpha
448
beta
436
chi
449
upsilon
438
xi
447
zeta
$
Of course we can ask the same question about the top level directory:
$ du -s .
11390
.
$
Page 7 of 22
450
kappa
453
omega
1187
lambda
$ du -s * | sort --numeric --reverse
1187
lambda
453
omega
450
kappa
435
omicron
435
eta
433
delta
$
We can use this trick to tunnel into a directory tree to find the over-large file. (The head
function chops off the first few lines of output.)
$ du -s * | sort --numeric --reverse | head -3
1187
lambda
453
omega
450
kappa
$ cd lambda/
$ du -s * | sort --numeric --reverse | head -3
767
chi
21
alpha
20
theta
$ cd chi/
$ du -s * | sort --numeric --reverse | head -3
748
psi
1
zeta
1
xi
$ ls -l psi
-rw-r--r-- 1 rjd4 rjd4 765432 2006-02-05 16:19 psi
$
Page 8 of 22
The ps program
This course assumes you already have some basic knowledge of the ps command. The ps
program has just a few options that you actually use from day to day. It has many other
options that you might use once in your life or in the middle of a particular shell script.
The options we will consider can be split into two classes: process selection options,
controlling which processes should be reported on, and display formatting options which
control how the data about the selected processes should be displayed.
Every process
-U user
-G group
-p pid1,pid2,pid3
-t terminal
$ ps -e
PID TTY
1 ?
2 ?
3 ?
10364 ?
10370 ?
10400 ?
10424 ?
10435 ?
10442 pts/8
952 pts/8
TIME
00:00:09
00:00:00
00:00:00
CMD
init
migration/0
ksoftirqd/0
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
sshd
pwfacmd
mount.ncpfs
automount
mount.ncp
bash
ps
$
Note that ps can spot itself running as PID 952 in the screenshot above.
$ ps -U rjd4
PID TTY
10400 ?
10435 ?
10442 pts/8
10461 ?
939 ?
963 pts/8
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
mount.ncpfs
mount.ncp
bash
mount.ncp
mount.ncp
ps
$
and as 963 in this one
$ ps -p 963,10442
PID TTY
TIME CMD
10442 pts/8
00:00:00 bash
$
and not as 963 in this one which is why only one line is listed when two PIDs were asked for.
This third run of ps has its own PID.
Page 9 of 22
TIME CMD
00:00:00 bash
00:00:00 ps
$
Finally, we have a listing of all the processes running in this terminal. There is an appendix to
these notes explaining the structure of terminal names if you are interested.
Full output
-l
Long output
--forest
Full output:
$ ps -U rjd4 -f
UID
root
root
rjd4
root
root
rjd4
PID PPID
10400
1
10435
1
10442 10364
10461
1
939
1
1222 10442
C
0
0
0
0
0
0
STIME
Jul10
Jul10
Jul10
Jul10
09:46
10:19
TTY
?
?
pts/8
?
?
pts/8
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
ncpd
ncpd
-bash
ncpd
ncpd
ps -U rjd4 -f
$
Long output:
$
F
5
5
4
5
5
0
ps -U rjd4 -l
S
UID
PID PPID
S
0 10400
1
S
0 10435
1
S 2049 10442 10364
S
0 10461
1
S
0
939
1
R 2049 1374 10442
C PRI
0 75
0 75
0 75
0 75
0 75
0 76
NI
0
0
0
0
0
0
ADDR SZ
464
481
- 1107
481
481
673
WCHAN
wait
-
$
Tree output:
$ ps -U rjd4 --forest
PID TTY
TIME CMD
10442 pts/8
00:00:00 bash
1385 pts/8
00:00:00 \_ ps
939 ?
00:00:00 mount.ncp
Page 10 of 22
TTY
?
?
pts/8
?
?
pts/8
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
mount.ncpfs
mount.ncp
bash
mount.ncp
mount.ncp
ps
00:00:00 mount.ncp
00:00:00 mount.ncp
00:00:00 mount.ncpfs
cmd
the command
pcpu
time
stime
start time
pmem
rss
user
user
uid
numeric user ID
group
group
gid
numeric group ID
pid
process ID
ppid
parent process ID
tty
controlling terminal
Page 11 of 22
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
mount.ncpfs
mount.ncp
bash
mount.ncp
mount.ncp
xclock
ps
$
So it's process number 1521. (Your number will differ, obviously.) Now we will kill it off from
the command line:
$ kill 1521
$
[1]+ Terminated
xclock -update 1
$
The kill command has lived up to its name; it has killed the xclock process. The
terminated line is the shell notifying you that a backgrounded job has finished and is not the
output from kill which produced no output of its own. We can see this most easily by running
the xclock from one terminal window and the kill in another.
1st terminal window
xclock -update 1
$
What the kill command actually did was to send a signal to the xclock process. A signal
is an asynchronous message, and Unix programs are written to deal with these messages out
of the blue. (Actually they tend to follow a set of default behaviours because writing your own
signal handler is difficult.)
By default the kill program sends a terminate signal (also known by its capitalized
abbreviation TERM) to the process. We could equally well have written this:
Page 12 of 22
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
mount.ncpfs
mount.ncp
bash
mount.ncp
mount.ncp
xclock
ps
Killed
xclock -update 1
$
The KILL signal cannot be ignored by a process or have its behaviour changed by the
program's author. This is the drop dead now signal. The process gets no opportunity to put
its affairs in order. It just has to die.
If you ever have to kill a process, always start with TERM (the default) and only proceed to
KILL if the process hasn't ended in the ten seconds or so following the TERM. If KILL doesn't
work then there is nothing you can do. Something has gone wrong at the system level and
you can't do anything about it without system administration privileges and even then that
may not be sufficient.
Note also that you can't kill processes that don't belong to you:
$ ps -U ntp
PID TTY
6305 ?
TIME CMD
00:00:00 ntpd
Page 13 of 22
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
mount.ncpfs
mount.ncp
bash
mount.ncp
mount.ncp
xclock
ps
Interrupt
xclock -update 1
$
More interestingly there are also signals to pause and restart a process: STOP and CONT
(continue). Note that the second hand stops moving between the STOP and CONT signals
(which is rather hard to show in printed notes).
$ xclock -update 1 &
[1] 1716
$ kill -STOP 1716
$
[1]+
Stopped
xclock -update 1
$
$
$ kill -CONT 1716
$
There are plenty more signals but the rest don't really concern us. If you want to see them all
run kill with the -l (for list) option:
$ kill -l
1) SIGHUP
5) SIGTRAP
9) SIGKILL
13) SIGPIPE
18) SIGCONT
22) SIGTTOU
26) SIGVTALRM
30) SIGPWR
36) SIGRTMIN+2
40) SIGRTMIN+6
44) SIGRTMIN+10
48) SIGRTMIN+14
52) SIGRTMAX-12
56) SIGRTMAX-8
60) SIGRTMAX-4
64) SIGRTMAX
2)
6)
10)
14)
19)
23)
27)
31)
37)
41)
45)
49)
53)
57)
61)
SIGINT
SIGABRT
SIGUSR1
SIGALRM
SIGSTOP
SIGURG
SIGPROF
SIGSYS
SIGRTMIN+3
SIGRTMIN+7
SIGRTMIN+11
SIGRTMIN+15
SIGRTMAX-11
SIGRTMAX-7
SIGRTMAX-3
3)
7)
11)
15)
20)
24)
28)
34)
38)
42)
46)
50)
54)
58)
62)
SIGQUIT
SIGBUS
SIGSEGV
SIGTERM
SIGTSTP
SIGXCPU
SIGWINCH
SIGRTMIN
SIGRTMIN+4
SIGRTMIN+8
SIGRTMIN+12
SIGRTMAX-14
SIGRTMAX-10
SIGRTMAX-6
SIGRTMAX-2
4)
8)
12)
17)
21)
25)
29)
35)
39)
43)
47)
51)
55)
59)
63)
SIGILL
SIGFPE
SIGUSR2
SIGCHLD
SIGTTIN
SIGXFSZ
SIGIO
SIGRTMIN+1
SIGRTMIN+5
SIGRTMIN+9
SIGRTMIN+13
SIGRTMAX-13
SIGRTMAX-9
SIGRTMAX-5
SIGRTMAX-1
Page 14 of 22
abcd3
root
root
root
root
PR
NI
VIRT
RES
TIME+
COMMAND
39
16
RT
34
RT
The information in the top five lines is generic system information. The following block has
one line per process.
We can restrict the jobs shown to a single user, typically oneself. To select a user, press the
u key. Do not press the Return key. The blank line between the generic system information
and the per-process table should prompt for the user wanted:
top - 12:31:10 up 12 days, 17:50, 5 users, load average: 1.03, 1.10, 1.08
Tasks: 107 total,
2 running, 105 sleeping,
0 stopped,
0 zombie
Cpu(s): 0.1% us, 0.4% sy, 31.3% ni, 68.2% id, 0.1% wa, 0.0% hi, 0.0% si
Mem:
2075180k total, 2024076k used,
51104k free,
71060k buffers
Swap: 2097144k total,
2692k used, 2094452k free,
734160k cached
Which user (blank for all): rjd4
PID
32601
1
2
3
USER
stjm2
root
root
root
PR
39
16
RT
34
After entering the user ID, press Return to get just that user's processes:
top - 12:33:42 up 12 days, 17:52, 5 users, load average: 1.00, 1.06, 1.07
Tasks: 107 total,
2 running, 105 sleeping,
0 stopped,
0 zombie
Cpu(s): 0.0% us, 0.0% sy, 27.1% ni, 72.8% id, 0.2% wa, 0.0% hi, 0.0% si
Mem:
2075180k total, 2024076k used,
51104k free,
71060k buffers
Swap: 2097144k total,
2692k used, 2094452k free,
734160k cached
PID USER
PR
NI
10442 rjd4
2610 rjd4
15
16
0
0
VIRT
RES
0.0
0.0
0.1
0.0
TIME+
COMMAND
0:00.18 bash
0:00.23 top
We can also change the number of seconds between updates by pressing the s key, the
number of seconds and then Return. On a heavily used, multi-user system is is regarded as
polite to increase the interval to at least 10 seconds.
Page 15 of 22
f,o
F or O
<,>
R
c,i,S
x,y
z,b
u
n or #
.
.
.
.
.
.
.
.
.
k,r
d or s
W
q
Page 16 of 22
Feb
5 13:15:02
$
We can run this under watch and get the results we would expect.
$ watch date
Now run it with an extra option on watch, the --differences option:
$ watch --differences date
Note how any output from the command that is different from the output of the previous run
of the command is highlighted in the output processed by watch, appearing in inverse video:
Every 2.0s: date
Feb
Page 17 of 22
5 13:43:31
Sun Feb
We can go further and ask for cumulative differences to be displayed (i.e. everything that's
different in the output from the first run of the command):
$ watch --differences=cumulative date
The highlighted area slowly grows over the command's output:
Every 2.0s: date
Sun Feb
Feb
5 13:49:24
The watch command in conjunction with ls is also useful to spot when files have stopped
growing. (e.g. log files from command runs, files being transferred in, etc.)
Page 18 of 22
Page 19 of 22
More terminals
You can get more terminals either by following the instructions for the first or by pressing
Ctrl+Shift+n on an existing one.
More interestingly, you can get another session as a tab on the original window by pressing
Ctrl+Shift+t:
You can switch between these by either clicking on the tab or by pressing Alt+1 for the first
tab, Alt+2 for the second, etc.
Scrolling
The scroll bar on the right hand side performs as one would expect but, in addition,
Shift+PgUp and Shift+PgDown will scroll up and down a windowful at a time.
Page 20 of 22
e.g.
\h
soup
\H
soup.linux.pwf.cam.ac.uk
\t
18:04:54
\T
06:04:54
\u
Current user
rjd4
\w
/home/rjd4/some/where
\W
where
\$
If you put the definition in single quotes then you can include spaces in it. Anything not
preceded with a backslash is taken literally.
If the export statement is put in a file .bashrc in your home directory it will be run every
time you log in.
Page 21 of 22
Terminal names
What is it with terminal names? If you look at the output from ps it quotes a terminal that the
process is running with, labelling the column TTY. The entries are either ? (no terminal) or
pts/N or sometimes ttyN. What do these mean?
The story dates back to the earlies, pre-internet days of Unix. Then a computer was a big
system with large numbers of terminals directly connected to it via serial links. These were
large paper-printing teletypes. That's what TTY stands for. Each of these teletypes had an
explicit number and were refered to in the Unix world by the name of a corresponding device
file: /dev/tty5 say. To read what was being entered at the teletype the system would read
from that file and to print text on the paper for that teletype the system would write to that
file.
On a modern Linux system there are still half a dozen or so teletypes defined. These are the
plain text interfaces you can get by pressing Ctrl+Alt+Fn (for n between 1 and 6). Press
Ctrl+alt+F7 to get back to the graphical interface. Each of those text termina lprovides an
independent text login: a teletype.
The cosy world of big computing was disrupted by the arrival of the networked computer.
When a connection was made to a computer remotely there was no teletype, at least not on
the computer being connected to. Various tricks were attempted to get around this but
ultimately the pseudo-terminal was arrived at. This created devices that acted as teletype
devices for connections that weren't from locally attached teletypes. These are used both for
network connections and sessions within windows and now form the majority of connections
on a system. These pseudo-terminals come as pairs called masters and slaves and the process
is attached to the slave pseudo-terminal. The device for a pseudo-terminal slave is a pts.
Each pseudo-terminal gets a unique number and the device file for the slave is /dev/pts/n
for various n.
If you want to know what teletype or pseudo-terminal slave you are connected to, issue the
command tty:
$ tty
/dev/pts/8
$
Page 22 of 22