Linux 101 : Symbolic links and Hard links - Why we can't create hard link on different partitions -



Inodes:

An inode is a Linux object that represents a file or a directory on the filesystem.
We can check the inode number associated with our files using the below command:


The above command shows the inode number for every file or directory.
The inode is a structure that holds data about the files in a filesystem.
Inodes contain for example, information about the size of the file, its owner, permissions, ...

Inodes are tied to a storage device or partition. If we copy a file to a different device or partition, a new and different inode will be assigned to it.

Creating a symbolic link:

A symbolic link refers or points to an object on the Linux filesystem. Any changes on the link will be reflected on the "original" object.
If we move the "original" object or delete it, the symbolic link will not work anymore.

If we delete all the symbolic links to a file or directory, the "original" file or directory remains, because it has a different inode.

We can create a symbolic link using the below command:


Below is the results we see using the "ls" command:


The below command also shows the inode number of the files:


The inode of the symbolic link and the inode of the file it links to are different, they are different objects.

We could see the "l" in the permissions of the file "lrwxrwxrwx", which means that it is a symbolic link.

Hard links:

A hard link and the file it refers to, share the same inode and the same physical location. So we could have two filenames or more pointing to the same exact location.

Remark:

When we delete a file,  as long as there are hard links pointing the the "original" file, the file remains.

We could create a hard link using the below command:


We could display the hard links using the below command:


We create a hard link named "test1", that points to the file "test" using the below command:


We see that the file "test" and its hard link "test1" have the same inode number.


The "test1" hard link shows as a regular file "-rwxrwxrwx".

Hard links have the same inode 
number as the file they represent - they are the same object -  as opposed to symbolic links.

We can't move or create a hard link on another device or partition, because the inode number wouldn't mean the same thing on different partitions or filesystems.

Comments

Leave as a comment:

Archive