Unix File System

Revised January 14, 2001. Press your browser's Reload or Refresh button to get the latest version.


Files and directories

A file is a piece of permanent storage in a computer. Files store documents, images, programs, etc. Every file has a filename. A directory is a file that contains a list of information about other files: their names, locations, sizes, owners, etc. Since a directory is just a kind of file, a directory can list other directories.

Unix users often speak as if directories were containers for files, saying a directory "contains" files or a file is "in" a directory. This is a bit misleading because directories do not contain the files themselves, they merely contain a (short) bit of information about each file. When you move a file from one directory to another, the file itself does not move; you merely move the (much shorter) directory entry.

Unix uses a hierarchical directory structure where directories are organized into a tree. The directory at the root of the tree is named root and is spelled / (just a single slash character). Root lists directories: its subdirectories, which have root as their parent directory. Each subdirectory lists its own subdirectories, etc., creating a branching structure as in this example.

Absolute pathnames

Every file can be identified by a unique absolute pathname formed by listing all the directories in order from root, ending with the filename. Absolute pathnames always begin with / (root). Each directory in the pathname is preceded by its parent and is followed by a subdirectory (or the filename). For example:


    /usr/users3/jackyj/web/fofc-lab.html

This identifies the file named fofc-lab.html, which is listed in the directory named web, which is a subdirectory of jackyj, whose parent is users3, in usr, in / (root).

Note that this is a different file from


    /usr/users3/jillk/web/fofc-lab.html

because one of the directories in the path is different.

Relative pathnames and the working directory

Absolute pathnames can be long and cumbersome to work with. Unix always provides each process (session at the computer) with a working directory (also called the current directory). This allows you to use a shorter relative pathname that does not begin with root. When the Unix sees a relative pathname, it implicitly prefixes the working directory to form an absolute pathname. When you log in your working directory is set to your home directory (also called your login directory). There are commands that change your working directory.

For example when I log in my working directory is set to


    /usr/users3/jackyj

When I type the relative pathname


    web/fofc-lab.html

Unix interprets this to mean the file whose absolute pathname is


    /usr/users3/jackyj/web/fofc-lab.html

The user jillk can type the same relative pathname, but Unix interprets this as a different absolute pathname because she has a different working directory.

We often use a geographic metaphor, speaking as if directories were places. We say we are "in" the working directory, we "move to" another working directory, we "move up to" the parent directory etc.

Abbreviations

Unix provides some convenient abbreviations for writing pathnames:

.working directory
..parent directory
~Your own login directory
~useruser's login directory

I can copy the file test.txt from jillk's login directory to my own working directory by using this command.


    cp ~jillk/test.txt .

This command has the same effect "anywhere" (in any working directory) because the abbreviations ~jillk and . have the same meaning in any working directory.

Basic commands

These are the basic commands for working with files and directories. Some commands have different effects when an argument is a directory (instead of an ordinary file). Be warned that some commands are destructive -- they can remove or replace existing files without warning.

cat > file
contents
Ctrl-d
Create file containing contents. This is a quick way to create short test files without using an editor.
more fileDisplay file a page at a time. Press space bar for next page, q to quit. Only works on text files, not directories, images etc.
command | moreDisplay output from command a page at a time. Useful for listing long directories etc.
Ctrl-cQuit command, get back to shell prompt (hold down Ctrl key while pressing c key).
pwdprint working directory
mkdir dirCreate directory dir
cd dirChange working directory to dir
cd ..Change working directory to parent directory (move up)
cdChange working directory back to login directory
ls List names of files in working directory
ls -aList files, including hidden dot files.
ls -FList files, using / to indicate directories etc.
ls -lList files, long listing with much information
ls -l dirLong listing of files in directory dir
ls -l fileLong listing about file only
ls -ld dirLong listing about the directory dir only
mv file1 file2Rename file1 to file2. If file2 already exists, it is destroyed (overwritten).
mv -i file1 file2Rename file1 to file2. If file2 exists, warn about overwriting.
mv file dirMove file into directory dir.
mv files dirMove each of files into directory dir.
mv dir1 dir2If directory dir2 exists, move dir1 to be a subdirectory of dir2.
If dir2 does not exist, just rename dir1.
cp file1 file2Copy file1 to file2. If file2 already exists, it is destroyed (overwritten).
cp -i file1 file2Copy file1 to file2. If file2 exists, warn about overwriting.
cp file dirCopy file into dir.
cp files dirCopy files into dir.
cp -r dir1 dir2 Copy dir recursively: copy the directory, its contents, all its subdirectories etc.
If dir2 does not exist, create it. If dir2 exists, create a new subdirectory.
rm filesRemove (delete) files. Careful, they cannot be restored.
rm -i filesRemove files, warn first.
rm -r dirRemove dir recursively: remove the directory, its contents, all its subdirectories etc.
rmdir dirRemove dir. The directory must be empty.

Jon Jacky, jackyj@evergreen.edu