Difference between a Hard Link and Soft (Symbolic) Link

Within the Unix/Linux file system, linking lets you create file shortcuts to link one or more files. Linux provides two different linking methods (hard and soft links) which will further be explained in this tutorial.

Introduction to Inodes:
 

To better understand how linking works, it may be easier to first understand the concept of inodes. Each file contains an index number called an inode. An inode (typically represented by a numerical value) is a data structure comprised of metadata information about the associated file. Linking utilizes inodes by pointing a file towards a specific inode value.

To view a file’s inode number, issue the command  ls –i  within the command prompt:

[Dillon@localhost Tutorial]$ ls -i
929447 example_file_1.txt
929448 example_file_2.txt
929449 example_file_3.txt
 
 
Creating a Hard Link:
 
A hard link essentially creates a mirror copy of the original linked file. Each hard linked file is assigned the same inode value as the original, therefore they both reference the same physical file location. Any changes to the data in either file is reflected in the other.

Hard links are beneficial as they are 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. Lastly, hard links can only be assigned to files and not directories as to avoid recursive loops.

In the following example, the original file and hard linked file point to the same inode value of “101”.
To configure a hard link, issue the following command  ln [original filename] [link name]  
 
[Dillon@localhost Tutorial]$ ln original_file new_hardlink_file

[Dillon@localhost Tutorial]$ ls –l

-rw-rw-r--. 2 Dillon Dillon 28 Dec 28 14:28 new_hardlink_file
-rw-rw-r--. 2 Dillon Dillon 28 Dec 28 14:28 original_file
 
Creating a Soft Link (also known as Symbolic Link):
 

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 are beneficial as they 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).

In the following example, the softlink points to the location of the original file. Notice that both files contain different inode values.

To configure a soft link, issue the following command  ln -s [original filename] [link name]  
 
[Dillon@localhost Tutorial]$ ln -s original_file new_softlink_file

[Dillon@localhost Tutorial]$ ls -l

lrwxrwxrwx. 1 Dillon Dillon 28 Dec 28 14:32 new_softlink_file -> original_file
-rw-rw-r--. 1 Dillon Dillon 28 Dec 28 14:32 original_file
 
How to fix a hanging link:
 

To fix a hanging link, you must first determine the original file that the soft link pointed to. Issue the command  ls -l  command to verify this file – the broken link will typically be indicated by red colored text. In this example, the file “original_file” has been moved which resulted in the associated soft link to become broken.

 
[Dillon@localhost Tutorial]$ ls -l

lrwxrwxrwx. 1 Dillon Dillon 13 Dec 28 14:32 new_softlink_file - original_file
 
Next, you must determine where the new file is located. Issue the command  find -name "filename" to locate the desired file:
 
[Dillon@localhost Tutorial]$ find -name "original_file"
Folder 1/original_file
 
Lastly, remove the current soft link by issuing the  unlink [link name]  command and re-configure the new soft link location with the following commands – notice when you issue the unlink command, the soft link file disappears:
 
[Dillon@localhost Tutorial]$ unlink new_softlink_file

[Dillon@localhost Tutorial]$ ls -l
            
[Dillon@localhost Tutorial]$ ln -s Folder_1/original_file new_softlink_file

[Dillon@localhost Tutorial]$ ls -l
lrwxrwxrwx. 1 Dillon Dillon 25 Dec 28 15:07 new_softlink_file -> Folder_1/original_file
 
Summary:
 
Hard Links:
  • Issue the  ln [original filename] [link name]  command to create a hard link
  • Original File and Hard Linked file contain the same inode value
  • Creates a mirror copy of the file
  • Any changes to either original file or hard linked file are reflected in the other file
  • Benefit – more flexible and remain linked if either the original or hard linked file is moved
  • Negative – unable to cross different file systems

 

Soft Links:

  • Issue the  ln -s [original filename] [link name]  command to create a soft link
  • Similar to shortcut feature in Windows Operating system
  • Original File and Hard Linked file contain different inode values
  • Each soft link file points to the original file’s inode
  • Any changes to either original file or soft linked file are reflected in the other file
  • Benefit – soft linked file can cross different file systems
  • Negative – if original file is deleted or moved, the soft link is broken (hanging link)
posted @ 2016-01-19 17:56  小 楼 一 夜 听 春 雨  阅读(490)  评论(0编辑  收藏  举报