Home Linux Commands How to Work with Hardlinks and Softlinks in Linux

How to Work with Hardlinks and Softlinks in Linux

Hardlinks and Softlinks are important concepts to understand when you are working in a Unix-like environment. In this article, we will discuss what is the hard link and soft link and how to create them in Linux.

Linux treats everything as a file. Whether it is a block device, character device, socket, or named pipe Linux will treat them as a file. Hardlinks and soft links are also a type of file that is actually created in reference to another file.

What is Hardlink in Linux

Hardlink is like a clone of the original file. All the hard links share the same inode number and removing the original or any other hard-linked file will not have any effect on other files and still, you can read the file content.

NOTE: Hardlinks cannot be created for directories.

How to Create Hard Links in Linux

Let’s see how to work with hard links. I am creating a file named file1.txt and writing some contents to it.

$ cat file1.txt
Create New File
Create New File

Now run the ls command with the -i flag to check the inode number of file1.txt and link count.

$ ls -li file1.txt
List File Inode Number
List File Inode Number

Now create a new hard link and run the same ls command to check the inode number. You will see both the files have the same inode numbers.

$ ln <source-file> <destination-file>
$ ln file1.txt file2.txt
Create Hardlink in Linux
Create Hardlink in Linux

I will now create a new hard link file from file2.txt which is already created from file1.txt. Check the link count and it is updated to 3 across all the files.

$ ls -li file2.txt /home/karthick/file3.txt
Create New Hardlink in Linux
Create New Hardlink in Linux

How to Remove Hard Links in Linux

Now if you delete any of the hard-linked files the link count will be updated accordingly in all the files and you can still access the file contents.

$ rm file1.txt

To remove all the hard-linked files completely across the file system you can use the find command to search the files with the same inode number and remove the files.

$ find <directory> -inum <inode-number>  # SYNTAX
$ find / -inum 415314          # Finding all files with Inode 51425368
Find Files with Inode Number
Find Files with Inode Number

To remove the files you can add an exec command to the find command.

$ sudo find / -inum415314 -exec rm -f {} \;
Remove Hardlinks in Linux
Remove Hardlinks in Linux

NOTE: When you run the find command and if you wish to scan all the directories in the file system it is better to use sudo.

What is Softlinks in Linux

Softlinks sometimes called symlinks or symbolic links. When you create a soft link, a new file will be created and that file will be pointing to the parent file. Think of this as a shortcut that you create for files and folders. A new file will have a different Inode compared to the parent file.

$ cat > slink.txt
$ ls -li slink.txt
Create File in Linux
Create File in Linux

How to Create Soft Links in Linux

Now create a new soft link and run the same ls command to check the inode number. You will see both the files have the same inode numbers. Also, take a look at the link count for each file.

$ ln -s <source> <destination>      # SYNTAX
$ ln -s file1.txt file2.txt
Create Softlink in Linux
Create Softlink in Linux

Take a look at the link count from the above image, it will always be 1 for soft links since all the files get a separate Inode number. Now if you delete the soft link it will not have any effect on the original file.

#  grep ^ ./slink*   # Display the contents of file1, file2
#  rm slinked_linked.txt
Remove Softlink in Linux
Remove Softlink in Linux

If you delete the original file linked file will throw an error when you try to access it.

$ ls -li
$ rm slink.txt
$ ls -li
$ cat slink_linked.txt
Remove Parent File
Remove Parent File

Sometimes you might remove the parent file but forget to remove all the linked files. You can check and clean all orphaned soft links by running the following command. Find command will try to find all the orphaned links from the /home/ directory and -delete will remove the links.

# find -L /home/ -type l -delete

That’s it for this article. We would like to hear your valuable feedback or any tips that can improve the articles.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.