UNIX Primer
 
 
UNIX is a computer operating system; and it is the preferred operating system for scientific computing.  Various flavors of UNIX exist, for example Linux and Mac OS X.

On the web, there are many good tutorials on UNIX. You can google them yourself,
or click on one of the following links: home
www.agecon.ucdavis.edu/unix_doc/tutor.htmlhttp://www.agecon.ucdavis.edu/unix_doc/tutor.htmlhttp://www.agecon.ucdavis.edu/unix_doc/tutor.htmlshapeimage_7_link_0
www.ee.surrey.ac.uk/Teaching/Unix/http://www.ee.surrey.ac.uk/Teaching/Unix/http://www.ee.surrey.ac.uk/Teaching/Unixshapeimage_8_link_0
To access the UNIX shell under Mac OS X, you start up the “Terminal” application. Normally, you find this in the “Applications -> Utilities” folder. “Terminal” will give you a window, which looks like: You can now type in commands at the prompt.

Other than Windows, UNIX is not very forgiving. This means that if you delete a file, it is really gone and you can’t get it back. Also, it is case sensitive, so be careful with what you type.

In UNIX, folders are called directories.  Just like in Windows or on the Mac, these are tree-like structures (one trunk with many branches, side-branches etc.).

Some sample commands (which are much better explained in the tutorials above) are:

ls           list all the files in your directory
rm         remove a file, for example “rm this_file”
mv         rename a file, for example “mv old_name new_name”
mkdir    make a directory, for example “mkdir new_dir”
cd          change to a directory, for example “cd new_dir”
pwd       list the current directory

The current directory is called . and the parent directory (the directory 1 step closer to the trunk) is called ..
So the command

cd ..

will bring you one step closer to the trunk. The command

pwd

will then show you the name of the current directory.
The echo command repeats the text that you typed.  This may not seem very useful, but you can also pipe (a UNIX term for send) this to a file.  For example, the command

echo “This text will go into my new file” > new_file

will put the text between the quotes in the file new_file.  The quotes are used, since spaces have a special meaning in UNIX; normally they separate the arguments that you pass to a command. The > sends stuff to a new file;use >> to append something to an existing file:

echo “And this text too” >> new_file

To now list the contents of this file, you can use the cat command:

cat new_file

You can also use the less command, which is helpful for to view large files:

less new_file
Many commands in UNIX have several options, that may change the output or the effect of the command.  The man page lists all these options, for example

man ls

lists all these options for the ls command.

echo is not very useful for editing large files.  To edit large files, one uses special editors.  A word of warning: editors are not the same as word-processors.  They are usually much less user-friendly, and their menus often work by typing commands rather than by pull-down menus. A popular editor is emacs; in fact, emacs is quite a powerful tool.  

emacs new_file

will startup emacs, and you will now edit the file new_file. You can use the arrow keys to move around and enter new text.  To save your changes, you hold the Control key,
and then type the letter x and then the letter s.  Once you accept that this sequence of keystrokes makes no sense whatsoever, emacs will turn out to be quite useful. This weird sequence of keystrokes is denoted by C-x C-s (thus, the first C means the Control key).  Emacs also uses the Escape key a lot, and for some unknown reason calls this key the M (or sometimes Alt) key.  Here are some useful emacs commands:

C-x C-s    save the current file
C-x C-c    quit emacs
C-s          search for a word in the forward direction
C-r          search for a word in the backward direction
M-%         replace a string
C-x C-f    open a file
C-x i        insert a file
          
After using emacs for a while, your head will be full of sequences like this.  There is an easier way however; you can also type the English name of a command. For example,

M-x-goto-line

(with the first M the Escape key) will ask you what line number you want to go, and then brings you there.

M-x-search-forward

is the same as C-s and searches for a word in the forward direction. Emacs can even help you finish a command; for example

M-x-sea-TAB

will give you a list of all commands that start with “sea” (TAB is the TAB key).

Copying and pasting text is also slightly more difficult than in Word, but you can store multiple text selections.  To select a block of text, you type

C-SPACE

at the beginning of the block (with C again the Control key, and SPACE the space bar).
You can then move up or down or left or right, until you are at the end of your selected block.  To copy the block, you type

C-x r x 1

(don’t hold the Control key while typing the r g and 1).  This copies the block to register number 1; as you may have guessed

C-x r x 5

would copy it to register number 5.  You could also have typed

M-x copy-to-register

(with M the Escape key).  To now paste the text of register 5, you type

C-x r g 5

Or, type 

M-x insert-register

You can even copy rectangles; for example, to copy a selected rectangle to register 5 you type

C-x r r 5

You can use 9 registers at the same time.  
Other helpful cut-and-paste commands are:

C-w  delete selected text (again, you select text by pressing C-SPACE at the beginning)
C-y   paste the selected text

Emacs can also let you open 2 (or 3 or 4 or...) files at once:

C-x 2  opens 2 windows
C-x o  switches between these two windows
C-x 1   closes the windows until you have only 1 left

Finally, some commands to quit unexpected behavior (stop emacs bugging you with the questions it is asking):

C-g  quit command
C-]    quit bugging me about windows


Now that you have mastered emacs, let us go back to a couple more UNIX commands.  The ps command shows you what processes you are running:

ps                                     gives a brief summary
ps -edlaf | grep arjan         gives a complete list of all of arjan’s processes

In the last command, the | is again a pipe (UNIX term for sending something): it sends the output of the ps command to the grep command. grep searches for string or patterns.

ps will give you a process number, this is the number in the first column. To end a process, you type kill, followed by the process number:

kill 1234

will kill process 1234. 

You can run processes in the foreground or in the background. If you run something in the foreground, you have to wait until the process is finished before you can give a new command in that terminal. So it may be handy to run certain things in the background, so you can do multiple things at once.

emacs &

This command runs emacs in the background; you will stay in the terminal and won’t see the black emacs bar. (But of course, the process will appear if you give a ps command).

fg

Will put the last command back in the foreground; so now the emacs window will appear.