Some Common Unix Commands

The purpose of this summary is to give a quick survey of the Unix commands that most users find useful. These commands have many more options -- alas, occasionally inconsistent from one computer to another. For additional information see the Unix manual pages, using the command man.

For several commands whose descriptions would have been too lengthy, only a brief description is given.


File and Directory Management

cat
Concatenates file(s). By default, these are printed on the monitor without pausing at the end of each page. To insert pauses, see the first example.

Examples:
cat chapter4 | more (or cat chapter4 | less)
Displays the file chapter4 using the command more (or less) to insert pauses one screen at a time.

cat chapter1 chapter2 > book
Concatenates the files named chapter1 and chapter2 in the current directory and collects them in a file named book on the current directory.

cat chapter12 | egrep broccoli > broccoli
Feeds the contents of the file chapter12 to the egrep filter which then only keeps the lines having the word broccoli. This is, in turn, saved in the file named broccoli.

Remark: Blank spaces above just before or after > are unimportant, they just make it easier to read.

cd
Change directory

Examples:

cd Tex
Change to the subdirectory (of the current directory) named Tex

cd Tex/mypapers
Change to the sub-subdirectory (of the current directory) named Tex/mypapers.

cd /Users
Change to the subdirectory Users of the root directory.

cd
Changes to your own home directory from any other directory.

cd ~ksmith/public_html/
Changes to the user ksmith's public_html directory.

chmod
Change who has permission to read, write, or execute a file. See the man page.

cp
Copy file(s)

Examples:

cp .cshrc .cshrc3-24
copies the file named .cshrc to the file named .cshrc3-24

cp *.bak tmp/
Copies all files whose names end .bak to the subdirectory tmp of the current directory. The subdirectory tmp must already exist.

cp -i *.bak tmp/
Same as above, but warns before overwriting a file with the same name.

crypt
Encrypt a file using a key you specify. The key should not contain whitespace (space or tab).

Example:

crypt F(x)=12 < exam1 > green
Encrypts a file named exam1 using the key F(x)=12. Here the encrypted output file is named green.

crypt F(x)=12 < green > happy
This deciphers the above file and saves the output in the file happy. One must use the same key, but the output file need not have the old name (you may not know the original name anyway).

find
Finds files with specified names and then does something you request, such as print their locations, or delete them, perhaps depending on when they were last modified. This is a very powerful command; most people use only its simplest abilities.

Example:

find /usr/local/ -name pico -print
Find all files named pico in the directory /usr/local/ and its subdirectories. The list is printed on the monitor.

gzip, gunzip, zcat
gzip will compress a single file. Use the command gunzip to uncompress. For a group of files, such as a complete subdirectory, first use the program tar to collect all of these files into one longer file, which is then gzipped.
Note: this program is similar to the related program named zip, but their files are not interchangeable. gunzip will, however, uncompress any files compressed with either the compress or pack commands--these files usually have a .Z or .z suffix, respectively.

Examples:

gzip exam1.ps
This creates the compressed file named exam1.ps.gz and deletes the file exam1.ps

gunzip exam1.ps.gz
Gives the original file exam1.ps and erases the compressed file exam1.ps.gz

head
Displays the first 10 lines of file(s). (See also tail).

Example:

head 141* | more
Displays the first 10 lines of the files whose names begin with 141 and uses more to display them one screen at a time.

less
Displays a file on the screen, one page at a time. Many people prefer this to the older program more since less has more options.

Example:

less chapter1
This is equivalent to cat chapter1 | less

ln
Link files, that is, have a filename in one directory refer to the file located in another directory. See the man page.

ls
List the directory contents.

Examples:

ls
Alphabetical list of all the files in the current directory except those whose names begin with a period . (thus, one frequently begins the names of a configuration file with a period so it is hidden from a routine directory listing).

ls -a
Alphabetical list of all the files in the current directory, including those whose names begin with a period.

ls -l (or ls -al )
Long directory listing (including size, date the file was last modified, and who has permission to read, write or execute). The second version also includes all files whose names begin with a period.
If the directory has many items, it will not fit on one screen. To avoid the listing scrolling off the screen one can either pipe the output through the commands more or less, as
ls -al | more

ls -lt *bak
Long listing (ordered by the time the files was last modified) of all files whose names end with bak.

mkdir
Make a new directory.

Examples:

mkdir Tex
Make a new subdirectory named Tex in the current directory.

mkdir Tex/classes
Make a new subdirectory named classes in the directory Tex.

mkdir Tex/classes; cd Tex/classes
Make a new subdirectory named classes in the directory Tex and then immediately change to that directory.

more
Displays a file on the screen, one page at a time. Many people prefer the program less, since less has more options.

