Directories All files in the Unix system are stored in one large hierarchical tree structure. Everybody's files are somewhere in this tree, occyping some sub-branch: the system's files, your files, other users' files, the mail files, the documentation files. See the picture. Learning your way around this tree structure, gaining a rough idea of what's kept where, and knowing how to get to it, are fundamental to your understanding of Unix. Without it you can function, but you will always feel a little confused, a little out of it, like a stranger in a group of old friends who just doesn't quite ever get it. The tree structure is made possible by a special kind of file, a directory file. A directory file is nothing but a list of pointers to other files. These other files can be text or binary files, but they can also be more directory files (often called subdirectories). These subdirectories can, in turn, point to still more directory files down to many levels of depth. Up to know we've seen only simple filenames like myfile or fortprogram.f. A file's full name is its "given" name precded by the path of directories that the system must follow to reach that file in the Unix tree structure, separated by the / (slash) character. Look again at the picture. At the top of the tree is a file called "the root directory" because it roots the whole tree. Its name is / (just a simple slash). Below that are a set of major directories like bin, dev, etc, mit, projects, sys, and usr. Now look at the mit directory (proper name: /mit). This is the major subtree where user's files are kept. Below it are more subdirectories, one for each letter of the alphabet (/mit/a). Below each of these the same alphabetic pattern is repeated (/mit/a/a). Strung out underneath these doubly-alphabetized directores are directores named after the usernames of everyone on a particular machine. User dkfritz has a directory /mit/d/k/dkfritz. The Working Directory We said that a file's full name is its given name preceded by the path of directories one must follow to find it. Why don't you have to give the file's full name every time you specify it? You don't because the system keeps a value for you called the working directory. (Also known as a "default directory," or "the current directory.") Whenever you specify a simple filename, the system assumes that you are talking about a file in your working directory. A working directory is, simply, the directory you are currently "in". You can change your working directory at any time. The system provides a few short-hand synonyms for the working directory. A simple . (dot) used in a directory specification means "the current working directory." As a shorthand for "the directory above my current working directory," the system recognizes .. (two dots together. Examples using these shorthand synonyms are given below. The HOME Directory Unix also employs the concept of a HOME directory. This is the beginning of your subtree on the main tree, where all the files that belong to you are stored. For user dkfritz above, it was /mit/d/k/dkfritz. Your own HOME directory is named according to the same pattern. You can verify your HOME directory by typing: printenv HOME When you login, your working directory is set to be your HOME directory. Thus, unless you explicitly say otherwise, the system assumes that you are always talking about your own files stored under your HOME directory whenever you give a filename in a command. Synonyms for HOME Directory: ~/ Typing out /mit/d/k/dkfritz everytime you want to discuss the HOME directory of a user is tedious. Unix provides shorthand synonyms for HOME directories. To say, "my HOME directory" type: ~/ To say, "user dkfritz's HOME directory" type: ~dkfritz To Make a Directory: mkdir You can make subdirectories under your HOME directory to better organize your files. Saying: host% mkdir forclass will make a directory called forclass under the current directory. Unix, as usual, will not verify that it has created the directory. To Change the Working Directory You will often want to temporarily change your working directory from your HOME directory to somewhere else on the tree. Use the cd (for "change directory") command. For example, if you had just created a subdirectory forclass and now wish to make and modify files in that directory (or "work in" that directory, as the saying goes) you could tediously specify those files as forclass/assignment1 and forclass/lectnotes. Or, you could change your working directory to forclass and speak of them as just assignment1 and lectnotes: host% cd forclass Sometimes, you may be working somebody else' directory, somewhere else on the tree. Rather than always typing out /mit/o/t/otheruser, you can just change the working directory to their HOME directory: host% cd /mit/o/t/otheruser or host% cd ~otheruser Perhaps you've gotten curious about how Scribe works and wish to rummage around in the Scribe database files. Switch to that portion of the tree with: host% cd /usr/athena/lib/scribe_data To Find Out What Directory You're In As you start hopping around the tree with cd commands, it is easy to forget where you are. You can always find out your current working directory with pwd (print working directory). For example: host% cd /usr/prototype_user host% pwd /usr/prototype_user To Return to Your HOME Directory The quickest way to change your working directory back to your HOME directory, no matter where you have wandered in the tree is: host% cd cd defaults to your HOME directory. This is equivalent to saying cd ~/. To Delete a Directory: rmdir You cannot delete a directory unless all of the files underneath it are gone. This prevents you from accidentally wiping out important subtrees with one careless command. A quick way to delete all the files in a directory you want to get rid of is to change to that directory with cd, then: host% rm * Be careful before you do this! There's no going back. If the directory also has .files in it, you must say: host% rm *. You can then delete the directory itself. Move out of the directory back to the one above it, then type: host% rmdir dirname The -r Recursive Option Many commands will have a -r or -R "recursive" option. When talking about directories, recursive means "do it to this directory and all the files and subdirectories underneath it." R options are thus very powerful and should be used with caution. For example, one could perform the two steps required to delete a directory in one step as follows: host% rm -r dirname For some reason, -r only works with rm, not with rmdir. A sleepy programmer logged in as the user "root", that special all-powerful username that system managers use to control the system, could take out the whole file system with just: host% rm -r / The recursive option on the cp (copy) command makes it easy to copy entire subtrees from one place to another. There are examples of this in the next section. Sample Directory Specifications It takes a little practise to get the knack of correctly typing filenames and directories so that you get the files you want. Here are some common specifications. All of these have synonyms that would work just a well. a directory called foo below my current one: foo (as in cd foo) a file stuff in directory foo below my current one: foo/stuff all the files in directory foo below my current one: foo/* my HOME directory: ~/ a file stuff in my HOME directory when I'm in my HOME directory: stuff a file stuff in my HOME directory when I'm someplace else: ~/stuff a file stuff in user sam's HOME directory: ~sam/stuff a file stuff in user sam's subdirectory foo: ~sam/foo/stuff up one directory: .. (as in, pop me up one level: cd ..) a file stuff in the directory above me: ../stuff a file stuff in the directory foo in the directory above the one I'm in: ../foo/stuff copy my file george into my subdirectory archive: cp foo archive copy sam's file foo into my current directory, and keep the same name: cp ~sam/foo . copy sam's file foo into my current directory, but with a different name: cp ~sam/foo fromsam copy all of the files in sam's subdirectory foo, including all subtrees into my current directory, and keep all the names the same: cp -r ~sam/foo . Problem: ls /Mail: Permission denied. This is one manifestation of a common mistake people make when learning about directory specifications. This person was trying to list out the files under the Mail directory. Your first reaction is to feel indignant about not being allowed to look at your own mail files. The problem is the / A slash at the beginning of a directory specification means the whole system's root directory. Thus, you aren't naming your Mail directory, you're naming a Mail directory immediatly under the root directory. This happens to belong to the system manager, who doesn't want you looking at his/her mail. Never start a directory specification with / unless you explicitly mean one of the directories immediately below the whole system's root directory. (I.e., like /etc, /mit, and /usr.) System File Organization: An Aside This section on directories has emphasized talking about your own, and possibly a neighbor's files. but as anyone new to Boston must, over time, construct a mental map of the city, its suburbs, squares, T stops, restaurants, movie theatres, and points of personal interest beyond their immediate neighborhood, so should you get an inkling of what is kept in other parts of the Unix file system. Here's a nickle tour. Start at the root directory and enter: host% ls -F / (The -F is there to show you which files are directories, text files, and binaries.) You will see something like: Mail/ etc/ mnt/ src/ u2/ bin/ lib/ projects/ src.log usr/ boot* lost+found/ restoresymble staff@ vmunix* dev/ mbox site/ sys/ vmunix.0.11 mit/ source/ tmp/ vmunix.0.3 Each of these root level directories contains different sorts of files that the system needs to function. There are strong historical conventions for what goes in each of these root directories (as well as many arbitrary exceptions). You can look at most of these files. Except for the /mit subtree and a few other exceptions, Unix is open for inspection by anyone curious enough to look. The /bin directory contains the binary (unreadable, excecutable machine language) versions of the programs you absolutely can't bring up a Unix system without, such as the cat and rm commands. When you type the cat command, you are actually executing the file /bin/cat in this directory. The /etc directory contains all the administrative information files necessary to run the system. This includes the /etc/passwd file that defines who has a username on the system. It makes for interesting browsing. The /mit directory, as you already know, contains all user files. The /projects directory contains all the files for Athena projects. You may not have a /source or /src directory. It normally contains all the source code for the entire system. Perhaps the most interesting directory is /usr. /usr has traditionally been where all users' files were kept. (Athena uses /mit for this.) Later, it also came to be the place where documentation, games, and programs added at the local site were kept. For example, /usr/ucb has all the programs that the University of California at Berkeley added to the original Unix to make Berkeley 4.2. /usr/athena has all the programs, like Scribe and Emacs, that Athena is adding to the Unix it got from Berkeley. /usr/doc contains documentation of general interest to users. /usr is worth quite a bit of browsing. To get a detailed outline (somewhat out of date) of this entire system tree, enter: host% man hier