Hard links & symbolic links

Jennifer Huang
3 min readFeb 13, 2017

--

The difference between hard links and symbolic links(soft links) comes down to what they’re referencing.

When a file is created in a directory, the file name and an inode number are assigned to that file. An inode is a data structure containing a file’s metadata(information about the file — > size of file, user id, device id, mode, timestamps, etc). Operating systems recognize and differentiate files by inode numbers. It’s similar to serial numbers and social security numbers to identify individuals. When you look up a file by its name, the file name is being matched to its inode number through an internal table called inode table. An inode also stores pointers to disk blocks of the file’s actual data.

Inode numbers can be viewed with the command ls -i

Hard links are esssentially different names to the same file. Hard links share the same inode number with their original file. They point to the same inode of a hard drive.

We’re going to create 2 hard links, hardlink1 and hardlink2, to file2. All three files share the same inode number because they are the exact same link. If we delete the original file, file2, the links of the inode 55779500 will decrement to 2, with the two hard links containing file2’s data.

Hard links are great for backup purposes, but they lack the ability to link directories or across file systems. And so soft links are created.

Unlink hard links, Soft links(or symbolic links), points to the name of another file and does not contain any data of their own.

Soft links can be created with the command ls -s <name of original file> <name of soft link>

Here, we created a soft link called softlink that points to the file file1. You’ll notice an arrow pointing softlink to file1. They also have different inode numbers because softlink doesn’t contain file1’s data, it just points to file1. If the original file, file1, is deleted, its soft link will become obsolete because it will be pointing at a nonexisting file.

--

--