Examples:

more chapter1
This is equivalent to cat chapter1 | more

mv
Move or rename a file.

Examples:

mv chapter1 chapter1.old
Renames chapter1 as chapter1.old

mv *.bak tmp/
Moves all the files in the current directory whose names end with .bak to the directory tmp.

mv -i *.bak tmp/
Same as above but warns before overwriting a file that already exists.

rm
Remove (= delete = erase) files(s).

Examples:

rm book *.dvi
Removes the file book and all the files in the current directory whose names end with .dvi.

rm -i book *.dvi
Same as above but is interactive, prompting before removing each file.

rm -r book
Recursively removes the directory book and all of its files and subdirectories. rm -ri book is the interactive version.
Note: USE WITH CAUTION!

rmdir
Remove a directory (it must already be empty, but see rm -r).

Example:

rmdir math141
Removes the (already empty) directory math141.

tail
Displays the last 10 lines of a file. Also see head.

tar
Collects a group of files into one file. There is no command "untar"; instead one uses tar but with different flags. By default, tar recursively includes all sub-subdirectories and retains the subdirectory structure. One frequently uses gzip to compress a file after using tar. Files distributed on the network are usually tarred and compressed or gzipped. This program is typically used when archiving files.

Examples:

tar cvf letters1994.tar letters
Collects all of the files in the subdirectory letters (and its subdirectories) into one large file letters1994.tar in the current directory. The original files are not deleted or changed. One might then use gzip to compress the tar file:

gzip letters1994.tar
to create letters1994.tar.gz (and delete letters1994.tar). One can combine these into one step with:

tar cf - letters | gzip > letters1994.tar.gz

tar xvf letters1994.tar
Reverses the above and "untars". If needed, this creates the subdirectory letters or any sub-subdirectories. The file letters1994.tar is not erased.

zcat letters1994.tar.gz | tar xf -
The zcat command uncompresses the gzipped file which is then "untarred" by tar xf - (note the final -). This avoids creating the file letters1994.tar, which may use critical disk space. On some computers the program zcat might be named gzcat.

Text Processing Commands

In addition to the commands below one also has the commands axe, emacs, nedit, pico, and vi to use various editors on different computers.

diff
List the differences between two files.

Example:

diff chapter1 chapter1.old
In the output, the lines beginning with "<" refer to the first file, those beginning ">" refer to the second file.

dvips
Print a TeX dvi file on a postscript printer. The command dvips by itself lists a variety of options.

Examples:

dvips exam1.dvi (or just dvips exam1)
Prints the file exam1.dvi on the default printer.

dvips -oexam1.ps exam1.dvi (or dvips -oexam1.ps exam1 )
Processes the file exam1.dvi and saves the output in the file exam1.ps rather than printing it immediately. At a later time one can use the command lpr exam1.ps to print this.

egrep, fgrep, grep
Search file(s) for a pattern. egrep is recommended, as it is typically faster than grep or fgrep. The grep family of commands has many useful options.

Examples:

egrep Johnson chapt2 (or egrep -i johnson chapt2)
Display all the lines in the files chapt2 that contain the word Johnson. The case insensitive version using egrep -i does the same but will also catch JOHNSON, JohnSon, johnson, etc..

fmt
Reformat a text file to include, say, in an email message, where at most 80 columns are permitted. This breaks lines at white space (tab or space). No hyphenations are introduced. Reformatting is useful for files produced by editors such as edit on the NeXT, where the lines may be very long.

Example:

fmt myfile
Formats the text file myfile so that each line has at most (by default) 72 columns.

latex2html
Changes a LaTeX file into an html file that can be posted on the World Wide Web. This processes the LaTeX file and makes each mathematical formula in the document into a graphic file that can be viewed by World Wide Web graphical browsers, such as Mosaic and Netscape. One should avoid fancy LaTeX commands. One should also break the document into sections, which are then treated as separate html files; these then take less time to load in World Wide Web browsers.

Example:

latex2html homework3.tex
This produces the subdirectory homework3 containing the main file homework3.html and other graphics and html files that that file uses.

ispell
Interactive spelling checker. One can use this by itself and also within the editor emacs. [Within emacs on our system, use Ctrl C 3 to begin a spell check of the complete current document].

Example:

ispell chapter1
Checks the spelling in the file chapter1.

lpq
Lists all the jobs (and their job numbers) queued to be printed.

Examples:

lpq
List the jobs queued to be printed on the default printer.

lpq -P4E1
Lists the jobs queued to be printed on the printer named 4E1.

lpr
Print a file on the printer (for many years, a "line printer" was used, whence the name lpr).

