The ``twelve'' most important Unix commands
Carl Mason
carlm@demog.berkeley.edu
rev 1.0 Fall 2008
Although Unix has a point and click graphic user interface, called
X11, which works just like those other operating systems, Unix is at heart a
command line operating system. So while it is possible in many cases
to do what you want via pointing and clicking, using the command line
and other text based tools will make you happier much more
efficient... eventually.
To operate with the command line, you will need to know the 12 most
important Unix commands described in Section 4. To
enjoy it you will also need to know a few tricks that are also covered
in this document.
You don't need to know much about Unix in order to start
doing Science, but it would not hurt to learn more. In your copious
free time, check out some of the Unix primers on the web. Ask google
something like ``Unix beginner'' to find more resources than you could
possibly want.
Terminal windows
In order to use the command line, you must open a terminal window
(also knows an xterm window). The first terminal window can be
launched from: [Application]
[System
Tools]
[Terminal].
It should look something like Figure 1. Notice
that the window features a menu bar, unfortunately the menu bar it is
both useless and misleading. Make the menu bar disappear by pressing
the
and clearing the ``show Menubar'' check-box.
Now aside from the title bar at the top, the only words in the
terminal window should be the Unix prompt. In
Figure 1 the Unix prompt is [carlm@twins ~]$,
indicating the user, carlm, the machine, twins and the
current directory which is indicated by the ~. In this and
other documents, the Unix prompt will look like this: @:
.
If you are in no particular hurry to finish your dissertation, you can
modify a large number of colors and beeps and other important features
of the terminal window.
[Edit Current Profile] is the place
to start wasting time.
If you have already wasted time on this sort of thing and are thus old
enough to find the default font a bit small, a useful trick with
terminal windows (and browser windows too) is the
+
+
to increase and
+
to
decrease the size of the typeface.
Figure 1:
terminal window
|
|
The command interpreter
The command interpreter, or shell is the program that runs in
each terminal window. It waits
for you to type something at the Unix prompt, @:
, and then does
what it thinks you meant. The shell we use here is called
tcsh (pronounced ``teesh''). Tcsh is one of several modified
versions of the original csh (pronounced ``sea shell'').
The most important thing that the shell does for you is to let you give
commands to the computer. These include the 12 most important Unix
commands (Section 4) as well as commands to launch
applications like R, Stata, word processors or spreadsheets1. The shell does
several other things for you some are essential, some enhance
efficiency and others are just cool.
Among the essential features of the shell is a mechanism to
communicate with running programs that are not expecting user input
or have run amok. This is not all that common, but when it happens
you need to be able to get the program's attention and tell it -
generally to drop dead.
+
does this.
Below is a list of some handy/essential key combinations that the
shell recognizes:
To make you more efficient, the shell offers three particularly
nice features: ``history'', ``
completion'' and ``scripting''.
The history feature allows you to recall
and edit any command that you have previously issued. To make
the previous command appear at the @:
hit
+
2. To see
even more previous stuff type
+
more times.
+
will make the next command appear -
obviously, this makes no sense unless you have type
+
at
least once.
You can operate on a recalled command using several standard emacs
editing keys:
-
+
To go to the beginning of the current line
-
+
To go to the end of the current line
-
+
To go forward one character
-
+
To go backward one character
The
and
keys do what you would expect.
If you hit the
key anytime
while constructing a command, tcsh will do it's best to figure out
what you are planning to type next. If you are typing a
command it will try to find a command that starts out with what you
have already typed. If you are typing the name of a file tcsh will try
to complete if for you. If what you have typed does not uniquely
determine a command or filename, tcsh will beep at you and provide a
list of possible completions. You can then
type a few more characters and hit
again.
Whenever you find yourself typing the same
command several times, it's time to consider scripting. A shell script
is just a file of commands that you could have entered at the
keyboard, but typed into a file instead. You can then set the file's
execute bit (Section 7) and execute that file - perhaps now,
perhaps later. Using emacs to create that shell script can save you
lots of time and hair loss - particularly if many of the commands you
are typing are quite similar.
Scripts are also very useful for people who like the idea of being
able to reproduce results.
The shell is also responsible for displaying the results of the ls
command (See 1) in lots of colors.
Below is a list of the 12 most important Unix commands. They are
simple enough to be easily memorized by anyone who can keep
the names of all twelve months in his head.
For the most part, these commands are for logging on and off; for
printing; and for moving files and directories around. Many of these
commands functions can be done using a file manager or under emacs, but
knowing how to do them from the command line, opens the possibility of
automating tasks with shell scripts.
- ls
-lgdaF
regular expression
The ``list
file'' command, ls is used -not surprisingly- to list the names
and pertinent information about some or all of the files in a
particular directory. The most common and useful option is
-l that a lower case L not a one. It reveals the most
interesting properties of your files.
A regular expression is a set of special characters (or
``meta-characters'') that can be used to represent a list of files or
more generally, the set of all character strings with certain
characteristics. Regular expressions come up in a lot of places and
can be very useful when working with datasets that are full text
and/or errors. Regular expressions are used with ls
to limit the number of files displayed. For example ls X*Y
would list only the filenames that start with an X and end with a Y;
ls [aeiou]* would show you all the files that begin with a lower case
vowel (In the previous example the [ ] are part of the command)
- cd
directory-name
The ``change directory''
command makes another directory your present working directory.
With no argument, it "moves you" to your home directory. To move
one directory "higher" use ".." (two dots) in place of the
directory's name. The parent directory of the current directory
is always addressable as "..".
- cp
-R
source file/directory target
file/directory The copy command, cp is used to copy files and
directories. The -R flag is used to copy entire directories and
the contents thereof.
- mkdir <new-directory-name> The ``make directory''
command is used to create a new sub-directory of the current
working directory.
- rm file-name or
regular expression
The ``remove''
command is used to remove, or erase files. Here again,
regular expressions can be very useful - and quite dangerous.
rm is aliased to rm -i so that it asks you to
verify that you really want to remove a file. If you get tired
of this safety feature, use \rm instead.
- rmdir directory-name the ``remove directory'' command
removes directories. In order to remove a directory, it
must be empty or files and sub-directories.
- chmod
aogu +/- rwx
filename-or-directory The
``change mode'' command is used to modify the permissions
(or mode) of a file or directory. Permissions are the
characteristic of a file or directory which determine who has
what type of access to it. All files and all directories have
permissions, only the owner of the file/ directory is permitted
to change modes.
The first argument is a string of characters that grant (+) or
revoke (-) permission to read(r) write(w) or execute (x) the file
or directory. The letters aogu indicate who is to receive or lose
the given permission. u=user, g=group, o=other, and a=all. Thus
to revoke write permission to all users you would type:
chmod a-w filename
To grant permission to yourself and the group to write and
execute a file you would type:
chmod ug+wx filename
- ln
-s
real-file-name artificial-file-name
The ``link'' command creates an alternative name for an
existing file or directory. This is particularly useful when
using data sets that you keep in /data/commons (as you
should). Rather than typing /data/commons/userid/datafile to
reference your data, a symlink would allow you to type something
much shorter.
ln -s /data/commons/userid DATA
would create a link in your current directory called
``DATA''. But DATA is really just a secret back way to
/data/commons/userid. typing ls DATA for example is the
equivalent of typing ls /data/commons/userid.
It would be a good idea to create the above link right now. Use
the mkdir to create a new directory in /data/commons
called your userid. Then create a link in your home directory so
that you can start storing and accessing huge data sets right
away.
- mv file-name new-file-name The ``move''
command changes the name or location within the filesystem of a file
or a directory.
- less file-name Variant of the more command -
less is used to scroll through a file on the screen. While
displaying a file,
scrolls one additional line;
scrolls one additional screen full;
scrolls
backwards,
quits,
searches forward for
``word'',
searches backward for ``word''.
- lpr
-Pprinter-name
filename
The ``line printer'' command prints a file to the named
printer.
Demography Department Printers
| Printer |
Location |
Type |
| age |
Basement Lab |
HP Laserjet 4015 postscript monochrom duplex 1200dpi |
| parity |
Room 101b |
HP 4050n postscript monochrom 600dpi |
| class |
2224 2nd floor |
HP 4100n postscript 600dpi |
| cohort |
2232 2nd floor |
HP 4200n postscript 600dpi |
| status |
Library |
HP 4200n postscript monochrom 600dpi |
| ses |
Xerox Room |
Ricoh postscript color duplex 1200dpi |
Note that by default each of the above printers is configured to print
in economy mode thus saving toner and by extension the world. If you
need high quality printouts add ``HQ'' to the printer name e.g.
lpr -PsesHQ filename.
- pwd ``present working directory'' tells you where you
are, that is, it tells you which directory the shell thinks is the
current directory.
- du
directory
The ``disk use''
command is designed to tell you how much disk space each
directory is consuming. It's main use, however, is simply to
display the directory structure.
- man -k key-word | command-name
The "manual" command is used to display manual pages on your
screen. To say that man pages are not particularly easy to read
is is an understatement of almost biblical magnitude. But they
are very handy for refreshing your memory or searching for
something very specialized.
The man program puts the contents of the man page in a ``less''
process, see item 10 for a description of how to navigate
in less.
In this century a very good source of information on Unix is
google. The web knows all about Unix and while there are lots of
different distributions, command line tools in particular are
nearly identical in all distributions including Solaris, Hpux,
every flavor Linux, BSD, and even Apple's OS X (which by the way
is Unix too).
- exit or logout closes the current Unix window,
and logs you off - if the current window is the console window.
In addition to the key combinations and commands discussed Unix also
supports several characters with special meanings to the shell. Below
is a list some of the more common ones:
- *
- The asterisk or ``star'' character is used in regular
expressions (See item 1). When the shell sees a * by
itself as in @:
ls * it replaces * with a list of all the
files and subdirectories in the current directory. @:
ls *
tells the shell to run the ls command on each and every file and
subdirectory in the current directory. So where @:
ls will
show files and subdirectories @:
ls * will list the files
that live in subdirectories of the current directory as
well.
- &
- The ampersand tells the shell to run the process in the
``background''. When a process is launched in the background, the
xterm (See 2) immediately returns with a prompt. When you
run a process in the foreground (the usual case) the prompt comes
back only when the process exits.
NOTE it only makes sense to run programs in the background
if the program spawns a new window. So emacs, stata,
userfirefox, or oowriter are all fine running in the
background. The 12 most important Unix commands are not. They all
write their responses to the terminal window. If you put them in the
background they cannot do this.
REALLY important: R should not be run in the background for
the same reason: it runs in the window from which it was launched.
This will all make sense after the first week or two of 213.
To bring a backgrounded program to the foreground, type
@:
fg
%n
where %n is the percent sign followed by a
number indicating which backgrounded process you want to
foreground. You only need to enter the %n if you have more than one
process running in the background. Type
@:
jobs
to get a list
of backgrounded processes associated with the current xterm.

