Soft and Hard links on UN*X System.

Hello curious people. In this intro. to hard links and symbolic links, we will first learn and define what each one means. Then we will discuss how do they differ.

Soft and Hard links on UN*X System.
Soft and Hard links on UN*X System

Hard links and symbolic links are two different methods to refer to a file on Linux or other Unix-like operating systems. These methods are part of the filesystem that organizes what file is what and where. A hard link is merely an additional name for an existing, it is essentially a synced carbon copy of a file that refers directly to the inode of a file. Symbolic links on the other hand refer directly to the file which refers to the inode, it is a special kind of file that points to another file, much like a shortcut. In order to understand how symbolic and hard links work, we will need to go over what are inodes.

What is an inode?

The inode is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. They are essentially the numerical equivalent of a full address. With an inode, the OS can retrieve information about the file such as permission privileges and the physical location of the data on the hard drive to access the file. Should a file be moved from one folder to another, the file will be moved to a different location on the hard drive and its inode value will change with it automatically.

Oooookay!! but what does this definition really mean?
Well, we will create a hard link to an existing file by using the command ln file_name hardlink.
ls -i , so that it lists all files with inode.

touch ln.txt
ln ln.txt 1ln.txt
ls -li
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey   38 Feb 10 12:19 ln.txt
1053413 -rw-rw-r-- 2 snubmonkey snubmonkey   38 Feb 10 12:19 1ln.txt

As you may have noticed the file ln.txt and 1ln.txt carry the exact same inode #1053413.

A hard link is a direct reference to a file via its inode. You can only hardlink files and not directories. By using a hardlink, you can change the original file’s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file. There is no referencing to the original file. In addition, hardlinks can only refer to files within the same volume otherwise symbolic links will be needed. To make a hard link of a file, you will require the ln command and refer to the source file before naming what the hard link will be named; as we saw earlier.

ln ln.txt 1ln.txt

Soft links is a special kind of file that points to another file, much like a shortcut. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, the symbolic link is a link that directs to the original file, changing the symbolic link should change the original file, whereas hard links preserve the contents of the file. Since the symbolic link is referring to the original file and not its inode value, then replacing the original file into a different folder will break the symbolic link.

Let's create a directory monkeyGroup and make a soft link to the home directory of user batman.

$
mkdir monkeyGroup
ln -s /home/snubmonkey/monkeyGroup /home/batman

ls -i , so that it lists all files with inode

524302 drwxrwx--- 2 snubmonkey snubmonkey 4096 Feb 10 09:36 monkeyGroup

now, let's su to batman

$
su batman
Password:
cd
ln -li
1053420 lrwxrwxrwx 1 root root 16 Feb 10 09:52 monkeyGroup -> /home/batman/monkeyGroup

As, you can see the inode number (524302 vs 1053420) and file permissions (lrwxrwxrwx vs -drwxrwx---) are different, even though the softlink file has same contents as source.