Linux+: Linux Shell 21 – File Linking – Hard Links

J

Jarret W. Buse

Guest
Linux+: Linux Shell 21 – File Linking – Hard Links

Files are stored on a partition and within a folder structure. Files can be stored by two methods which are file links. For some people, file links can be a difficult subject. With a little patience they can be understood.

The two linking methods are:
  1. Hard Link
  2. Soft Link (Symlink)
Files are stored on a partition in two places. The first place is the inode table which stores the filename, various times (modify, access, creation, etc.), owner, permissions, inode address, etc. The inode address is the address of the physical file location in the partition.

To begin with, partitions contain a section for the inode table as well as a pool of free inodes. When a file or folder is placed into the partition it uses a free inode address. The file information is placed into the inode table with a free inode address. The file is then placed at the inode address.

A hard link is when two filenames in the inode table have the same inode address. Whichever filename you use to open the file, they are identical. The file contents will remain the same for either filename used to open the file. The same is true for file permissions, etc.

To make a hard link, use the command 'ln'. The syntax is as follows:

ln source_file target_file

Let's look at an example. If I create a file name 'text' with my name in it, in a folder called 'Linking' within my home folder, I can then make a hard link called 'name' with this command:

ln text name

The results can be shown in Figure 1.

Figure 21-1.jpg

FIGURE 1​

Here you can see that both files, text and name, have an inode address of 524455. Both files also have the identical security settings. If the security settings are changed on one file, it changes both. You can see the permissions in Figure 1 shows read and write for the owner and read for group and others. After the change is made to add read and write for all, the permissions were changed for both files.

NOTE: Some applications may edit a hard linked file, make changes and save it as a new file. This now removes the hard link and changes the inode address of the one filename. For instance, if 'name' was edited by such an application, then 'name' would be at a different inode address than 'text'.

If one of the hard linked files were deleted, the other would remain. For example, if 'name' is deleted, then 'text' would still exist.

NOTE: A file can be hard linked more than twice.

In Figure 2, the third column shows the link count for the files. Here, 'name' and 'text' have a link count of 2. If one is deleted, the count may remain 2 until the deleted file is removed from the Trash.

Figure 21-2.jpg

FIGURE 2​

If the count is not an expected number, you can find the link and its location by using the following command:

sudo find / -inum inode_number

Looking at Figure 2, let's say I have the 'name' file and do not know what the other hard linked file is or where it is located. If I needed to find it, I could run the following command:

sudo find / -inum 524455

The result would in this case be ~/Linking/text. Do recall that the file may exist in the Trash.

If you look at Figure 3, you can see the contents of 'name' and 'text'. The contents were changed for 'text' which were also changed for 'name'.

Figure 21-3.jpg

FIGURE 3​

If a hard linked file is copied, such as copying 'text' to 'text1', the file is copied and not linked. The concept is demonstrated in Figure 4 where the listing of the files shows that 'text1' has a different inode address than 'text' or 'name'.

Figure 21-4.jpg

FIGURE 4​

With hard linked files, the hard links must be on the same partition. The inode address cannot be linked across partitions. Each partition has different address tables. If one partition is larger, it will have more inode addresses available than the smaller partition. Hard links must exist on the same partition, but can be in different directories.

Folders cannot be hard linked since it causes a problem with cycling issues. For example, if a folder is created called 'x' and within 'x' is created 'y' and you create a hard link to 'x' within 'y' called 'z' there is a problem. By changing to folder 'y' (/x/y/) you can change into folder 'z' (/x/y/z/) which is technically being in 'x'. Now, you can change to 'y' again (/x/y/z/y/) and the folders can continue to a depth which can cause the file system to reach a folder depth limit. Looking back, folder '/x/y/' has a parent of 'x'. When you are in folder '/x/y/z/' the parent folder is 'x' as well. If you try to perform a hard link on a folder, you should receive an error.

Be aware of the limitations and uses of a hard link. Some people see them as having a backup of a file that if it is deleted, it still remains. The 'backup' does not take up extra space on the partition on entries in the inode table.
 

Attachments





Members online

No members online now.

Top