Examples:

lpr chapter1.ps
Prints the file chapter1.ps on your default printer.

lpr -P4E1 chapter1.ps
Prints the file chapter1.ps on the printer 4E1.

lprm
Removes a job from the print queue. One first needs to run lpq to get the job number. After removing a file from the print queue, you can run lpq again to see if it was actually removed. You can only remove your own files, not those of others.

Example:

lprm 235
Removes job 235 from the print queue.

sort
Sort a file. There are many possibilities such as reversing the order of a sort, and sorting by various fields in a line.

Examples:

sort file1 > file1.s
Sorts the lines in file1 by the characters at the beginning of each line. Store the output in file1.s. ( > file1.s is equivalent to -o file1.s ).

sort +2 file1 > file1.s
As above, but sorts beginning with the 3rd field; by default -- which can be changed -- fields are separated by whitespace, i.e. spaces or tabs. The output is again saved in file1.s

tex, latex, amstex
Processes a TeX (or LaTeX or AMSTeX file), creating a dvi file.

Example:

tex exam1.tex (or tex exam1 )
Processes the file exam1.tex and creates exam1.dvi

xdvi
Preview a TeX dvi file on an XTerminal.

Example:

xdvi exam2.dvi &
Displays the file exam2.dvi (the optional & at the end of the line frees up that terminal window for subsequent commands without requiring that you first exit from xdvi).

Graphics Processing

ghostscript, ghostview
Ghostview uses Ghostscript to view and modify PostScript files.

Example:

ghostview diagram1.ps
Exhibits diagram1.ps. You can change such items as orientation, and magnification.

grab
On the NeXT computer, used to grab an image on the screen and capture it as a tiff file. Documentation is in the application, which is located in /NextApps/Grab.app . This image can be edited subsequently using IconBuilder.app (see below) or changed to some other format such as gif using pbmplus.

IconBuilder.app
On the NeXT computers, used to edit images which are tiff files, such as icons. Documentation is in the application, which is located in /NextDeveloper/apps/IconBuilder.app. As an alternate, one might prefer to edit the graphic using xpaint on our X-Terminals. One may need to first use pbmplus to change the format of the graphic.

pbmplus
A family of programs that change a graphics file from one format to another. One should presume that any change of format will result in a loss of information and a degradation in quality.

Example:

tifftopnm touch.tiff | ppmtogif > touch.gif
Changes the file touch.tiff to touch.gif.

xpaint
An X-Windows program for making computer graphics. One can edit or create a picture.

Example:

xpaint &
This opens xpaint, which has both a toolbox and a canvas window. Click on File to edit an existing picture or icon.

xv
An X-Windows program that displays many different file types, such as gif and xbm. It can also grab (color) images from your monitor and edit them as well as many additional capabilities.

Example:

xv touch.gif
Displays touch.gif which you can then process using xv's tools.

System Commands

at
Executes a command line -- or a script -- at a specified time. For instance, you may wish to print a long file at 4:30 in the morning (=04:30 on a 24 hour clock). Or you might want to begin a long calculation in the middle of the night when the computer is less busy.
Use atq (or at -l) to see the queue of pending at commands. If desired, you can then delete jobs from the queue with the command atrm (or at -r); the procedure is similar to lpq, lprm mentioned above.

Example:
		at 0430
		lpr  myfile.ps
		echo "myfile.ps was just printed"
		[Ctrl D]
In the above, each line ends with a carriage return, and the last line closes the file with the single character Ctrl D. After your file is printed, you will be send an email message stating myfile.ps was just printed.

date
Prints the current system date and time on the screen.

Example:

date

du
Prints a summary of the disk usage for the current directory and all subdirectories. Careful: by default some computers use blocks of size 1024 bytes while others use blocks of size 512 bytes.

Example:

du
Give size of each directory.

du -s
Give summary total only.

du -a
Give size of each file.

kill
Terminate a process (i.e. terminate a computer program that is running out of control). Killing a process may also kill all of its "children". Thus, killing a shell (such as csh) may kill all of the foreground and background processes started from that shell. Killing a users login shell is equivalent to that person logging out.
For some types of severe problems, the easiest (and perhaps only) way to kill a process or a whole login session is to login to the same terminal from another terminal and then kill the desired process(es).
Before using kill one needs to run the command ps to determine the process number.

Examples:

kill 2765
Terminate process number 2765. This gently stops the process, automatically closing all opened files and cleaning up. If this does not kill the process, then use kill -9 below.

kill -9 2765
Terminate process number 2765. Not gentle. You may need to do your own cleanup, but at least you almost always get the process to stop. If this doesn't work, ask an expert.

