REU Program in Number Theory
University of Illinois, Summer 2002
Unix Tips
Here are some Unix commands and shortcuts that I have found useful,
especially when working with large files of data.
Some of these commands work only in the csh or tcsh shells. However,
those shells are the default on the Math Department computers, and
most likely you are set up to use these shells.
You can check this by doing
"finger login", where "login" is your login name.
Shell shortcuts
-
!!
Repeats the most recent command.
-
!t
Repeats the most recent command beginning with "t". Similarly for
other letters, or strings of initial letters in a command. This is
very handy when going through a compiling/running/editing cycle.
For example, if you work on a latex document, after giving
the full command names "latex file", "xdvi file", "dvips file", during
the first cycle, you can then repeat the steps using the shortcuts "!l",
"!x", "$d". (Be careful, though; if you type "!r" instead of "!t", you
may inadvertently repeat the last delete command.)
-
dvips !$
This executes dvips with the last argument of the most recent command.
For example, after doing "xdvi verylongfilename", the above
command will run the file "verylongfilename" through dvips. Similarly, after
editing a file with "vi verylongfilename", you can print out the file
by saying "lpr verylongfilename".
-
h
Gives a list of the most recent commands. Useful if you want to
repeat a command, but don't remember the exact form you used.
-
!196
Repeats the command numbered 196. The command numbers can be displayed
with the "h" command.
-
Up/down arrows
As an alternative to the exclamation mark commands, you can use
the up/down arrows
to move back and forth in the command history and repeat/edit a
command.
Pipes and Redirection
One of the most useful features of Unix is the ability to "pipe" the
output of one command into another command, and to "redirect" the
output to a file.
Pipes are denoted by vertical bars (|); redirections are denoted by
"greater than" signs (>). Spaces around these symbols are not
required, though for clarity, you might want to surround the symbols
by spaces.
Redirection of output is a very simple concept; instead of displaying
the output on the screen the program dumps its output onto a file.
A Unix pipe works much like a plumber's pipe: it takes the output of
the command to the left of the pipe symbol (|) and uses this as the
input of the command to the right of the pipe symbol.
Multiple pipes can be stacked together.
Also, you can combine history shortcuts with pipes. For example,
if, after displaying a sorted file with the "sort" command,
you find that the file is too big to fit on one screen, the command
"!! | less" will redisplay tthe file, one screen at a time.
The following
examples illustrate the use of pipes. Most are
self-explanatory; "file" stands for a generic filename.
-
sort file | less
Sort the file and
display the sorted file, one page at a time. (If you prefer the
standard command "more", you can use this instead of "less".
"less" is an enhanced version of "more" - for example,
it allows you to move backwards
and forwards in the file; to exit "less" , use "q".)
-
sort file | lpr
Sort the file and send the sorted file to the printer.
-
sort file > file.sorted
Sort the file and create a new file, "file.sorted", with the contents
of the sorted file.
Tools for analyzing data files
The following are some handy tools for analyzing data files.
-
sort: Sort files.
-
grep: Extract lines matching a given pattern from a file.
-
awk: Extract specific fields, columns, from a file (plus much
more).
-
perl: The ultimate geek tool.
Can do all of the above, plus much more.
The first three are part of any standard Unix distribution and
come with man pages that you can consult. Perl comes with multiple man
pages, there is an elaborate online
documentation system (accessed with perldoc), and
an enormous amount of information available, both online and in print.
While learning the full power of these tools takes time
and effort (years in the
case of perl), it is easy to learn enough to be able to use
these utilities on the command line for simple data analysis tasks.
To illustrate this, assume you have a file of numbers, three per line,
separated by blanks (this is the most convenient format for the above
utilities), like the following:
123 398 17359
317 19 2909
39 -399 -5789
49 33 200
255 33 -378
Here is how you could accomplish various tasks using one of the
mentioned utilities. (Here "file" is assumed to be the filename.
Recall that you can save the output of each of these commands to a
file by appending a command like "> file.out" or pipe it through
a pager with "| less".)
Last modified: Mon 16 Dec 2002 08:33:36 AM CST
A.J. Hildebrand