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)

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. git使用问题整理

    git访问远端仓库报"fatal: Authentication failed for"错误的,可能原因是账户密码变更,git配置了使用creditial helper,所以需要取 ...

  2. loj2049 「HNOI2016」网络

    好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...

  3. centOS如何设置时间同步

     1.进入系统-管理-时间和日期  2.这个需要root权限才能进行设置,在弹出框中填入root密码  3.设置时间和日期-勾选同步,并且选择NTP时间服务器,点击确定 4.选择时区为亚洲上海点击保存 ...

  4. web自动化测试:watir+minitest(三)

    本文,谢绝转载. 整体框架设计: 1.用例的解耦性.一个测试用例一个脚本.而并非minitest中的N个test写在一个文件中 2.单独调试与全量连跑或部分连跑 3.任意变量.参数配置.这点对后期维护 ...

  5. [cocos2dx utils] cocos2dx读取,解析csv文件

    在我们的游戏中,经常需要将策划的数值配置成csv文件,所以解析csv文件就是一个很common的logic, 例如如下csv文件: 下面是一个基于cocos2dx 2.2.4的实现类: #ifndef ...

  6. Java实现简单的socket通信

    今天学习了一下java如何实现socket通信,感觉难点反而是在io上,因为java对socket封装已经很完善了. 今天代码花了整个晚上调试,主要原因是io的flush问题和命令行下如何运行具有pa ...

  7. Moscow Pre-Finals Workshop 2016. Japanese School OI Team Selection. 套题详细解题报告

    写在前面 谨以此篇题解致敬出题人! 真的期盼国内也能多出现一些这样质量的比赛啊.9道题中,没有一道凑数的题目,更没有码农题,任何一题拿出来都是为数不多的好题.可以说是这一年打过的题目质量最棒的五场比赛 ...

  8. nodeJS学习(6)--- Sublime Text3 配置Node.js 开发环境

    参考:http://www.bubuko.com/infodetail-798008.html http://www.cnblogs.com/bluesky4485/p/3928364.html 1. ...

  9. nodeJS学习(4)--- webstorm/...开发 NodeJS 项目-节1

    前提: 已安装好 IDE ,eg:webstorm/IDEA 2016.3 & 2017.1 nodeJS(含 npm 及 相应的模板等) 要用 webstorm 开发 NodeJS项目(we ...

  10. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...