Unix--the Bare Minimum Norman Matloff February 25, 1995 Modified by Scot Kleinman 30Apr97 The information here is intended to be a review for those who have had a bit of prior exposure to Unix, and as a quick introduction to Unix for those who have never seen it before. (Some of the material may be new even to those with some prior exposure to Unix.) On Unix systems, the command reader/processor is called a shell. We use a popular shell called the TC-shell; its official name, taken as a command itself, is tcsh. Unix uses a hierarchical file system, meaning the following. When you first log in, you will be at a point in your file system known as your home directory. Within that directory you can make subdirectories, and within them you can make sub-subdirectories, and so on. So, your file system has a tree-like shape. (And your file system is in turn a subtree of the collection of all files on the machine.) This is very similar to directories in Windows95 and DOS. Your Unix home directory will be called /home/MACHINE_NAME/USER_NAME. For example, on Begonias, my home directory is /home/begonias/sjk. I also have on on Sundowner which is /home/sundowner/sjk etc. To create a directory, use the mkdir command. Suppose, for example, you are in your home directory, and wish to make a subdirectory named files. You could type mkdir files To go from one directory to another, use the cd command. For example, if you are currently in your home directory and you have created the files subdirectory, simply type cd files However, suppose you are in your bin subdirectory (the bin directory was installed in your account when it was made) and you wish to go to files. The above command won't work, since files is not a subdirectory of bin. Instead, you can type cd ~/files with the tilde mark signifying your home directory. In other words, you are saying, ``Change to files, which is a subdirectory of my home directory.'' Thus, if you want to get back to your home directory at any time, simply type cd ~ or more simply cd or you can type me which is an alias I created in your .cshrc file. (Aliases are simply synonyms for other commands, usually made to simplify memorizing and typing long command strings. The directory which is up one level in the directory tree can be referred to as ``..''. Thus for example, cd .. would take you up to that level. As another example, an alternate method for moving from the bin directory to the files directory in the example above would be cd ../files To see which directory you are currently in (theoretically you should know, but sometimes you might lose track of which directory you are in), type the pwd command. To get a list of all files you have in the current directory, use the ls command. This command is more useful, though, if you use the -F option so I have aliased ls to be ls -F so you can simply type ls This will tell you which files are executable (their names will be appended with asterisks), and which are subdirectories (their names will be appended with slashes). By the way, ls will not report files whose names begin with a period (there typically are several of these); to see those, add the -a option, e.g. type `ls -a'. 'ls -l' gives you a more detailed listing. To remove (delete) a file, use the rm command. E.g. rm x will result in the file x being deleted. You cannot get a file back after removing it, so be careful (particulary when using wild cards: 'rm *' will remove all files, 'rm *.hz' will remove all files that end in ".hz", 'rm unix.*' will remove all files that start with "unix" and 'rm a?b' will remove all files with 3-character names beginning with "a" and ending with "b", ex. acb,aab, etc.) To rename a file, use mv. E.g. mv x y means ``move x to y,'' i.e. take the file x and rename it as y. If file y already exists, it will be replaced by x. Unix is not very forgiving. All these commands above can be used on directories other than the current one. For example, if we are currently in the directory bin but wish to know what files are in the directory files, we could use cd to go to the latter directory and then use ls there, but it is easier to just type ls ~/files from bin, not leaving that directory. Similarly, if we had a file z in bin which we wanted to copy to files, we could type (from the bin directory) cp z ~/files/z In fact, even cp z ~/files would work. The symbol `.' refers to the current directory. For example, if we are currently in bin and there is a file abc in the files directory, mv ~/files/abc . would move it to the current directory. To create a new file or modify an old one, we use a text editor. A widely-used editor in Unix and other systems is vi. For example, vi x would be used to create the file x, or to modify x if x already existed. You will need to know vi (or some other editor) in order to do many Unix operations. So, you should learn it now, before going any further. See the introduction given in the Appendix to this document. Many people hate vi as it borrow its archaic syntax from Unix. It is very powerful, however, and the chief advantage of using it is that all Unix machines will have it in some form or another meaning you won't have to learn more than one new editor to master Unix editing. We also have pico, a very simple to learn although not very powerful editor, and emacs which is very powerful, usually easier to learn than vi, but not quite as universal, although it is gaining ground quickly. Note: To use vi or most other editors, Unix needs to know which kind of terminal you are using. If you log in at the machine console itself, this is no problem, but in general you will need to state the terminal type, which is typically vt100. If you get an error message, try typing setenv TERM vt100 (Alternatively, you can type ``set term = vt100''. The system sees to it that the two variables TERM and term are kept up to date with each other, so it doesn't matter which one you use.) You can view a file by using an editor, but usually it is quicker just to use the more command. For instance, more uv would display the file uv on the screen, one screenful at a time; just hit the space bar whenever you are ready to go to the next screenful. If you wish to discard the remaining screenfulls, just type q (for ``quit''). If you want to back up a screenfull, type b. Sometimes you will find it useful to save the output of a command. You can do this by redirecting the output to a file. For example, ls > y will send the output of the ls command to a file y, instead of to the screen. If the file y already exists, it will be replaced with the new date - be careful. footnote: Similarly, if a command expects input from the keyboard, you can have it read from a file instead, by using `<'. The command ls >> y will append the output of ls to the file y. An error will result, however, if y does not yet exist. Often it is useful to pipe the output of one command as input to another command. Say you find the output of ls to be very long, zooming by on the screen before you have had a chance to read all of it. One solution to this problem would be to send the output to a file and then view the file as your leisure, but an easier, more direct method would be to pipe the output of ls into more, i.e. ls | more which would allow you to see the output of ls one screenful at a time; again, you would hit the space bar whenever you are ready to go to the next screenful. You can get online information on almost any Unix command, by using man. For example, to get information on all the options available for the ls command (there is a very large number of them), type man ls and a detailed (though terse) description of everything ls does will then appear on the screen. footnote: NOTE CAREFULLY: This is how most people learn more about Unix--by reading these ``man pages.'' By the way, this description will be automatically piped through more, so as usual, just hit the space bar when you are ready to go to another screenfull. If you want to find commands that pertain to some keyword, use the 'man -k' (which I have also aliased to 'help') command, then do a 'man' on the command of interest. For example, man -k postscript produces the following output: pdf2ps (1) - convert an Adobe PDF file to Postscript Tk_CanvasPsY, Tk_CanvasPsBitmap, Tk_CanvasPsColor, Tk_CanvasPsFont, Tk_CanvasPsP ath, Tk_CanvasPsStipple (3) - utility procedures for generating Postscript for canvases dvips (1) - convert a TeX DVI file to PostScript grops (1) - PostScript driver for groff mpage (1) - print multiple pages per sheet on PostScript printer nenscript (1) - format an ASCII file and convert to PostScript pbmtoepsi (1) - convert a portable bitmap into an encapsulated PostScript style preview bitmap pbmtolps (1) - convert portable bitmap to PostScript pfbtops (1) - translate a PostScript font in .pfb format to ASCII pnmtops (1) - convert portable anymap to PostScript ps2pk (1) - creates a TeX pkfont from a type1 PostScript font psbb (1) - extract bounding box from PostScript document psidtopgm (1) - convert PostScript "image" data into a portable graymap ghostview (1) - View PostScript documents using ghostscript To end your Unix session, simply type exit or sometimes (depending on how you logged in) logout You will also have to logout of your X11 Window seesion, which will be discussed in another document. To stop a Unix program while it is running type a Ctrl-c (sometimes, if it is waiting for input, for example,, a Ctrl-d is needed instead. Ctrl-z merely suspends a progam and leaves it in a frozen state- don't use this unless you know what you are doing. Ctrl-Z is discussed in anothe one of these blurbs.) Introduction to Vi As a brief introduction to vi, go through the following: First, type vi x at the Unix prompt. Assuming you did not already have a file named x, this command will create one. (If you have tried this example before, x will already exist, and vi will work on it. If you wish to start the example from scratch, simply remove x first or use a different file name.) The file will of course initially be empty. To put something in it, type the letter `i' (it stands for ``insert-text mode''), and type the following (including carriage returns at the end of each of the three lines): The quick brown fox will return. Then hit the Escape key, to end insert-text-mode. Now save the file and exit vi, by typing `ZZ' (note the capitals). The key to learning vi is to keep in mind always the difference between insert-text mode and command mode. In the latter mode, as its name implies, one issues commands, such as the ZZ above, which we issued to save the file and exit vi. The characters you type will appear on the screen if you are in insert-text mode, whereas they will not appear on the screen while you are in command mode. By far the most frequent problem new vi users have is that they forget they are in insert-text mode, and so their commands are not obeyed. For example, suppose a new user wants to type ZZ, to save the file and exit vi, but he has forgotten to hit the Escape key to terminate insert-text mode. Then the ZZ will appear on the screen, and will become part of the text of the file--and the ZZ command will not be obeyed. You now have a file named x. You can check its contents by typing more x which will yield The quick brown fox will return. just as expected. Now let's see how we can use vi again to modify that file. Type vi x again, and make the following changes. First, let's say the fox will not return: Type `/re', which instructs vi to move the cursor to the first instance of `re' relative to the current cursor position. (Note that typing only `/r' would have moved the cursor to the `r' in `brown', which is not what we want.) Now use the `i' command again: Hit `i', then type `not ' (note the space), and then hit Escape. Next, let's delete the word `brown'. Type `/b' to move the cursor there, and then hit `x' five times, to delete each of the five letters in `brown'. (This will still leave us with a blank line. If we did not want this, we could have used the `dd' command, which would have deleted the entire line.) Now type `ZZ' to save the file and exit vi. Use `more' again to convince yourself that you did indeed modify the file. You now know how to use vi to insert text, move the cursor to text, and delete text. Technically, the bare-bones set of commands introduced above is sufficient for any use of vi. However, if you limit yourself to these few commands, you will be doing a large amount of unnecessary, tiresome typing. So, you should also learn at least some of these other frequently-used vi commands: h move cursor one character to left j move cursor one line down k move cursor one line up l move cursor one character to right arrow keys also move cursor one character up, down, left, or right w move cursor one word to right b move cursor one word to left 0 move cursor to beginning of line $ move cursor to end of line nG move cursor to line n G move cursor to last line of file ctrl-f scroll forward one screen ctrl-b scroll backward one screen i insert to left of current cursor position (end with ESC) a append to right of current cursor position (end with ESC) dw delete current word (end with ESC) cw change current word (end with ESC) r change current character dd delete current line yy copy current line, leaving it in place but able to be put with the p command below x delete current character ma mark currrent character d`a delete everything from the marked character to here p dump out (put) at current place your last deletion u undo the last command . repeat the last command J combine next line with this one :w write file to disk, stay in vi :q! quit VI, do not write file to disk, ZZ write file to disk, quit vi /string search forward for string ?string search backward for string n repeat the last search :s/s1/s2 replace (the first) s1 in this line by s2 :a,b/s/s1/s2/g replace all instances of s1 in the line range a to b by s2 (where a and b are either explicit line numbers, or . (current line) or $ (last line) Note: The `a' command, which puts text to the right of the cursor, does put you in insert-text mode, just like the `i' command does. Many of the commands can be prefixed by a number. For example, 3dd means to delete (consecutive) three lines, starting with the current one. As an another example, 4cw will delete the next four words. The p command can be used for ``cut-and-paste'' and copy operations. For example, to move three lines from place A to place B: 1. Move the cursor to A. 2. Type `3dd'. 3. Move the cursor to B. 4. Type `p'. The same steps can be used to copy text, except that p must be used twice, the first time being immediately after Step 2 (to put back the text just deleted). ---------------------------------------------------------------------------- Norm Matloff Wed Nov 8 18:00:45 PST 1995 Last modified April 30, 1997