next up previous contents index
Next: 3.10.2 Symbolic links Up: 3.10 Managing file links Previous: 3.10 Managing file links

3.10.1 Hard links

  The ln command is used to create multiple links for one file. For example, let's say that you have the file foo in a directory. Using ls -i, we can look at the inode number for this file.

# ls -i foo
22192 foo
#

Here, the file foo has an inode number of 22192 in the filesystem. We can create another link to foo, named bar:

# ln foo bar

With ls -i, we see that the two files have the same inode.

# ls -i foo bar
22192 bar 22192 foo
#

Now, accessing either foo or bar will access the same file. If you make changes to foo, those changes will be made to bar as well. For all purposes, foo and bar are the same file.

These links are known as hard links because they directly create a link to an inode. Note that you can only hard-link files on the same filesystem; symbolic links (see below) don't have this restriction.

When you delete a file with rm, you are actually only deleting one link to a file. If you use the command

# rm foo

then only the link named foo is deleted; bar will still exist. A file is only actually deleted on the system when it has no links to it. Usually, files have only one link, so using the rm command deletes the file. However, if a file has multiple links to it, using rm will only delete a single link; in order to delete the file, you must delete all links to the file.

  The command ls -l will display the number of links to a file (among other information).

# ls -l foo bar
-rw-r--r-- 2 root root 12 Aug 5 16:51 bar
-rw-r--r-- 2 root root 12 Aug 5 16:50 foo
#

The second column in the listing, ``2'', specifies the number of links to the file.

As it turns out, a directory is actually just a file containing information about link-to-inode translations. Also, every directory has at least two hard links in it: ``.'' (a link pointing to itself), and ``..'' (a link pointing to the parent directory). The root directory (/) ``..'' link just points back to /.



next up previous contents index
Next: 3.10.2 Symbolic links Up: 3.10 Managing file links Previous: 3.10 Managing file links



Matt Welsh
mdw@sunsite.unc.edu