Files In the small, closed Unix universe, the most popular topic of conversation is files. A "file" is just a thing with a name and data in it. Most things that you do, you do to files or their contents. There are two main types of files, simple files and directories. We will begin by discussing simple files and leave directories to the next section. File Names File names can be from one to 256 characters long. You can use any character on the keyboard in a file name, but it is wisest to stick to a-z, A-Z, 0-9 and the . (period or "dot"), and _ (underscore) characters. If you use any of the other characters, you may be letting yourself in for serious trouble. As noted earlier, case matters in file names. myfile is a different file from Myfile. Name Conventions You often see Unix files named with "extensions," for example: program.h, paper.mss. The ".characters" is the extension. People do this for two reasons. First, to remind themselves what type of file it is. A .h file is a C language "include" file. A .mss file is a Scribe text formatter file. Second, because some Unix programs, notably the compilers, require that language source files have proper extensions; .c for C, .f for Fortran, and .p for Pascal. Some file names start with a . (dot), e.g., .cshrc or .login. The only significance to this is tht these files do not normally show up when you ask for a list of your files. Background, utility, "start up" files are named beginning with .. Some examples are discussed below in the section "Exploring Your Environment." Wildcards: * and ? Unix provides a shorthand notation for referring to many files at once called "wildcard characters." There are many wilcards. Here are the two most useful: * Matches all files except those whose names begin with . (dot). It can also match characters within a filename. For example, *.f means "all the files with a .f extension. h.f and verylongfilename.f would both match. a* means "all the files that begin with a" File names a and anothernameforyou would both match. ? Matches single characters. ?.f means all one character file names with extensions of .f. a.f would match. ab.f would not. The section on Directories has examples of wildcard characters in use. To Get a List of Your Files: ls The command to get a list of the files in a directory is ls. If you are a new Athena user and you type ls, this is roughly what you will see: host% ls Mail welcome ls by itself lists just the files names, alphabetically (A-Z before a-z) in as many columns as will fit across the screen. The ls command has many options. To see all the options, enter man ls while logged in at a terminal. Here are the most important options. To get a list of all of the files in a directory, including those whose names begin with a . (dot) character, use the -a option (for "all"). A new user doing this would see something like: host% ls -a . .emacs_keys .logout .tiprc .. .emacs_vars .mailrc Mail .cshrc .login .mh_profile welcome The difference, then, is that ls doesn't show the files whose names begin with ., and ls -a does. Just what all these files are is explained below under "The Files that Come with Your Username" in the "Exploring Your Environment" section. To get a "long" listing of your files that shows much more information about them, use ls -l. host% ls -l drwx--x--x 1 you 2 Nov 3 1983 Mail -rw------- 1 you 0 Nov 3 1983 mbox -rw-r--r-- 1 you 404 Nov 27 1983 welcome The first set of information on the line is the file's "mode." A "d" in the first column means that the file is a directory. The r's and x's that follow show who has permission to read, write, delete, and execute the file. This is explained in more detail in the section on "File Protection" below. The second column shows the number of "links" the file has. We won't explain links here. For directories, it shows how many subdirectories exist beneath the file. The third column shows the username that owns the file. In most cases this should be your username. The fourth column column shows the size of the file in "bytes." In a text file, this is the number of characters in the file. The fifth column shows the date when the file was last modified. If a file has never been modified, it shows when it was created. The final column shows the name of the file. You can use other options on the ls command to get it to show you other information about your files. To get a long listing of all the files in a directory, you would enter ls -la. There are different types of files: text files, files that are directories to more files, and executable binary files. Often, you need to know the type of the files being listed. The -F (note, uppercase F) will show this: host% ls -F Mail/ a.out* myfortpgm.f welcome Notice the suffix characters / and * that are now displayed. These characters are NOT part of the filename. The / character means that the file is a directory. The * character means that the file is an executable binary. Nothing means either a plain text file, or that Unix cannot recognize the type of the file. To See the Contents of a File: cat and more There are two commands that willshow you what's in a file. The simplest is the cat command (short for "catenate"). The format of the commands is: cat filename. For example: host% cat .login stty dec setenv PATH /usr/athena:/usr/new:/usr/new/mh: /usr/ucb:/bin:/usr/bin:/usr/local:~/Bin:/usr/games:/usr/hosts setenv VISUAL /usr/athena/xemacs setenv EDITOR /usr/athena/xemacs setenv MORE -cs set ignoreeof host% The contents of the file are shownon the screen. If the file is very long, it will zip by you on the screen faster than you can read it. (CTRL-S and CTRL-Q don't work.) In this case you should probably use the more command instead of cat. You can cancel a cat command and stop its output at any time; enter CTRL-C. Problem: I "catted" a file, but I got a lot of gobble-dee-gook and the terminal acted stranged, beeping a lot. Some kinds of files are "unprintable." They contain data in a format that cannot be displayed on a terminal screen. Binary files (the ones ls -F shows with a * suffix) are not printable. Directory files (ls -F shows with / suffix) are not very printable. It is very common when you start out to say ls when you meant cat and cat when you meant ls. The more command displays a file's contents one screen at a time, pausing after every screenful so that you can read it. The format is: more filename. Most people use more in preference to cat. Once more has filled up a screen, it will not go on until you make it. The typical commands are: SPACEBAR give me the next screenful RETURN give me one more line q or CTRL-C quit, I've seen enough Alas, more cannot back up. More is a very interesting program. If one were making an "all-time Top-10" list of Unix commands, more would be near the top. It is clever enough not to display binary and directory file. There are commands that let you browse through a file for specific words, or invoke a text editor to edit the file. Most interesting is that more is what is called a Unix "filter." Many other Unix commands "pipe" their output through more. This means that instead of giving you their output directly--where it may zip past faster than you can read it the way cat does-- they send it through more first so that it will be easy to read under your control. The man command does this. You, too, can "pipe things through more." Whenever you enter a command and its output is too copious and rapid to manage, cancel the command with CTRL-C and reenter it like this: command | more The | is the "pipe" character. For example, if you have collected hundreds of files, you could pipe the output of ls through more: host% ls -l | more Look at man more to learn more about its powers. To Create a File There are many ways to create a file. The normal way to create a file is with the Emacs text editor. The free Athena document Essential Emacs, explains how to do this. A "quick and dirty" way to create a file is with the cat command (short for "catenate"). It works as follows. host% cat >easyfile Unix just moves the cursor down to the next line. You begin to type. End each line with a RETURN. You can fix typing mistakes with the DELETE key in the line you are currently typing. There is no way to fix previous lines. When you are done, finish the last line with a RETURN and then at the beginning of the next line enter just CTRL-D. It echoes as ^D. ^D host% ls Mail easyfile welcome There is no way to fix mistakes in this file short of deleting it and re-entering the whole thing. Don't forget the >filename part. If you do, cat will just echo your input to the terminal and create no new files. The >filename is what tells cat to save it as a file instead. You can take the output of any Unix command and make it into a file. For example, the command that shows you what electronic mail messages you have is scan: host% scan 102 2/10 cec (Cecilia d'O Re: IPS educational and consulting 104 2/10 jg (Jim Gettys) UUCP outgoing mail. <mymail host% ls Mail easyfile mymail welcome The ">filename" at the end of the command line is the redirection mechanism. Be sure your arrow is pointing the right way. It is all right to have a blank between the > and the filename you have selected. The "quick and dirty" mechanism shown above is really just redirecting the output of the cat command to a file. To Delete a File It is a good idea to delete files as soon as you know you won't be needing them anymore. Otherwise, your directory can become very cluttered and you will eventually run out of your alotted file storage quota and begin to have problems. The command to delete a file is rm (short for "remove"): host% rm easyfile host% Unix doesn't verify the deletion, it just prompts for the next command. You can put more than one name on the line. You can use wildcards (with caution!) to delete whole classes of files. To Copy a File The cp command copies files: host% cp file1 file2 host% Unix copies from the existing file1 to the new file2. If file2 already exists, it will be overwritten. Again, Unix does not verify the copy. Copies from one machine to another work slightly differently. See the section below on "Dealing with Other Machines." To Rename (Move) a File To rename or move a file, use the mv command: host% mv file1 file2 host% Unix renames file1 to file2, in effect "moving" it from one file to another. If file2 already exists, it is overwritten. As ever, Unix does not confirm that it has done the operation.