File Links

By using links, a file can be referenced by more than one name.


How links work: Hard links

Hard links are on the level of the filesystem’s inodes. You can see the first inode by typing “ls -i”

stw@laptop:~> touch inode-example
stw@laptop:~> ls -i inode-example
1324694 inode-example

You can think of the inode being the ‘location’ of the file on the harddisk. You can think of the filename to be a pointer to the inode which holds the file.
You can create another filename which points to that exact inode using the ‘ln’ command.

stw@laptop:~> ln inode-example new-name
stw@laptop:~> ls -i inode-example new-name
1324694 inode-example  1324694 new-name

While editing the file under one name will obviously change the contents even if you access it by the other, you can remove those links to the inode (that is: the filenames) individually.
“ls -l” shows how many filenames link to the inode in the second column.

stw@laptop:~> ls -l inode-example new-name
-rw-r--r--  2 stw users 0 2004-07-12 18:13 inode-example
-rw-r--r--  2 stw users 0 2004-07-12 18:13 new-name
            ^
here---------

Let’s remove one of them:

stw@laptop:~> rm inode-example
stw@laptop:~> ls -i inode-example new-name
/bin/ls: inode-example: Datei oder Verzeichnis nicht gefunden
1324694 new-name
stw@laptop:~> ls -l inode-example new-name
/bin/ls: inode-example: Datei oder Verzeichnis nicht gefunden
-rw-r--r--  1 stw users 0 2004-07-12 18:13 new-name
            ^
only one left

If there are no more names linking to an inode, the actual diskspace is marked unused, the file finally is deleted.

Please note: The file is deleted, not the actual data. The data is still there on those magnetic disks, but might be overwritten any time the system needs diskspace.

Since the inodes are specific to the actual harddisk, you cannot create hardlinks that span across disks. Also, hardlinks to directories are not possible.

Helge Hafting on the linux-kernel mailing-list has explained why hardlinks to directories are a bad thing and should be impossible, even though they might be technically doable. It boils down to avoiding “directory-loops” where /a/b/c is a hardlink to /a. The problem here is that if you delete /a you have no chance of accessing or deleting b. You can read the whole story here:
www.uwsg.iu.edu/hypermail/linux/kernel/0308.0/1027.html


How links work: Soft links

Anyway, here comes the second type of link: Soft links. These soft links are not pointers to inodes, but to filenames. Since directories are only special types of files, softlinks can also point to directories.

stw@laptop:~> ls -l new-name softlink-example
-rw-r--r--  1 stw users 0 2004-07-12 18:13 new-name
lrwxrwxrwx  1 stw users 8 2004-07-12 18:34 softlink-example -> new-name

Please note the “1” in the “reference counter”.

stw@laptop:~> ls -i new-name softlink-example
1324694 new-name  1324716 softlink-example

The file and the softlink have two different inodes. So the softlink is another kind of special file which stores the path to the actual file that should be accessed.

And here come the features of this technique:

  1. Softlinks can point to other disks
  2. Softlinks can point to directories
  3. The link target does not need to exist.
    It can be on a filesystem that is not mounted, yet.

Created by stwaidele


No comments yet.

Leave a Reply