Types of Links

T

What is a link?

Links are pointers pointing to a file or a directory. Like a spirit is linked to the ouija board. Remember, it is not the board moving but the spirit. Just like links.

Why do we need Links?

  • Some programs require a particular version of a library. Eh, like my mother; particular in everything.
  • When a library upgrade replace the old version, the program would crash with an error specifying the name of the old, now-missing library. So, we make soft links of the old names linking to the new files with new names. Usually, the only change in the library name is the version number. 🙄

TL;DR → Why do we need Links?

  • Linking libraries
  • Making sure files are in constant locations (without having to move the original)
  • Keeping a “copy” of a single file in multiple locations

Types of Links

Hard Links: Definitely not as hard as rocks.

Soft Links: Definitely not as soft as pillows.

They both provide multiple directory entries (or references) to a single file. Links are powerful and add flexibility to Linux file systems because everything is a file.

Hard links

  • It is an additional name for an existing file.
  • We can create more than one hard link for a single file.
  • All the hard links connect to the same inode.
  • In case you are wondering what an inode is?
    • Each file has one inode that contains information about that file, including the location of the data belonging to that file. In-short, inode contains the metadata about the file.
  • Hard links share the same data blocks on the hard disk, while they continue to behave as independent files.
  • When the source file is removed or moved, then hard links are not affected.
  • Links have actual file contents. Like, a soul to the body.

Command to create a hard link is:

ln [original filename] [link name] 
ln I-am-a-file hard-link
  • The number of hard links for a file is displayed with the ls -l command.
ls -l 
-rw-r--r-- 2 aaakriti users 0 Sep 3 15:59 hard-link
-rw-r--r-- 2 aaakriti users 0 Sep 3 15:59 I-am-a-file
  • If you want to display the actual inode numbers, the command ls -li does that.
ls -li 
82752916 -rw-r--r-- 2 aaakriti users 0 Sep 3 15:59 hard-link
82752916 -rw-r--r-- 2 aaakriti users 0 Sep 3 15:59 I-am-a-file
  • The hard link has the same permissions as of the original file and will always be synced with the original file.

Soft links

  • A soft link is something like a shortcut in Windows. It is an indirect pointer to a file or directory.
  • Soft Links are the virtual or abstract location of the file.
  • It doesn’t contain any information or content of the linked file. Instead, It has a pointer to the location of the linked file.
  • So, a new file is created with a new inode, having a pointer to the inode location of the original file.
  • If the original file is deleted or moved, the soft linked file will not work. And is called hanging link.
  • A soft link can link to a directory. A hard link cannot.
  • It has a different inode number and file permissions than the original file. And, permissions will not be updated or synced with the original file, unlike hard link.
  • Command to create a Soft link is:
$ ln -s [original filename] [link name]  
ln -s I-am-a-file soft-link
  • ls -l command shows all links with the first column value and the link points to the original file.
ls -l 
lrwxrwxrwx 1 aaakriti users 11 Sep 3 16:16 soft-link -> I-am-a-file
  • If a soft link is broken i.e. the original file doesn’t exist or is deleted, the ls command highlights broken links with flashing white text on a red background in a long listing.

We can clearly see, the contents of the file, the hard link and soft link are the same.

[aaakriti@aakriti ~]$ cat I-am-a-file 
You know this content sucks.

[aaakriti@aakriti ~]$ cat hard-link 
You know this content sucks.

[aaakriti@aakriti ~]$ cat soft-link 
You know this content sucks.

If you delete the original file:

rm -f I-am-a-file
[aaakriti@aakriti ~]$ cat hard-link 
You know this content sucks.

[aaakriti@aakriti ~]$ cat soft-link 
cat: soft-link: No such file or directory
Hard LinksSoft Links
Additional name for an existing file.Indirect pointer to a file or directory.
Connect to the same inode.Connect to different inode.
Share the same data blocks on the hard diskDo not share the same data blocks on the hard disk
Hard links are not affected by deletion of source fileSoft links are affected by deletion of source file
Have actual file contentsDoesn’t have actual file contents.
Cannot link to a directoryCan link to a directory
Same permissions as of the original file and in syncDifferent permissions as of the original file and not in sync
ln [original filename] [link name]ln -s [original filename] [link name]

Show some love?
+1
4
+1
0
+1
0
+1
1
+1
1
+1
0
+1
0

About the author

Aakriti

Trying to be a Linux wizard

Add Comment

By Aakriti

Say Hi!