Main index

Introducing UNIX and Linux


Files

Overview
The UNIX directory hierarchy
Filesystems
Manipulating files
      Creating directories
      Creating files
      links
      'Dot' files
Protecting files
      Groups
      File access control
      Changing privileges
File contents
      Text files
      Comparing files
      Filtering files
      Non-text files
Printing files
File archives and file compression
Other relevant commands
Summary
Exercises

links

To create a second name for a file that already exists, we can create a link - sometimes called a hard link - to it using the command ln ('link'). With two arguments, which must be filenames, provided the first file does exist and the second does not, we can create a link from the first to the second. For instance, if user sam whose home directory is /home/ugrad/sam has a file datafile that I wish to have in my own home directory under the name samsdata, then the command ln can be used to create a link between the two:

ln /home/ugrad/sam/datafile samsdata

We say that samsdata has two links, and it has two names, one samsdata, the other /home/ugrad/sam/datafile. The file has a single inode, however. When samsdata is amended, the contents of /home/ugrad/sam/datafile are changed at exactly the same time (and vice versa). If we delete /home/ugrad/sam/datafile, we actually delete that filename, and the file continues to exist, but with only one name (samsdata) and one link. The kernel will keep track of how many names (links) an inode has, and when this drops to zero the filespace allocated to that inode is released for use elsewhere. There is one important point to note here - inodes are unique only within a single filesystem, and therefore you can only link a file to another file within the same filesystem. We can check precisely which inodes are allocated to which files by using option -i ('inode') to ls:

ls -i
total 561
241563 myfile     43532 dir1    86475 dir2
567721 prog.c    563341 foo    563341 bar

In this example, files foo and bar have the same inode, namely 563341, and have therefore been linked. Note that two linked files do not necessarily have to be in the same directory.

At this point, it is worth discussing briefly what a directory actually is. If you type ls while in a particular directory, any directories contained within it (referred to as subdirectories) will appear as if they were files. In a sense, this is correct - every directory can be considered as a file, each with its own inode. This 'file' contains - in a form that need not concern us - information as to where the files in that subdirectory are stored. By typing cd followed by the name of a directory, the file representing that directory is examined, and the data in the file that indicates where it is stored is retrieved and used to work out where the new current directory is stored.

Worked example

What is the inode of your home directory?
Solution: First of all, type cd to change to your home directory. If you then type ls -i the inodes of the files contained in that directory will be given; the manual page for ls indicates that option -d ('directory') will list directories like other files, rather than listing their contents. So you require

ls -id


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck