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. 关于前后端日期处理 开发注意事项 jquery.tmpl()函数的使用

    1当后端将日期传到前段的时候 我们通常会需要将日期转为制定格式 除了平常我们使用的前段插件将日期转好 spring @datetimeFormat 注解 这些形式外 我们还可以在实体里通过get方法进 ...

  2. Event log c++ sample.

    1. Init regedit. bool InitLog( TCHAR *logName, TCHAR *sourceName, TCHAR *MessageDllName ) { // This ...

  3. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  4. Boost入门

    [转载网友转载的 不过不知道原作者地址] Boost入门向导 简介:boost是一套开源的.高度可移植的C++模板库.它由C++标准委员发起,且里面很多组件有望成为下一代的C++标准库,其地位将会与S ...

  5. python-day3-之函数

    不使用函数的情况下只能遵循面向过程的编程,即,根据业务逻辑从上到下实现功能,往往重复的功能,使得代码出现重复. #最简单的函数调用 #由于python遵循从上到下读入内存的规律,所以函数要放到调用的前 ...

  6. day05_07 标志位讲解

    continue: 需求:大于5才打印 for i in range(10): if i<6 : continue print(i) continue作用:结束本次循环,继续下次循环 break ...

  7. [转]手写数字识别错误NameError: name 'mnist' is not defined

    转自:https://blog.csdn.net/coder_Gray/article/details/78562382 在Tensorflow上进行mnist数字识别实例时,出现如下错误 NameE ...

  8. Spring Cloud 目录

    Spring Cloud Eureka Spring Cloud Config Spring Cloud Feign Spring Cloud Hystrix Spring Cloud Ribbon ...

  9. 查看apache和nginx的负载和连接数情况

    1.查看apache当前并发访问数:netstat -an | grep ESTABLISHED | wc -l对比httpd.conf中MaxClients的数字差距多少. 2.查看有多少个进程数: ...

  10. 求中位数为K的区间的数目

    给定一个长为 $n$ 的序列和常数 $k$,求此序列的中位数为 $k$ 的区间的数量.一个长为 $m$ 的序列的中位数定义为将此序列从小到大排序后第 $\lceil m / 2 \rceil$ 个数. ...