- the tilde character is interpreted by the shell to mean
``home directory'' by itself, it means your home directory,
if it is followed by a username as in
carlm it refers to that
user's home directory. The
can be used in complicated
pathnames such as
carlm/public_html/213F97/welcome.html.
For it to make sense, the
must be the first character (and
perhaps the only character) of a pathname.

- The ``pipe'' is used to send the standard output of one
process into the standard input of another. For example, if you
wanted to know the number of lines in every data file in the current
directory you might type: @:
ls *.data | wc -l . The ls
*.data produces a list of files in the current directory that end
in ``.data'', the
then feeds this list to the word
count command ``wc''. The -l argument tells
wc to only report the number of lines. This example assumes
that you have named all of your data files
somethingorother.data.

- The right angle bracket (or greater than sign) is used to
send the output of a process into a file. @:
ls > file.list
would produce a file called file.list containing (surprise) a list
of files. Use double angle brackets to append a process's output to
an existing file.
@:
ls
/public_html » file.list
would add
the names of the file's in your public_html directory.
The ``twelve'' most important Unix commands
This document was generated using the
LaTeX2HTML translator Version 2002-2-1 (1.71)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split 0 12important
The translation was initiated by Carl Mason -- Director Demography Lab on 2008-08-18
Footnotes
- ... spreadsheets1
- It is of
course possible to launch most applications via the menu system or
by clicking on corresponding data files in the file manager, but the
command line is often faster
- ... 2
- the up arrow key will also work
Carl Mason --- Director Demography Lab
2008-08-18