Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge > Department of Engineering

Unix Links and Filesystems

A file system is a set of files plus tables of information about those files. All unix machines have a filesystem whose root is /. Onto this can be mounted other file systems. Typing df or bdf will display the mounted file systems and show where their roots are mounted. The I-node table is the focus of all file activity in UNIX. Within each file system there is a unique inode allocated for each file of every type. It is natural but erroneous to think of files being "in" directories. In fact directories contain the file names and corresponding indices into the i-node table. Each entry in a directory is of the form

[file name]    [i-node number]

meaning that all the information about the file referenced by file name is contained in the entry for i-node number in the i-node table.


The i-node of a file can be found using "ls -i". Two or more file names can be associated with the same i-node (ie the same file) using the "ln" command.

	     touch spon   - to create a new file "spon"
	     ln spon spon_too
will create two names for the same file. Note that "spon" and "spon_too" are on an equal footing. They are both "hard links": "spon_too" is in no way inferior to "spon".

The i-node entry for a file contains a field that stores the number of links made to that file. If you continue the previous example with

ls -li spon*

then you will see that the file names refer to the same i-node entry (1st column) and the 3rd column shows that this file has 2 links to it (namely "spon" and "spon_too").

  1673 -rw-------   2 xyz99      ugrad            0 Oct 12 02:21 spon
  1673 -rw-------   2 xyz99      ugrad            0 Oct 12 02:21 spon_too

A file doesn't disappear until all the hard links to it have been removed, so doing

rm spon

will not remove the file that "spon" referred to, only this link to it.

Note also that you can't hard link between file systems ; each file system has its own i-node table so an i-node index is not unique across file systems.

Symbolic links

If you do:

rm spon*

touch spon

ln -s spon spon_too

ls -i

you will see that "spon" and "spon_too" no longer reference the same i-node.

A symbolic link is a (sort of) string alias; wherever you mention "spon_too" it will be substituted for by "spon". "spon" and "spon_too" no longer have the same status; if you do:

rm spon_too

you will only remove the alias whereas if instead you do:

rm spon

you will remove the file that "spon" referred to, and "spon_too" will be left pointing to a nonexistant file.

To see if an entry in a directory is a symbolic link or not, type:

ls -l

If an "l" appears in the 1st column, then the file is a symbolic link, the size is the size of the link and the last column shows where the link points.

-rw-------   1 xyz99      ugrad            0 Oct 12 02:39 spon
lrwx------   1 xyz99      ugrad            4 Oct 12 02:39 spon_too -> spon
If you do:

ls -lL

you will be given information about the file that the symbolic link points to rather than the link itself.

A number of programs distinguish between hard and symbolic links. For example, tar has an option (-h) which saves files that links point to rather than the links themselves.

Symbolic links, unlike hard, can be made between file systems and can refer to directories; this is their main use.

cd .. can have unexpected (but predictable) results with symbolic links.

To find the symbolic links recursively under a directory, use:

find directory-name -type l -print

To find all the mount points on the root directory, use:

find / -type M -print

For further information see:-

ln(1) - use man ln: note that the syntax of this is

ln [options] file_linked_to name_of_link

The arguments are this way round so that the final argument can default to the last component (the basename) of the file_linked_to.

© Cambridge University Engineering Dept
Information provided by Stephen Mounsey (sjm17)
Last updated: 26th April 2010