What is the Difference Between Hard Link and Soft Link in Linux?

Learn via video courses
Topics Covered

In Linux, a link is a reference that allows multiple directory entries to point to the same file or directory. It provides a convenient way to access and organize files within the file system. Linux provides various methods for creating links to files, including hard links and soft links. This article explores the features of hard links and soft links in linux with their differences.

  • Symbolic links are another name for soft links which are special types of files that point to another file or directory in the system.
  • Symbolic links are similar to shortcuts in graphical user interfaces and can be created across different file systems.
  • Symbolic links do not store any of the file's data but instead as a symbolic path to the target file or directory.
  • A soft link, also known as a symbolic link or symlink, is a special type of file that points to another file or directory. It acts just like a shortcut in Windows.
  • Soft links only store the path and name of the target file and have no information regarding the data in the target file.
  • When any action is performed on the file through the soft link, the action is performed on the target file.
  • Soft links provide flexibility and convenience in managing files and directories. These are commonly used in scenarios where files need to be accessed from multiple locations without duplicating the file data.
  • When the target file of a soft link is deleted or moved, the soft link becomes invalid or broken and the user will not be able to access the original file through the soft link.
  • A hard link in Linux is a special type of link that creates access to the stored file data.
  • Hard links provide multiple names or paths to access the same physical file data or inode.
  • An inode is a data structure in the Linux file system that stores information about a file, including its metadata and the location of the actual data on the disk.
  • The hard link will never point to the original file, instead, it will refer to the same underlying file data or inode.
  • The hard link is denoted by the link count, stored in the inode. When a hard link is created, the link count increases. A normal file can also be called a hard link.
  • A hard link can never be mentioned as a copy, because, each hard link created to a file is essentially equal and refers to the same data.
  • When a modification is made to the file through one hard link, then the changes will be immediately visible when accessing the file through any other hard links. This is because all hard links point to the same inode.
  • The main advantage of the hard link is that, even though the original file is deleted, unless one hard link to the file exists, the data will never be deleted.
  • Hard links cannot be created for directories.

The differences between hard link and soft link in linux are,

CategoryHard LinksSoft Links
Data AssociationDirectly shares file data with target filePoints to target file by name and path
File System BoundariesLimited to the same file systemAccess across different file systems and machines
File RemovalDeleting target file does not affect the linkDeleting target file makes the link invalid or broken
Link CountIncreases the link count of the target file's inodeDoes not affect the link count
PermissionsCreated with the same permission as the original fileCreated with all permission by default

In exploring hard link and soft link in linux, it is important to understand the working of hard links. A hard link is a new name or directory entry for an existing file that directly shares the same data as the target file.

  • When a file is created in Linux, it is assigned an inode. At this point, the link count will have a default value of 1 which is the directory entry that points to the file data.
  • When a hard link is created, it adds another directory entry pointing to the same inode and increases the link count in the inode.
  • Editing a file through any of its hard links will affect all the other hard links as well, as they all point to the same data.
  • As a hard link is deleted, the link count in the inode decrease.
  • When the link count becomes 0 or all hard links including the file are deleted, the file system considers the file as no longer in use and frees up the disk space previously occupied by the file.
  • These hard links are efficient in terms of disk space utilization, as they do not create additional copies of the file.

In exploring hard links and soft links in linux, it is important to understand the working of soft links. A soft link is essentially a special type of file that acts as a pointer to another file or directory.

  • When a soft link is created, it creates a new file with its inode.
  • This file contains the name and path information to the target file or directory.
  • When you open the soft link, the operating system reads the soft link file, retrieves the path information, and then follows the path to access the target file or directory.
  • If the target file or directory of a soft link is moved or deleted, the soft link will become invalid or broken.

The ln command in Linux is used to create hard links and soft links in linux. To create a hard link using the ln command, you have to specify the source file and the name of the hard link. The syntax for creating a hard link is as follows,

  • <source_file> is the path to the existing file that you want to create a hard link to.
  • <hard_link_name> is the name with the path of the hard link you want to create. If you provide just a name, the hard link will be created in your current directory.

Let us consider an example, where we have a file named original.txt located in the /home/hari/Documents directory, and we want to create a hard link called hardlink.txt in the /home/hari/links directory that points to the original.txt file. We can use the following command to accomplish this,

