Useful UNIX Commands
For more complete information use our help pages
The quick alphabetical list:
at awk cal
cancel cat cc
cd chmod cmp
comm cp csh
cut date diff
echo f77 find
grep kill ln
lp lpr lpstat
ls mail man
mesg mkdir mv
nohup nroff passwd
paste pg pr
ps pwd rm
rmdir sed sh
sort spell stty
tail tee time
tty uniq unzip
wc who write
zip Special
The slightly more organised list:
- File and directory manipulation
- cd chmod cp
find ln ls
mkdir mv pwd
rm rmdir unzip
zip
- File searching and editing
- awk cmp comm
cut diff find
grep paste sed
sort spell tail
uniq wc
- File display and printing
- cancel cat
lp lpr lpstat
nroff pg pr
- System checks & programming
- at cal cc
date echo f77
kill nohup passwd
ps
stty time tty
who
- Other
- csh mail mesg
sh tee write
Special
- HELP!
- man
command options argument
------- ------- --------
at -c,l,m,r,s time[date]
run job(s)/process(es) at a particular time[date]
e.g. at 03
at> ls > save.ls
at> pwd >> save.ls
at> ^D at 3 am, put a list of the
files and directories in the
pwd into 'save.ls' and append
this file with the pwd info
at -l show a list of the job(s) under
'at' control, with ID number
at -m 1445
at> clear
at> mywee.sh XXXXX > mws.result
at> ^D at 2:45 pm, run the shell
script 'mywee.sh' using XXXXX as
an argument and redirect the
output to 'mws.result'. Also send
an EMail to the user upon
completion
at -r 389012.a cancel the job with ID 389012
command options argument
------- ------- --------
awk -Fc -f file filename
similar to grep, with the advantage of being able to
find by fields within lines and to process the lines it finds
with further commands. Rather like grep coupled to find and
piped through a math function, like a spreadsheet in function.
e.g. awk -f seek.schmo poem uses the file of awk commands
called 'seek.schmo' and applies
them to 'poem'.
awk '/breath/ {print $0}' poem
finds lines in the file 'poem'
with the string 'breath' and
displays them on the terminal.
Note: {print $0} is default
action
awk -F\ '$1~/breath/ {print $1, $2}' poem
finds lines in the file 'poem'
with the first word (seen as a
field with a space delimiter)
containing the string 'breath'
and displays the first and
second words (fields)
awk '$3!~/nausea/ {print $2, $1, $6}' illness
finds lines in the file
'illness' with the third field
NOT containing 'nausea' and then
displays fields 2, 1, and 6, in
that order
awk -F: '$1~/item/ {tn=$3+$4+$5;tv=tn*$6;print $2, tn, tv}\
inventory
finds lines that list items
from the file 'inventory'. The
itemID ($2) is printed, as is the
total number of items in the
three warehouses ($3+$4+$5) and
the total value of these items
(tn*$6, where $6 is the item unit
cost). Notes: field delimiter
is full colon; semi-colon is used
to separate the three actions
awk 'END {print NR}' poem
goes to the end of the file
'poem' and prints out the total
number of lines. NR = Number of
Records, and is built in to AWK.
awk '$3 >= 15 {print $0}' inventory
finds the records with the
third field GE 15 and prints
them. Conditional pattern
selection.
awk '$1~/[A-F]*/&& $3 <= 150 {nd=150-$3;print "warehouse \
3 needs " nd " more of " $1}' inventory
finds the items beginning with
A through F and assesses the
stock requirements of warehouse
#3 for these items. && means
BOTH conditions must be met.
awk also has the following control commands for action statements:
if ( conditional ) statement [ else statement ]
while ( conditional ) statement
for ( expression ; conditional ; expression) statement
break
continue
{ [ statement ] ...}
variable=expression
print [ expression-list ] [ > expression ]
Sprintf format [ , expression-list ] [ > expression ]
next skip remaining patterns on this input line
exit skip the rest of the input
command options argument
------- ------- --------
cal - [month] year
show a calendar for a particular month or year
e.g. cal 05 1873
command options argument
------- ------- --------
cancel - job ID from lp
stops a printing job.
e.g. cancel 96 stops printing job ID 96
command options argument
------- ------- --------
cat - filename(s)
dump contents of file(s) to screen
e.g. cat Personal/form.txt
command options argument
------- ------- --------
cc -c,o filename(s).c (or .o)
compiles c programme(s) contained in 'filename.c(or .o)', and
generates an executable file, usually called a.out
e.g. cc -o works yes.c it.c compiles 'yes.c' and 'it.c' into
an executable file named 'works'
command options argument
------- ------- --------
cd - Dirname
change to another directory
e.g. cd Pictures
cd ../lib
command options argument
------- ------- --------
chmod ugo,+/-,rwx filename(s)
changes the protection and/or mode of operation of a file
e.g. chmod u+x test makes the file 'test' into an
executable one within a shell
for the user
chmod go-rw poem removes permission for the
group or the world to read or
write the file 'poem'
chmod o-rwx Poems no one outside you or the group
may read files in, write files to,
or even list the files in the your
Poems sub-directory
chmod o-x,g+w script others can't execute your file
'script', but your group can write
to it
command options argument
------- ------- --------
cmp -l file1 file2
compares the two files and reports the location of the first
difference found between them
e.g. cmp poem poem1
cmp -l poem poem1 shows all differences
cmp poem shows first difference between
what you enter and file 'poem'
command options argument
------- ------- --------
comm -1,2,3 file1 file2
compares the two files, but only if they have been sorted
first, and shows results in a three column table. Col1 lists
lines only in file1, col2 for file2, and col3 for lines in common.
e.g. comm -12 poem poem1 show only lines in common (col3)
command options argument
------- ------- --------
cp - filename(s) newfilename
filename(s) Dirname
copy file(s) into another filename or directory
e.g. cp 1st 2nd composite puts contents of '1st' and
'2nd' into the 'composite' file
cp assorted Misc copies 'assorted' into the
'Misc' subdirectory
command options argument
------- ------- --------
csh - -
make a c shell; exit with ^D
command options argument
------- ------- --------
cut -c,f -d filename(s)
cuts columns from a file and passes them along. Works either in
character mode or field mode. The -d option allows you to
change the default field delimiter from a .
e.g. cut -c1-4,7 cheese selects only characters
1 through 4 and 7 from each
line of 'cheese' and sends them
to the screen
cut -f2,5- report selects the delimited
fields 2, 5, 6, 7 ... (to the
last column) from 'report'
cut -f3,4,9 -d" " poem > texture
selects only words 3, 4 and 9
from 'poem', storing them in
the file 'texture'
command options argument
------- ------- --------
date - -
show the date and time
command options argument
------- ------- --------
diff -b,e file1 file2
compares two files and lists the changes required to make the
first identical to the second.
e.g. diff -b poem poem1 ignores blanks
diff -e poem poem1 lists differences as a series
of ex editor commands required
command options argument
------- ------- --------
echo - charstring;
env var; `command`
returns the argument to the screen; useful for descriptions in
shell scripts
e.g. echo Version 1.1
prints 'Version 1.1' to the screen
echo $HOME
prints the value of your home
directory
echo `grep nonsense *.html`
prints the lines from any "html"
files with the word "nonsense"
command options argument
------- ------- --------
f77 -c,o filename(s).f (or .o)
compiles fortran programme(s) contained in 'filename.f (or .o)',
and generates an executable file, usually called a.out
command
-------
find dirname search criteria actions
finds file(s) in specified directory (and all subdirectories)
matching the given pattern and performs an action upon them
e.g. find /home1/cc -name oops -print
finds all files named 'oops'
in the directory 'cc' or its sub-
directories, and shows the path
find . -name '*.[fc]' -print
finds all fortran and c files
in the current directory and
shows the path
find ./bin ./lib -atime +7 -ok rm {} \;
finds all files last accessed
over 7 days ago in the /bin and
/lib subdirectories, and
queries you, for each one, if
you want it removed
find . -mtime 1 -exec lpr -P2up {} \;
all the files you modified
yesterday are printed
find .. -size +2 ! -newer std.in -print
all the files in the directory
above the current on are
searched for those with sizes
greater than two blocks and
with updates NOT more recently
than std.in, and then their
paths are shown
command options argument
------- ------- --------
grep -n,c,v string [filename(s)]
search for 'string' in the files named and dumps lines with
'string' result to screen
e.g. grep -n silly poem finds 'silly' in the file 'poem'
showing the lines and their
numbers in the file, too
grep -c "h.s" Well/que finds all words such as his,
whose, hostile, hassle, hussle,
watches ... in 'que' file and
displays a count of matching lines
grep -v "and so" tale finds all lines without "and so"
and displays them
grep '^Yo Mama' insults finds all lines beginning with
the words 'Yo Mama' in the file
'insults'
grep 'ley$' poem finds all lines ending with
'Tetley', 'lines of ley', etc
grep [n-t]at poem finds all lines with words like
tattle, prat, gnat, but excludes
those lines with the words cat,
mat, vat
grep [^n-t]at poem finds all lines withOUT words
like tattle, prat, gnat, but
won't omit lines with cat, mat,
vat
command options argument
------- ------- --------
kill -9 process ID number
stops a job/process; process ID number comes from 'ps' command
e.g. kill 3890 stops process 3890
kill -9 3890 REALLY stops it
command options argument
------- ------- --------
ln - file1 file2
filename(s) Dirname
links a new file to an existing one, in the current or
different directory
e.g. ln ../New/text doc puts a link file named 'doc' to
the file 'New/text' in the present
directory
ln text doc Wip puts link files for 'text' and
'doc' into Wip directory
command options argument
------- ------- --------
lp -d filename(s)
sends a formatted file to the specified printer
e.g. lp -dps poem prints 'poem' on the post-
script (laser) printer, ps
command options argument
------- ------- --------
lpr -P[ps,2up,ansi] filename(s)
formats a file for printing and sends it to the specified
printer
e.g. lpr -Pps Wip/text prints 'Wip/text' to postscript
(laser) printer, ps
lpr -P2up poem prints 'poem' @ 50% on ps
command options argument
------- ------- --------
lpstat -p -
returns the status of the printing queues
e.g. lpstat shows which printers are active
and how many jobs are queued
lpstat -p full report of all printers,
regardless of activity
command options argument
------- ------- --------
ls -a,c,l,p,r,s,R,... [Dirname]
shows list of files and directories in current or named
directory
e.g. ls -a lists ALL entries
ls -c lists files by creation time
ls -l files in long format, showing
links, size, owner
ls -p ../Wip marks directories with /
ls -r reverse order
ls -s shows size in blocks
ls -R shows subdirectories
command options argument
------- ------- --------
mail - filename(s)
Email programme
e.g. mail text sends file 'text' to whoever
command options argument
------- ------- --------
man -k [keyword],??? [command]
shows help file/manual page for 'command' or list of possible
commands relevant to 'keyword'
e.g. man -k erase lists commands and manual pages
with the string 'erase'
man cat shows manual page on cat command
command options argument
------- ------- --------
mesg - y or n
lets you turn on (y) or off (n) your ability to receive messages
from other users of the computer
command options argument
------- ------- --------
mkdir - Dirname
makes a new subdirectory called 'Dirname'
e.g. mkdir Forpost puts a new subdirectory named
'Forpost' in current directory
command options argument
------- ------- --------
mv - file1 file2
filename(s) Dirname
moves (renames) the contents of a file into a new filename
moves (renames) a (group of) file(s) to a new Directory
e.g. mv ../New/text doc moves the contents of the file
'text' into the file 'doc'
mv text doc Wip moves the two files 'text' and
'doc' from the current directory
into the Wip directory
command options argument
------- ------- --------
nohup - command
prefacing a command with 'nohup' prevents the ensuing
job/process from being terminated at user logout
e.g. nohup ls > save.ls puts a listing of all files
from current directory into
'save.ls', even though user has
logged out
command options argument
------- ------- --------
nroff -e,h,i,q -mNAME - nN filename(s)
formats text files with indents and line spacings and whatnot
for eventual dumping to screen or file or printer. The info to
tell what formatting to do may be a simple option (-e), a
series of basic commands "embedded in the text file, or an
option that calls a whole file of possible formatting macros
(-me , where e is the NAME of the macro-holding file), these
macros themselves being embedded in the text file.
e.g. nroff -e poem dumps 'poem' to screen with
equally spaced words in
adjusted lines
nroff -me poem dumps 'poem' to screen, using
embedded macros defined in the
'tmac.e' file for formatting
command options argument
------- ------- --------
passwd - -
change to a new password
command options argument
------- ------- --------
paste -s -d filename(s)
pastes lines together from two or more files to form a "table"
of the lines from the files. Alternately, paste can concatenate
all the lines of one file with the -s option. Delimiters may be
specified other than and many delimiters may be cycled
through as the merge proceeds.
e.g. paste poem1 poem2 appends each line of poem2 onto
the end of each line of poem1,
delimited by a
paste -s -d" \t\n" report
takes the lines of 'report' in
threes, separating the first
two by , the second two
by and ending the third
with a (aka CRLF)
command options argument
------- ------- --------
pg -c,s,??? [filename(s)]
shows the contents of a file on screen, one page at a time. pg
is a 'read-only text editor'; once using pg to show a file,
move around within the file by pressing for the
next screen, or +/- integers to jump a number of screens
ahead or back. The 'q' command quits pg.
e.g. pg text shows the contents of 'text'
one page at a time
command options argument
------- ------- --------
pr -a,d,f,ln,m,n,p,r,t,wn,+n filename(s)
formats file(s) for display to terminal or piping to printer
-a multicolumn output, by line across page (with -n option)
-d doublespaced
-f use formfeed to separate pages; doesn't affect page length
-ln make page length n lines, instead of 66
-m print all files at once, one per column
-n output in n columns, by line down the page
-p pause before the beginning of each page
-r do not show diagnostic report if error occurs
-t omit 5 line header and footer
-wn set page width to n characters, instead of 72
+n start with nth page
e.g. pr -dp poem show 'poem' on screen, double-
spaced and requiring a RETURN
before showing next page
command options argument
------- ------- --------
ps -a,f -
display the process status, which shows the process ID numbers
of jobs in the system, the types of jobs and from where they
are being run
e.g. ps -a show ALL processes from all users
ps -f show ALL information about the
users' jobs, including priority
command options argument
------- ------- --------
pwd - -
show the name of the present working directory
command options argument
------- ------- --------
rm -i,r [Dirname/]filename(s)
removes the named file(s)
e.g. rm -i Janu*.* deletes all files starting with
'Janu' and with only one extension,
with a query for each file
rm -r Old deletes the directory 'Old' and
every file within it!
command options argument
------- ------- --------
rmdir - Dirname(s)
removes the named directory(s) IFF empty
e.g. rmdir Oldstuff deletes 'Oldstuff' if it has no
files
command options argument
------- ------- --------
sed -n 'ed command' filename(s)
edits file(s) as though it was (they were) a stream of text,
dumping output elsewhere (screen or another file) instead of
modifying the original file. Great for global changes and for
specific changes IFF you know which lines (by number or by
pattern) need to be altered. Use sed under sh, not csh!
An 'ed command' has the following structure:
'(number(,range)OR(,/pattern/)command(/old/new/)(\insert)'
This is a complicated structure and needs clarification.
The first part of an 'ed command' tells sed where to apply the
changes: to a line or lines matching numbers or patterns, to a
mix of the two, or globally.
e.g. sed '2d' poem deletes second line of 'poem'
sed '2,4 s/tree/timber/' poem
replaces the FIRST occurance of
the word 'tree' with the word
'timber' in lines 2 through 4
of 'poem'
sed '7,/Linus/p' poem prints every line from the
seventh to the first with the
word 'Linus'
sed 's/[Hh]emo/[Hh]aemo/g' poem
in ALL lines of 'poem', change
EVERY occurance of 'hemo' or
'Hemo' to 'haemo' or 'Haemo'
(g = global)
sed -n '$p' poem prints the last line of 'poem'
sed -n '/c[ao]n/p' poem prints only lines with 'can' or
'con' in them
sed -n '/c[^ao]n/p' poem
prints only lines with words
containing 'c?n', where ? isn't
either a or o; omits 'can I go
for a walk?' but includes 'can
I go to Cincinatti for a walk?'
sed -n '/c[ao]n/!p' poem
prints only lines WITHOUT 'can'
or 'con' in them
The second part of an 'ed command' tells sed what changes to
apply. Note that some of these change commands will not accept
a range of lines (e.g., 2,6) for their action.
e.g. sed 'a\
> append THIS!' poem puts the phrase 'append THIS!'
on its own line after EVERY
line in the file 'poem'
sed '/opt/ i\
> insert THIS!' poem puts the phrase 'insert THIS!'
on its own line before every
line containing the word 'opt'
sed '/You/ c\
> Yo Mama' insults
changes all lines with the word
'You' into the phrase shown
sed '/boots/ s/military/army/g' insults
changes the word 'army' to
'military' whenever 'boots' is in
a line of the file 'insults'
sed -n '3p' poem prints the third line of 'poem'
sed '/nausea/q' ill prints the file 'ill' up to the
first occurance of 'nausea'
sed '$ r finish' poem appends the contents of the file
'finish' to the file 'poem'
sed '/[aeio]/ !w uvowel.ls' poem
copies every line from 'poem'
lacking the letters [aeio] and
puts them in a file called
'uvowel.ls'
sed '=' poem prints the line numbers for
each line of 'poem'
sed '/tart/=' poem prints the line numbers of each
line with the word 'tart' in 'poem'
sed '/don\'t/!p' poem prints all lines without the
word 'don't' from 'poem'
Finally, sed commands may be multiple; concatenate them by
enclosing them within one set of '', but on separate lines.
command options argument
------- ------- --------
sh - -
makes a Bourne shell; exit with ^D
command options argument
------- ------- --------
sort -b,d,f,n,o filename,r -tc,+sw -ew[cf] filename(s)
sorts and merges lines from file(s). Resulting list is alpha-
betical, but UPPER and lower case letters are sorted separately,
numbers are sorted by first digit (not value), blanks beginning
lines come before A or a in listing, and other symbol characters
are used in sorting as well. These defaults can be over-ridden
and lines may also be sorted by strings other than the first
one, by using the fields option.
e.g. sort -b poem sort 'poem', ignoring blanks
sort -d poem sort 'poem' using ONLY letters,
numbers and blanks
sort -f poem sort 'poem' ignoring letter case
sort -n inventory sort 'inventory' by number value
sort -o surpoem poem sort 'poem' and put result into
a newfile called 'surpoem'
sort -r poem sort 'poem' in reverse order
sort -t: -f -o invsort +1 -3 inventory
sort 'inventory' using only the
second and third words on each
line. Words are delimited by a
full colon (not spaces). Ignore
the case of the letters and dump
the result to a file called
'invsort'
sort -t; -n +2 -3 +7 number.list
sort 'number.list' using only
the third and eigth values on
the lines. Values are delimited
by semi-colons
sort -t- -d +2.3r -4 +6f listing > processed.list
sort 'listing' using only the
third character of the third
word, sorted in REVERSE, the
fourth word, sorted normally,
and the seventh word, sorted
independent of letter case. The
field delimiter is a dash '-'
and the output is redirected
to 'processed.list'
command options argument
------- ------- --------
spell - filename(s)
produces a list of all questionable words in the file(s) listed.
command options argument
------- ------- --------
stty -option settings, baud rate
reports on the current settings of baud rate and terminal
options. When followed by a option with a new setting, changes
the terminal environment to the new setting
e.g. stty -echo turns off echoing
stty erase \ ^h sets 'erase' to Back Space
command options argument
------- ------- --------
tail +/- number,l,b,c filename(s)
prints sections of file(s). +number means start after this
number of lines (or blocks or characters, if the flags b or c
are given), while -number means start this number of lines
before the end of the file. Default number is -10l.
e.g. tail poem shows last ten lines of 'poem'
tail +30c poem shows all of 'poem' beginning
with thirtieth character
command options argument
------- ------- --------
tee -a,i filename(s)
forks output to both the screen and a named file
e.g. ls | tee save.ls sends list of files to screen
and to file 'save.ls'
command options argument
------- ------- --------
time - command
runs the command indicated and reports on the time used. Time
is reported for user (command execution) sys (command support)
and real (time sharing)
command options argument
------- ------- --------
tty - -
reports the pathname of your terminal
command options argument
------- ------- --------
uniq -c,d,u infile [outfile]
removes all repeated lines from infile; output to screen or to
outfile. Repeated lines must be adjacent (so sort first!)
e.g. uniq -c poem shows all lines, with number of
repeats counted
uniq -d poem shows only duplicated lines
uniq -u poem shows only unique lines in infile
command options argument
------- ------- --------
wc -l,w,c filename(s)
counts the number of characters, words and lines in a file
e.g. wc poem shows three numbers for # chars,
words and lines + filename
wc -l poem shows only # lines + filename
command options argument
------- ------- --------
who - [ami] [ am i]
tells who is using the system, and who YOU might be
e.g. who shows all current users
whoami shows your userID
who am I shows your userID + stuff
command options argument
------- ------- --------
write - userID
sends lines from your terminal to the terminal of another user
e.g. write norbert lets me fill Norbert's screen with
my blather
command options argument
------- ------- --------
zip - h,i,r filename(s)
shrinks or (unzip) expands files, between their usable forms
(i.e., editable, executable, viewable, etc.) and a much smaller
format for transfer or storage
e.g. zip mbf mybigfile.txt shrinks the file 'mybigfile.txt'
to one called 'mbf.zip'
zip -h writes a short help file on zip
to the screen
zip allst -i allstuff.* makes a compressed archive called
'allst.zip' of all files beginning
with 'allstuff', regardless of
the file extensions
zip -r megadir . makes a compressed archive called
'megadir' of the current directory
and all its subdirectories
unzip mbf.zip expands 'mbf.zip' back to
'mybigfile.txt' again
Special features
------- --------
Wild cards ? matches any one character
* matches any number of characters
[co]*.c matches c programme files
beginning with 'c' or 'o'
[a-q]* matches files beginning with
any letter from a through to q
Job control
& put job/process followed by '&' into background
at tell a job/process when to run
kill terminates a specified job/process
nohup make job/process permanent, even if user logs out;
should be used with '&', so log out is possible!
Redirection methods
> sends output to a file instead of the screen
>> appends output to a file instead of the screen
< receive input from a file
| "pipeline" send output from one command to another command
tee fork output to screen and to named file
Continuation methods
; command separator - for consecutive commands on one line
\ command line linker - to continue command on next line
# line nulifier - text following is treated only as text
Value/content recall methods
$variable return value of variable
`command` return contents of command output
neutralising METAcharacters * ? [ ] > >> < | & ; \ ` " $ #
\ METAcharacters following \ is used as a character
` METAcharacters within `` are used only as characters
" MEtacharacters within "" EXCEPT $ ` \ are used only
as characters
Text file formatting
nroff alters appearance of simple text files via "embedded"
commands placed in the text file and executed by nroff
when the file is given as its argument
command function
.ce N centre next N lines
.de cmds .. define an embedded nroff macro
.in N indent text N spaces
.hy N auto-hyphenation on when N ne 0
.ll N line length N characters
.ls N line spacing to N lines
.na no right margin justification
.nf no fill text
.sp N include N blank lines
.ti N indent next line N spaces
.tr sdfn replace 's' with 'd', 'n' with 'f'
David Featherston
( )
Updated on Wednesday, 12 February, 2005
Copyright © 1995-1996 by
David W. Featherston