Hard Link vs Symbolic Link

What’s the difference?

Underneath the file system, files are represented by inodes. When you create a file in the file system, a link to an inode is also created.

Hard Link

A hard link is merely an additional name for an existing file. Any number of hard links can be created for any file. Fore example, my name is Rashaad. Other people refer to me as Shaad, Jr, and so on. Even tho each name is different, they are still representing the same person. In order to delete the original file, each link must be deleted as well.

Why Use a Hard Link?

Hard links are an productive way to organize files. Say you have 3 folders for images with specific characteristics, and a single picture that contains those characterisitcs. You could copy the file into each folder, but now you have one picture taking up 3 spaces in memory. A hard link however, takes up no space at all. You could the same picture in various different categories without taking up unnecessary disk space.

If you had a photo called StinsonBeach in the summer folder and you wanted to create a link to the 2018 folder, you can use the following command.

ln /summer/StinsonBeacn.jpg /2018/StinsonBeach.jpg

Use this command to see how many files are linked to the same inode.

ls -lt

Symbolic Link

Think of a symbolic link as a shortcut on your desktop. The symbolic link simply points to the location of a file. When you delete a symbolic link, it has no effect on the physical file in which the link is pointing to. A symobilic link can point to any file wtihin the current file system or other file systems. This makes it more versatile than a hard link.

How to create a symbolic link

ln -s /path/to/file /path/to/link