Unix: Introduction to files

Using a Unix shell account inevitably involves working with files. This page gives a very brief introduction to files under Unix.

The directory structure

Under Unix, files are organised into directories. All directories are arranged into a single tree shaped structure, irrespective of where they are physically located. The root of this tree is called the root directory, and is refered to as "/". The root directory contains various sub-directories, which in turn can also contain sub-directories, and so on. In this way, all directories are "descended" from the root directory.

The location of files can be specified using either absolute or relative paths. Absolute paths start with a "/", and then give their location relative to the root directory, for example: /var/spool/mail/pdw. Relative paths are taken relative to the current directory, for example foo/bar, refers to the file bar in the subdirectory foo of the current directory.

You can get a listing of the files in the current directory by typing ls.

Your home directory

Your home directory is a directory that you and only you have permission to write to. When you first log in to your shell account your current directory is your home directory. You can find out what the full, absolute path of this directory is by typing pwd, but you should never normally need to know this.

You can change your current directory using the cd command. For example, to change into a subdirectory of the current directory called mail, type:

cd mail

To switch back to the parent directory type:

cd ..

At any time you can switch to your home directory, simply by typing cd on its own. When typing commands, you can also refer to your home directory as "~". For example, ~/foo refers to the file foo in your home directory.

File names

Unix imposes very few restrictions on characters that can be included in the name of a file, or on the length of such names. In practice, it is a good idea to avoid whitespace characters (such as space, tab and newline) and other special characters, as these can be awkward to work with.

In contrast to DOS and Windows, Unix does not generally distinguish between filetypes by extensions, all though there are some well known conventions for naming files. Whether a file is a program file, and can be run, is determined by setting the executable file attribute rather than giving it a particular extension.

If a filename starts with a "." it is treated as a hidden file, and will not show in a directory listing when you type ls. To see such files you must give the -a option to ls:

ls -a


Another concept in the Unix filesystem is that of links. Links can be either symbolic or hard. Hard links are alternative names for a file, and are in every way equivalent to the "original" name. More common are symbolic links. These are files that point to another file and have various uses. One place that you will see symbolic links is if you have a virtual web server. If this is the case, the documents for your server are not located in your home directory, but are stored elsewhere in the filesystem. We will create symbolic links in your home directory pointing to the relevant point in the filesystem. This is for convenience so that you do not have to remember the full path to your web server documents, and for most intents and purposes they will appear as if they are in your home directory.