/**/ How to create links using ln command in Linux? - Dextutor

How to create links using ln command in Linux?

Links in Linux are like Short-cuts in Windows. We create a shortcut for a file/folder in Windows so that the original file can be accessed from multiple locations. The same concept goes for links. Links help to access the file from multiple directories. Before we learn how to create links using ln command in Linux, we will learn a bit more about links.

Note: the content of the file and the links will remain the same. You update one and the other will reflect the same change

Types of links in Linux

  1. Hard Links
  2. Soft links

Hard Links – is a copy of the original file. The command to delete a hard link is ln. For example.

$ln cal.txt D2/cal.txt_link

will create a hard link for the file cal.txt in the directory D2 with the name cal.txt_link. The difference between creating a link and copying a file using cp command is that with links the content of both the files will always be in sync i.e., you modify either the original file or the link the change will reflect in both. But this is not the case in case of copying files using cp command.

Another point to note is creating hard links increase the number of link count associated with the file. In the figure below the number of links increase to 2 after a link is created.

How to create links using ln command in Linux?

The link count will increase by 1, each time you create a link of a particular file.

The advantage of creating a hard link is even if you delete the original file the link file will still work and vice-versa.

Note: It is not possible to create hard link for a Directory.

Soft Links – Soft links in Linux are exactly similar to short-cut in windows. The command to create a soft link is ln with -s option. For example:

How to create soft links using ln command in Linux?

will create a soft link for file data.txt with the name data.txt_link inside the D2 directory. The count of links do not change on creating a soft link as is shown in the figure above.

Another difference is that in case of soft links if the original file is deleted the soft link will not work. But if the soft link is deleted the original file can still be accessed.

How to create links for Directories?

Only soft links are possible for directories. The command is ln -s. For example:

$ln -s D1 /home/baljit/D2/D1

will create soft link for directory D1 inside the /home/baljit/D2 directory with the same name D1

Difference between Hard link and Soft Link

Hard LinkSoft Link
1. Only files can have hard links1. Both files and directories can have soft links
2. The number of link counts increases2. The number of link count remains the same
3. The link can be accessed even if the original file is deleted3. The link will not work if the original file is deleted
4. The inode number of both the original file and the link are same4. The inode number of both the original file and the link are different
5. Command used is ln5. Command used is ln -s

Practice questions on how to create links in Linux

Q1. Consider a file f1 in your current working directory. Create a hard link and a soft link for f1. What will the count of links in the $ls -l f1 command output.

Q2. How to identify a link from the other files in a directory?

Relevant Commands

ls
cp
touch
mkdir

Leave a Comment