Working with files and directories

Finding out whether a file has hard links

To find out how many hard links there are to a file, use the ls -l or l (long listing) command, as follows:

   $ l
   -rw-r--r--   1 johnd   unixdoc    10586 Feb 25 12:26 1.start
   -rw-rw-r--   2 johnd   techpubs   61339 Feb 24 14:45 2.scosh
   -rw-rw-r--   1 johnd   techpubs   14741 Feb 25 11:18 3.dire
   -rw-rw-r--   3 johnd   techpubs   40419 Feb 25 15:57 4.files
You need to locate all the links to a file or directory if you want to delete it: as long as there are links, you cannot delete the file or directory.

In a long listing, the number of links are shown in the second column from the left (after the sets of permissions). For example, in the above example, the file 4.files has three links to it.

You can find out where the common links to a single file are located, in two steps. First, you need to identify the inode number of the file (see ``How the system manages files and directories'' for information on inodes). To do this, use the ls -i option, as follows:

   $ ls -i
   20350 basking_shark
    3886 cod
    2002 halibut
    3526 herring
   10182 narwhal
The number before the filename is the file's inode number, for example, 2002 for the file called halibut.

To trace all the links to this file, you must find all the other files in the filesystem with this inode number. You can do this using the find -inum option, as follows:

   $ find / -inum 2002 -print 2>/dev/null
In this case, there are three files with the same inode number. In order to delete this file from the filesystem, it would be necessary to run rm on all the links.

Next topic: Creating a link to a directory
Previous topic: Creating a link to a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003