Appendix C APPENDIX C
Important Unix Commands
Modern Unix systems come with hundreds and hundreds of commands. Many of
them are specialized, but many are also generally useful, both in everyday interactive
use and in shell scripts. It’s impossible to cover every program on every system in
existence, nor would that be useful. (Although books like Unix in a Nutshell make a
valiant effort to describe a large cross section of what’s out there.)
It is possible, however, to identify certain valuable commands, the ones that a Unix
user or programmer should come to understand first, before moving on to the rest of
the programs out there. Not surprisingly, many of these are the older commands that
have been around since the early days of Unix. This appendix is our recommended
list of commands that you should go out and study in order to improve your skills as
a Unix developer. For brevity, we have resorted to simple, sorted, tabular lists of
commands.
Shells and Built-in Commands
First and foremost, it pays to understand the Bourne shell language, particularly as
codified by POSIX. Both bash and ksh93 are POSIX-compliant, and several other
shells are compatible syntactically with the Bourne shell:
bash The GNU Project’s Bourne-Again Shell.
ksh The Korn shell, either an original or clone, depending upon the operating system.
pdksh The Public Domain Korn shell.
sh The original Bourne shell, particularly on commercial Unix systems.
zsh The Z-shell.
Along similar lines, you should understand the way the shell’s built-in commands
work:
. Read and execute a given file, in the current shell.
break Break out of a for, select, until, or while loop.
cd Change the current directory.
473
This is the Title of the Book, eMatter Edition
Copyright © 2011 O’Reilly & Associates, Inc. All rights reserved.
command Bypass the search for functions to run a regular built-in command.
continue Start the next iteration of a for, select, until, or while loop.
eval Evaluate given text as a shell command.
exec With no arguments, change the shell’s open files. With arguments, replace the shell with another
program.
exit Exit a shell script, optionally with a specific exit code.
export Export a variable into the environment of subsequent programs.
false Do nothing, unsuccessfully. For use in shell loops.
getopts Process command-line options.
read Read lines of input into one or more shell variables.
readonly Mark a variable as read-only; i.e., unchangeable.
return Return a value from a shell function.
set Print shell variables and values; set shell options; set the command-line parameters ($1, $2, …).
shift Move the command-line parameters down by one or more.
test Evaluate expressions, which may be string-, numeric-, or file-attribute-related.
trap Manage operating system signals.
true Do nothing, successfully. For use in shell loops.
type Indicate the nature of a command (keyword, built-in, external, etc.).
typeset Declare variables and manage their type and attributes.
ulimit Set or display various per-process system-imposed limits.
unset Remove shell variables and functions.
The following commands are useful in day-to-day shell scripting:
basename Print the last component of a pathname, optionally removing a suffix. Mainly used in command
substitution.
dirname Print all but the last component of a pathname. Mainly used in command substitution.
env Manipulate the environment of a command.
id Print user and group ID and name information.
date Print the current date and time, optionally under control of a user-supplied format string.
who Print a list of logged-on users.
stty Manipulate the state of the current terminal device.
Text Manipulation
The following commands are used for text manipulation:
awk An elegant and useful programming language in its own right, it is also an important component
of many large shell scripts.
cat Concatenate files.
cmp Simple file comparison program.
cut Cut out selected columns or fields.
dd A more specialized program for blocking and unblocking data, and converting between ASCII and
EBCDIC. dd is especially good for making raw copies of device files. Note that iconv is a better
program for doing character set conversions.
474 | Appendix C: Important Unix Commands
This is the Title of the Book, eMatter Edition
Copyright © 2011 O’Reilly & Associates, Inc. All rights reserved.
echo Print arguments to standard output.
egrep Extended grep. Matching uses Extended Regular Expressions (EREs).
expand Expand tabs to spaces.
fgrep Fast grep. This program uses a different algorithm than grep for matching fixed strings. Most,
but not all, Unix systems can search simultaneously for multiple fixed strings.
fmt Simple tool for formatting text into paragraphs.
grep From the original ed line editor’s command g/re/p, “Globally match RE and Print.” Matching
uses Basic Regular Expressions (BREs).
iconv General-purpose character-encoding conversion tool.
join Join matching records from multiple files.
less A sophisticated interactive pager program for looking at information on a terminal, one screenful
(or “page”) at a time. It is now available from the GNU Project. The name is a pun on the more pro-
gram.
more The original BSD Unix interactive pager program.
pr Format files for printing on line printers.
printf A more sophisticated version of echo that provides control over the way each argument is printed.
sed A stream editor, based on the original ed line editor’s command set.
sort Sort text files. Command-line arguments provide control over sort key specification and priority.
spell A batch spellchecker. You may have aspell or ispell, with a shell script wrapper named
spell, instead.
tee Copy standard input to standard output and to one or more named output files.
tr Transliterate, delete, or reduce runs of duplicate characters.
unexpand Convert runs of spaces into the appropriate number of tabs.
uniq Remove or count duplicate lines in sorted input.
wc Count lines, words, characters, and/or bytes.
Files
The following commands work with files:
bzip2, bunzip2 Very high quality file compression and decompression.
chgrp Change the group of files and directories.
chmod Change the permissions (mode) of files and directories.
chown Change the owner of files and directories.
cksum Print a file checksum, POSIX standard algorithm.
comm Print or omit lines that are unique or common between two sorted files.
cp Copy files and directories.
df Show free disk space.
diff Compare files, showing differences.
du Show disk block usage of files and directories.
file Guess the type of data in a file by examining the first part of it.
find Descend one or more directory hierarchies finding filesystem objects (files, directories, special
files) that match specified criteria.
gzip, gunzip High-quality file compression and decompression.
Files | 475
This is the Title of the Book, eMatter Edition
Copyright © 2011 O’Reilly & Associates, Inc. All rights reserved.
head Print the first n lines of one or more files.
locate Find a file somewhere on the system based on its name. The program uses a database of files
that is usually rebuilt automatically, nightly.
ls List files. Options control the information shown.
md5sum Print a file checksum using the Message Digest 5 (MD5) algorithm.
mkdir Make directories.
mktemp Create a unique temporary file and print its name. Not universally available.
od Octal dump; print file contents in octal, hexadecimal, or as character data.
patch Update the contents of a given file to a newer version by reading the output of diff.
pwd Print the current working directory. Usually built into modern shells.
rm Remove files and directories.
rmdir Remove just empty directories.
strings Search binary files for printable strings and print them.
tail Show the last n lines of a file. With -f, keep printing the (growing) contents of the file.
tar Tape archiver. Now used mostly as a software distribution format.
touch Update the modification or access time of a file.
umask Set the default file-creation permissions mask.
zip, unzip File archiver and compressor/decompressor. The ZIP format is portable across a broad range of
operating systems.
Processes
The following commands create, remove, or manage processes:
at Executes jobs at a specified time. at schedules jobs to be executed just once, whereas cron schedules
them to be executed regularly.
batch Executes jobs when the system is not too overloaded.
cron Executes jobs at specified times.
crontab Edit per-user “cron table” files that specify what commands to run, and when.
fuser Find processes using particular files or sockets.
kill Send a signal to one or more processes.
nice Change the priority of a process before starting it.
ps Process status. Print information about running processes.
renice Change the priority of a process that has already been started.
sleep Stop execution for the given number of seconds.
top Interactively display the most CPU-intensive jobs on the system.
wait Shell built-in command to wait for one or more processes to complete.
xargs Read strings on standard input, passing as many as possible as arguments to a given command. Most
often used together with find.
Miscellaneous Programs
There’s always a “miscellaneous” category:
476 | Appendix C: Important Unix Commands
This is the Title of the Book, eMatter Edition
Copyright © 2011 O’Reilly & Associates, Inc. All rights reserved.
cvs The Concurrent Versions System, a powerful source-code management program.
info The GNU Info system for online documentation.
locale Print information about available locales.
logger Send messages to system logs, usually via syslog(3).
lp, lpr Spool files to a printer.
lpq Show the list of print jobs in progress and waiting in the queue.
mail Send electronic mail.
make Control compilation and recompilation of files.
man Print the online manual page(s) for commands, library functions, system calls, devices, file formats,
and administrative commands.
scp Secure remote copy of files.
ssh Secure shell. Provide an encrypted connection between machines for program execution or interac-
tive login.
uptime Tell how long the system has been up, and show system load information.
Also in the miscellaneous category are the commands for the Revision Control Sys-
tem (RCS):
ci Check in a file to RCS.
co Check out a file from RCS.
rcs Manipulate a file that is under RCS control.
rcsdiff Run diff on two different versions of a file controlled by RCS.
rlog Print the check-in log for one or more RCS-managed files.
Miscellaneous Programs | 477
This is the Title of the Book, eMatter Edition
Copyright © 2011 O’Reilly & Associates, Inc. All rights reserved.