Additional Unix Utilities

Contents

cmp
diff
find
head
tail
ps
kill
lpr
lpq
lprm
sleep
grep
echo
sed
test
expr

Additional Unix utilities

There are about 200 commands that are supplied with every Unix system, many of them quite obscure. The Unix philosophy is to have lots of tools specialised to particular tasks rather than big tools that do everything.

cmp

Compare two files for differences. Returns true if they are the same. This command is only useful in shell scripts where a Boolean value is required from a file comparison
if cmp /etc/hosts /etc/hosts
then
  echo "Compare the same file..."
  echo Obviously you get true
fi

diff

Report the differences between two files.
cd /tmp
echo "line 1
line 2
line 4" > tmp1$$
echo "line 2
line 3" > tmp2$$
diff tmp1$$ tmp2$$
rm tmp1$$ tmp2$$

find

Find files that match a pattern and perform an action on them. This command searches for files recursively from a given directory. eg
find . -name "*" -print
find / -name core -exec rm {} \;

head

List the first lines of a file.
head -50 file

tail

List the last lines of a file.

ps

Report on the status of active processes. There are many options to this, to examine processes in various states.
# some of these options may be 
# invalid for your Unix version
ps -aex | more

kill

Kill an active process.
kill PID

lpr

Print a file.

lpq

List files in the print queue.

lprm

Remove a file from the print queue.

sleep

Pause the process for a period.
sleep 20

grep

Search for lines in a file containing a pattern.
grep pattern file
It prints each line in the file that matches the pattern. You won't believe how useful this is till you have a lot of files...
cat /etc/services
echo "-------------------------------
Now grepping for word \"time\"
-------------------------------"
grep time /etc/services
With multiple files, you also get the filename as well as the line matched: The -l option just tells you which files contain matching lines

echo

Echo a string to standard output.
echo "hello..."

sed

Stream editor. This is useful for on the fly editing, typically of small strings.
sed 1,10d file
prints file with lines 1-10 deleted
sed 20q file
prints first 20 lines and then quits
sed -n 20,30p file
-n turns off default printing, so only prints lines 20 to 30
sed 's/old/new/' file
prints file with occurrences of ``old'' changed to ``new''.

If no file is given, sed reads from standard input e.g. to remove the first header line from ps output

ps | sed 1d

test

Perform tests for existence, permissions, etc on files, and some arithmetic comparisons.
test -f file
test -r file
[ -f file ]
test 20 -lt 3
if test -d /bin
then
  echo "/bin is a directory"
fi

expr

Perform Boolean and arithmetic operations on integers and strings.
expr 20 + 3 
Home Program Development using Unix Home
Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Wed Nov 19 17:43:26 EST 1997
Copyright ©Jan Newmarch