DEV Community

Khaled Abdel-Fattah
Khaled Abdel-Fattah

Posted on

For Beginners: Make Links Between Files in Linux🐧

Welcome to my article! 🫂 Today, I'm going to talk about how to create and use hard links and symbolic links in Linux. If you're new to Linux, you may have heard of these terms before but aren't quite sure what they mean. Don't worry, we'll break it down for you. In this article, we'll cover the basics of hard links and symbolic links, and their differences, By the end of this article, you'll have a better understanding of how links work in Linux and how to use them to your advantage. So, let's get started!

In Linux, various entities are identified by unique identifiers (IDs). Users are identified by their user IDs (UIDs), while groups are identified by their group IDs (GIDs). Processes are identified by their process IDs (PIDs).

When it comes to files, they are identified by index nodes (inodes).
An inode is a data structure that contains information about a file or directory. It stores metadata such as ownership, permissions, date and time stamps, and the path to data blocks containing file data.

The inode acts as a pointer to the physical location of the file's data on the disk. When you access a file, the operating system uses the inode to locate the file's data and read it into memory. So you can create multiple file names that point to the same file. These file names are called links.

In Linux, there are two types of links: hard links and symbolic links (also called soft links). Both types have their own advantage and disadvantages.

Creating hard link

every file is created with a single hard link, which links its name to the file data on the filesystem. When you create a hard link to a file, you create another name that points to the same data. The new hard link functions identically to the original file name and once created, there is no visible difference between the new hard link and the original file name

You can use the ls -l command to determine the number of hard links a file has. The link count, which indicates the number of hard links to the file, is one of the items reported by this command. In this example, the file mynewfile.txt has a link count of 1, indicating that it has exactly one

[user@host ~]$ ls -l mynewfile.txt
-rw-r--r--. 1 user user 0 Jan 11 14:14 mynewfile.txt
Enter fullscreen mode Exit fullscreen mode

To create a hard link, you can use the ln command followed by the path to the existing file and the desired path for the hard link. For example, to create a hard link called mynewfile-hardlink.txt for the existing mynewfile.txt file in the /tmp directory, you can use:

[user@host ~]$ ln mynewfile.txt /tmp/mynewfile-hardlink.txt
Enter fullscreen mode Exit fullscreen mode
  • You can confirm that the two files are hard links by using the ls command with the -i option to display each file's inode number. If the files are on the same filesystem and their inode numbers are the same, then the files are hard links that point to the same data. For example:
[user@host ~]$ ls -il mynewfile.txt /tmp/mynewfile-hardlink.txt
9624321 -rw-rw-r--. 2 user user 0 Jan 11 14:14 mynewfile.txt
9624321 -rw-rw-r--. 2 user user 0 Jan 11 14:14 /tmp/mynewfile-hardliknk.txt
Enter fullscreen mode Exit fullscreen mode

Note: In the case that an original file is deleted, as long as there exists at least one hard link pointing to the file, the contents of the file can still be accessed. Only when the last hard link is deleted, the file data is deleted from storage, and there will be no more hard link referencing that file data.

Limitation: Hard links are subject to certain limitations. Firstly, hard links can only be created for regular files, not for directories or special files.

Secondly, Hard links can only be created between files that exist on the same file system. If the file system hierarchy of your system is spread across multiple storage devices, it's possible that a directory and its contents might be stored on a different file system when you change into it. This means that you won't be able to create hard links between files in different file systems. You can use the df command to check if files are on different file systems.

A hard link points a name to data on a storage device.


Creating Symbolic Links (soft link)

The ln command has a -s option that creates a symbolic link, also known as a "soft link". Symbolic links have some advantages over hard links, including the ability to link files on different file systems and the ability to point to directories or special files, not just regular files.

user@host:~$ ln -s my_file mynewsymlink
user@host:~$ ls -li my_file mynewsymlink
83925 -rw-r--r-- 1 user user 24 May 13 00:15 my_file
84154 lrwxrwxrwx 1 user user  7 May 14 09:39 mynewsymlink -> my_file
Enter fullscreen mode Exit fullscreen mode

The character 'l' at the beginning of the long listing for a file indicates that it is a symbolic link, not a regular file. A symbolic link points to an existing file or directory.

Warning: If the original file is deleted, the symbolic link still points to the file, but the target no longer exists. Such a link is called a "dangling symbolic link."After the original file is removed, attempting to access the symbolic link will result in an error message such as "No such file or directory."

A symbolic link creates a reference from one name to another name, and the second name, in turn, points to the actual data on a storage device.

When to use Hard / Soft Links ?

Both hard links and symbolic links have their uses depending on the situation.
• Use hard links when you want to create multiple directory entries for the same file. This is useful when you want to access the same file using different names or from different directories, without creating additional copies of the file on the storage device. Hard links are also useful when you want to ensure that the file is not deleted until all the directory entries pointing to the file are removed.
• Use symbolic links when you want to create a new name or path for a file or directory. Symbolic links can be used to link files on different file systems, and they can also point to directories or special files. Symbolic links are especially useful when you want to create shortcuts or aliases for frequently accessed files or directories.


👉 In conclusion, both hard links and symbolic links have their advantages and disadvantages, and the choice of which one to use depends on your specific use case. Understanding the differences between the two types of links and how they work can help you make an informed decision and use them effectively in your system.

I hope you found this information helpful. Feel free to share any feedback you may have. Thank you for reading! 👋

Top comments (0)