Computers store information in things called files. A file is like a sheet of paper (maybe a very long one) with something written on it. Files are stored in things called directories. We are now going to learn how make, use, and manage files and directories. We will begin by figuring out what our home directory is:
% cd % pwd /u/c/jeremy % echo $HOME /u/c/jeremy
The cd command ( c hange d irectory) used with no arguments takes us from wherever we might be to our home directory. The pwd ( p rint w orking d irectory) tells in which directory we find ourselves for the moment. In the case at hand it is /u/c/jeremy. Don't be concerned for the moment about the /u/c/ part. It is a path, but that is irrelevant for now. Note that echo $HOME has exactly the same effect as pwd.Exercise: Figure out what your home directory is.
Now let us create a short file. For this we use the cat command. Follow the example below carefully:
% cat >dict red: rojo yellow: amarillo black: negro white: blanco blue: azul green: verde <control-d> %
By <control-d> we mean: hold the control key down; while it is down press "d". We have just used cat to create a short English-Spanish dictionary. This dictionary resides in the file dict . We told cat to put what we typed in dict using the "into" symbol, namely > . To tell cat that we were done typing we typed control-d ("d" for "done"). To check that the dictionary is really there and that it was correctly entered we do this:
% ls dict % cat dict red: rojo yellow: amarillo black: negro white: blanco blue: azul green: verde %
The ls command l ist s the files in the current directory. For the moment there is only one, namely dict. The command cat shows us what is in dict.
Now that we know how to make small files and view them, let's learn how to print them. Here's how:
% print dict Printing dict2 (text) on jwb129lab1 %
Print is not a standard Unix command. For this see lpr. The print command will try to figure out what kind of file you are trying to print and use the method it deems best. For more information on what it does, type the command with no arguments:
% print ... displays info on print ... %
If you need a list of printers, use print -l. The "-l" is an option for the print command. Many Unix commands have options.
Note for those who need it: The print command understands dvi and postcript (ps) files. For example, print foo.dvi correctly prints foo.dvi, and print bar.ps correctly prints the postscript file bar.ps. If you need to force a file to be printed as text use print -t, e.g., print -t weirdfile.
Unix has some useful commands for examining files, e.g., counting the number of words, seeing whether a particular word is in the file, sorting the file, etc. We will learn about a few of these commands now:
% wc dict 6 12 78 % grep white dict white: blanco % sort dict black: negro blue: azul green: verde red: rojo white: blanco yellow: amarillo %
The wc command c ounts w ords (and more). In the case at hand it tells us that dict contains 6 lines, 12 words, and 78 characters ("letters "). The grep command looks for the word white in the file dict and displays the lines in which this word appears. It gives us a way to search through files. The sort command does just what it says.
Before going on, let's see how to save a copy of our sorted dictionary. We'll put in a file called dict2.
% sort dict >dict2 % ls dict dict2 % cat dict2 black: negro blue: azul green: verde red: rojo white: blanco yellow: amarillo %
Notice once again the use of the "into" symbol ">". In our example it had the effect of directing the output of the sort command from the screen to the file dict2. Just to be sure that everything went according to plan, we used ls to be sure that dict2 was there, and we used cat dict2 to be sure that it contained what we thought it should.
Timeout: after working through the last example, stand up and stretch. Then congrutalate yourself for having made so much progress learning Unix.
% ls dict dict2 % rm dict2 rm: remove dict2? y % ls dict %
This is enough work for the second lesson. You now know how to create, view, print, and remove files, and you know how to manipulate them: to search for words and generate statistics on them. The main problem is what to do about creating longer files. It is too much to hope that you can do this with no typing errors using cat. For this reason you need an editor. This is computer jargon for a program for creating files, putting text in them, and modifying that text. We recommend emacs .
Do as before: Add the names of all the commands you have used in this lesson to your sheet of paper. Carry it around with for a few days, and use it to review the commands several times. Do this both at a workstation and away from it. Finally, explain to a coworker or fellow student what the commands do. Do this at a workstation so you both can try things out.
Back to tutorial | On to next lesson