man, TkMan, xman
Use this to read the Unix manual page on a command. On an XTerminal you may prefer to use xman (or, better yet, tkman).

Examples:

man man
Use this to read the Unix man page on the command man itself.

man ls
This gives the Unix man page for the command ls.

man -k print
This gives a list of all the Unix commands concerning "printing$quot;.

ps
List all the processes running. Before killing a process (=program) you'll need this to get the process id number. The switches on this depend on which computer you are using.

Examples:

ps -aef | egrep jsmith (System V Unix, recent SGI or Sun computers)

ps -aux | egrep jsmith (BSD Unix, on all NeXT computers)
List all of the processes running that have the word jsmith in their description. In practice, the user jsmith will use this to find all the processes she is running.

pwd
Print (on the screen) the complete pathname of the current working directory.

Example:

pwd

stty
Set or display the current terminal settings. For instance, set the number or rows, columns, and the delete key.

Examples:

stty
Display the current terminal settings.

stty rows 49
Set the screen size to 49 rows.

which
Which command is being used. The command must be in your PATH.

Example:

which emacs
Gives the full pathname to the emacs command, including any shell aliases being used.

who
Who is currently logged in this computer.

Example:

who
Gives a list of the userids of those currently logged in.

Network Commands

electronic mail (E-mail)
There are a variety of commands: elm or Mail work on all of our computers. In addition, while sitting at the console of our networked computers, each have their own E-mail program. Finally procmail is the standard program used to filter and pre-process mail, say depending on who it is from or the subject -- although the simple filter program is frequently adequate. One can also have mail automatically forwarded, or automatically send your correspondents a message that you are out of town.
One uses MIME to include non-text material (binary files, graphics, sound) in an email message.

ftp
Transfer files to or from a remote computer on the Internet. Some computers permit anonymous ftp that is accessible to anyone on the network, even if you don't have an account on that computer. In this case, when asked for the userid, type ftp and then give your true E-mail address as your password.

Example:

ftp math.berkeley.edu
To initiate a file transfer with the computer math.berkeley.edu

After connecting, one gives various file transfer commands, such as:

  get  file2      to get (receive) file2
  mget chapter*	(multiple) get all files whose names begin 
  		  with chapter.
  put, file1	to put (send) file1
  mput *.tex	(multiple) put all files whose names end
  		  with .tex 
By default, files are assumed to be text files. To send binary files (including compressed files), first use the command: bin

kermit
Transfer files between two computers, usually when one is not on the network but only connected via modem. Slow. ZMODEM transfer is usually faster if available on your other computer (for instance, ProcommPlus supports this).

Example:

kermit
After starting kermit one uses the commands

  send chapter1   sends chapter1 to the remote computer
  receive         receive files from the remote computer;
		  you then transfer control to the other
		  computer and tell it what files to send
In addition first one must set the file type for binary or text files (set file type binary or set file type text). The default is usually set for text files.

Reading News
The standard newsreader we use is tin (just give the command tin), but there are many others such as rn or (on the NeXT) NewsGrazer. The first time you read mail you will need to decide which newsgroups you wish to read.
One can also read some news groups from various World Wide Web browsers.

rlogin
Remotely login to a Unix computer. This automatically sets certain variables, such as which terminal (keyboard) you are using. Unless specified otherwise, the same userid is assumed.

Examples:

rlogin mail.sas.upenn.edu

rlogin mail.sas
You can use this shorter name if you are currently on another computer also in upenn.edu

telnet
Remotely login to a computer.

Examples:

telnet mail.sas.upenn.edu

telnet archimedes.math
You can use this shorter name if you are currently on another computer also in upenn.edu

World Wide Web
Our computers use the graphical World Wide Web browsers netscape for hans.math), and OmniWeb for the NeXTs. In addition one can use the browser lynx that does not offer graphics.

zmodem (rz, sz)
Transfer files between two computers, usually when one is not on the network but only connected via modem. Much faster than Kermit.

Examples:

sz *.tex
Send all files in the current directory whose names end with .tex

rz -b
Prepare to receive binary files from the other computer (the -b means binary). You must then use the other computer, perhaps at home, to specify which files are to be sent.

Some Configuration Files

There are a number of configuration files: several for the operating system as well as special files for separate programs. Here are some. Many of these are initially set with defaults that will be correct for most users. You may customize these -- and others -- yourself (caveat emptor).
shell:    .cshrc   .login      .logout  .profile .plan
X:        .chestrc .sgisession .Xdefaults
editors:  .emacs   .neditrc
mail:     .mailrc  .elm/elmrc .elm/aliases.text
          .forward .vacation
netnews:  .newsrc  .tin/tinrc