Difference between a Hard Link and Soft (Symbolic) Link

时间:2022-09-02 16:23:45

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.

Difference between a Hard Link and Soft (Symbolic) Link

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”.

Difference between a Hard Link and Soft (Symbolic) Link
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.

Difference between a Hard Link and Soft (Symbolic) Link
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)

Difference between a Hard Link and Soft (Symbolic) Link的更多相关文章

  1. 转载:Linux: What’s the difference between a soft link and a hard link?

    Link:https://www.moreofless.co.uk/linux-difference-soft-symbolic-link-and-hard-link/ This example sh ...

  2. linux shell symbolic link & soft link, symbol link, link

    linux shell symbolic link symbolic link https://en.wikipedia.org/wiki/Ln_(Unix) https://stackoverflo ...

  3. Windows中的硬链接和软链接(hard link 和 Symbolic link)

    先来了解一下Linux中的硬链接和软链接: Linux中的硬链接和软链接 Windows中的硬链接和软链接: 硬链接 从Windows NT4开始,NTFS文件系统引入了HardLink这个概念,它让 ...

  4. 硬链接(hard link)和符号连接(symbolic link)

    inode ====== 在Linux系统中,内核为每一个新创建的文件分配一个inode,每个文件都有一个惟一的inode号,我们可以将inode简单理解成一个指针,它永远指向本文件的具体存储位置.文 ...

  5. What is linux symbolic link

    Question :What is linux symbolic link In computing, a symbolic link (also symlink or soft link) is a ...

  6. Symbolic link and hard link的区别(linux)

    --Symbolic link and hard link的区别(linux) --------------------------------------------------2014/06/10 ...

  7. Linux文件链接hard link与symbolic link

    Linux中文件链接有两种方式,一种是hard link,又称为硬链接:另一种是symbolic link,又称为符号链接.要区分两者的不同要回顾Linux常用的ext2文件系统.这种文件系统使用in ...

  8. 出现 OSError: symbolic link privilege not held的解决方案

    jupyter notebook  出现 OSError: symbolic link privilege not held问题时  以管理员方式重新打开prompt.

  9. ln: creating symbolic link XXXXXX : Operation not supported

    ln: creating symbolic link XXXXXX : Operation not supported 转自:https://blog.csdn.net/z444_579/articl ...

随机推荐

  1. 读取SD卡文件夹下的MP3文件和播放MP3文件

    首先获取SD卡path路径下的所有的MP3文件,并将文件名和文件大小存入List数组(此代码定义在FileUtils类中): /** * 读取目录中的Mp3文件的名字和大小 */ public Lis ...

  2. 《转》iOS音频视频初级开发

    代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS   ...

  3. ubuntu 环境变量修改和恢复总结[收藏]

    在Ubuntu中有如下几个文件可以设置环境变量/etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行./etc ...

  4. CSS3弹性盒模型之Flexbox是布局模块box-sizing & box-orient & box-direction & box-ordinal-group

    css3 box-sizing属性 box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,bord ...

  5. jquery虎牙TV3D轮播特效

    css部分: *{ margin: 0px; padding: 0px; } body{ margin: 0px; padding: 0px; text-align: center; } #banne ...

  6. vue项目总结

    1.项目的结构

  7. 关于常用的编码工具如何引入jar包

    myeclipse和eclipse(差不多)引入jar包: 普通项目: 1.对准你的项目创建一个文件夹名字尽量命名成lib(注意要和src平级,不要在src下创建文件夹). 2.将下载好的依赖放到li ...

  8. 在SpringMVC Controller中注入Request成员域

    主题 在工作中遇到1个问题....我们定义了一个Controller基类,所有Springmvc自定义的controller都继承它....在它内部定义一个@Autowired HttpServlet ...

  9. myBatis + SpringMVC上传、下载文件

    摘自: http://limingnihao.iteye.com/blog/1069503 环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何 ...

  10. Django 打印

    转自:http://bbs.chinaunix.net/archiver/tid-1227401.html fentin 发表于 2008-07-28 17:52:44 请教Django Python ...