After running this command, a hard link with the name hardlink.txt will be created in the /home/hari/links/ directory. The hard link and the original file will share the same inode, meaning they point to the same data.

We can use the ls -li command, which lists files along with their inodes number. The l flag is called long format and gives additional details regarding the file or directory. The i flag is used to get the inode number. An inode number is a unique identifier assigned to every inode. This is being used to find the hard link and soft link in Linux.

create-hard-links-in-linux

The output produces the following details,

  • The number 930266 represents the inode number associated with the file.
  • -rw-r--r-- represent the read and write file permissions.
  • The number 2 shows the number of hard links to the file.
  • The next two hari shows the owner of the file and the group to which the file belongs.
  • The value 7 shows the file size in bytes.
  • The next details are the last modification date and the path of the field

From the output, we can see that the inode number is the same for both files, representing that they are hard links sharing the same data.

Some other ways to find the hard links for a file are,

  • Using the ls command with the -i option. The -i flag is used to display the inode number of each file.

  • The find command can be used with the -samefile option followed by the target file to search for the hard links of a file. The following command can be used to find all the hard links associated with the original.txt file.

  • We can also use the stat command along with the find command to find the hard links of a file. It has the following steps,

    • The stat command provides detailed information about files, including the inode number.
    • Get the inode number from the output of the stat command.
    • Use the find command with the -inum option and the inode number to find other files with the same inode.

If the ls -li command doesn't show you the inode number, use sudo ls -li instead as some systems request superuser privileges to view system information.

To create a soft link in Linux, you can use the ln command with the -s option, followed by the path to the target file or directory and the desired name for the soft link. The syntax for creating a soft link is as follows,

  • <target_file_or_directory> is the path to the existing file that you want to create a soft link to.
  • <soft_link_name> is the name with the path of the soft link you want to create. If you provide just a name, the soft link will be created in your current directory.

Let us consider an example similar to the previous section, where we have a file named original.txt located in the /home/hari/Documents directory, and we want to create a soft link called shortcut in the /home/hari/links directory that points to the original.txt file. We can use the following command to accomplish this,

After executing the above command, the soft link shortcut will be created in the /home/hari/links directory and will act as a shortcut to the original file original.txt.

We can use the ls -li command, which displays detailed information about the files to check the creation of a soft link.

make-soft-links-in-linux

In the output, we can see the following details,

  • The string lrwxrwxrwx represents the permissions for the soft link, where the l, indicates that the file is a symbolic link.
  • The permission indicates that the owner, group, and other users have read, write, and execute permissions on the soft link. This is the default behavior for soft links in Linux, where all permissions are granted to all users by default.
  • The soft link is named asshortcut and is indicated with an arrow (->) to its target file or directory.
  • Other details are similar to the details discussed in the previous section. Also, note that the inode number is different from the inode number of the original file.

The permissions on the soft link do not directly affect the permissions of the target file. This means, even though the link has read permission, the user can only access the file if the original file has read permissions.

Some other ways to find the soft links associated with a file are,

  • The find command can be used to search the entire file system(/) for symbolic links. We have to use the -type l option to specify we are searching for soft links and the -lname option to specify the target for which soft links are made.

  • The readlink command is used to display the target of a symbolic link. You can use it with the -f option to find all the soft links pointing to a file.

  • We can also use the stat command which gives various information about a file along with the grep command to get the soft links of a file. The command has the following steps,

    • The stat command uses the -c option to specify a custom format for the output. We specify the custom output format as %N %F, where %N represents the file name and %F represents the file type.
    • The grep command is used to get all the symbolic link text from the output.
    • The pipe(|) is used to get the output of the stat command and give it as input for the grep command.

You can also use the file command to check if a file is a symbolic link or a normal file.

Conclusion

  • Hard links create additional directory entries that directly reference the same file data or inode.
  • Soft links act as a shortcut to target files or directories and have individual inodes.
  • Hard links are based on the link count on the inode and also have the same inode number as the original file.
  • Soft links have a different inode number and can span across file systems.
  • Both hard link and soft link in linux can be created using the ln command. For soft links, an additional -s option has to be used.
  • Understanding the differences between hard and soft links allows users to use them for different file management scenarios.