Open In App

Soft and Hard links in Unix/Linux

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere. 

There are two types of links :

  1. Soft Link or Symbolic links
  2. Hard Links

These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the path name of its target); hard links always refer to the source, even if moved or removed. 

For example, if we have a file a.txt. If we create a hard link to the file and then delete the file, we can still access the file using hard link. But if we create a soft link of the file and then delete the file, we can’t access the file through soft link and soft link becomes dangling. Basically hard link increases reference count of a location while soft links work as a shortcut (like in Windows) 

1. Hard Links 
 

  • Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems.
  • ls -l command shows all the links with the link column shows number of links.
  • Links have actual file contents
  • Removing any link, just reduces the link count, but doesn’t affect other links.
  • Even if we change the filename of the original file then also the hard links properly work.
  • We cannot create a hard link for a directory to avoid recursive loops.
  • If original file is removed then the link will still show the content of the file.
  • The size of any of the hard link file is same as the original file and if we change the content in any of the hard links then size of all hard link files are updated.
  • The disadvantage of hard links is that it cannot be created for files on different file systems and it cannot be created for special files or directories.
  • Command to create a hard link is: 
     
$ ln  [original filename] [link name] 

2. Soft Links 
 

  • A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).
  • ls -l command shows all links with first column value l? and the link points to original file.
  • Soft Link contains the path for original file and not the contents.
  • Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling” link which points to nonexistent file.
  • A soft link can link to a directory.
  • The size of the soft link is equal to the length of the path of the original file we gave. E.g if we link a file like ln -s /tmp/hello.txt /tmp/link.txt then the size of the file will be 14bytes which is equal to the length of the “/tmp/hello.txt”.
  • If we change the name of the original file then all the soft links for that file become dangling i.e. they are worthless now.
  • Link across file systems: If you want to link files across the file systems, you can only use symlinks/soft links.
  • Command to create a Soft link is: 
     
$ ln  -s [original filename] [link name] 

Soft and Hard links in Unix/Linux – FAQs

What are soft links in Linux? 

Soft links, also known as symbolic links or symlinks, act as shortcuts or references to files and directories in Linux. Unlike hard links, soft links are not direct mirrors of the target files. Instead, they point to a specific path in the filesystem where the target files or directories reside. If the target file is moved or deleted, the soft link becomes a ‘dangling’ link, pointing to a non-existent file, which results in errors when accessed.

What are hard links in Linux?

Hard links in Linux are essentially additional names for an existing file on the filesystem. Unlike soft links, hard links are indistinguishable from the file they link to because they are direct references to the inode (the data structure that stores file metadata, including its contents). Creating a hard link to a file does not use additional disk space for the contents of the file. When the original file is deleted, the data remains accessible through any of its hard links until all references (links) to the file are deleted.

How to create a soft link in Linux?

Creating a soft link in Linux uses the ln command with the -s option, which stands for “symbolic”. The syntax is as follows:

ln -s target_path link_name

For example, to create a symbolic link link.txt that points to a target file original.txt in the same directory, you would use:

ln -s original.txt link.txt

This command creates a symbolic link named link.txt that points to original.txt.

How to create a hard link in Linux?

Creating a hard link in Linux does not require any special options with the ln command. The syntax is:

ln target_file link_name

For example, to create a hard link named hard_link.txt pointing to an original file original.txt, you would use:

ln original.txt hard_link.txt

This command creates a new directory entry hard_link.txt that points to the same inode as original.txt.

What are the differences between soft and hard links?

  • Reference Type: Soft links are references to the pathname of a file or directory, while hard links are references to the inode of a file.
  • Behavior on Original File Deletion: When the original file is deleted, a soft link pointing to it becomes invalid (dangling), whereas a hard link remains valid as it points directly to the inode of the file.
  • Cross-Filesystem Linking: Soft links can point to files or directories across different filesystems because they refer to pathnames. Hard links can only be made to files (not directories) within the same filesystem, as they link directly to inodes.
  • Visibility: When listing directory contents with ls -l, soft links show the path to which they point using an arrow notation (->), whereas hard links appear as regular files, showing no indication that they are links.


Next Article

Similar Reads

Article Tags :
three90RightbarBannerImg
  翻译: