Hard link and symbolic link

本文介绍了Linux系统中硬链接和软链接的区别及使用场景。硬链接指向硬盘上的具体位置,多个硬链接文件指向同一位置时,修改一个文件的内容会影响所有相关联的文件。而软链接则指向另一个文件,对软链接文件的修改不会影响到原文件。

There are 2 types of links that you can create within Linux:

https://medium.com/@krisbredemeier/the-difference-between-hard-links-and-soft-or-symbolic-links-780149244f7d  

 

Hard links point, or reference, to a specific space on the hard drive. You can have multiple files hard linked to the same place in the hard drive, but if you change the data on one of those files, the other files will also reflect that change.

Symbolic links work a bit differently. A symbolic link still points to a specific point on the hard drive, but if you create a second file, this second file does not point to the harddrive, but instead, to the first file.

 

 

Why Use Hard Links?

Hard links provide an efficient way to organize files. The easiest way to describe this is with an old Sesame Street episode.

Bert told Ernie to tidy away all his things and so Ernie set about his task. First of all, he decided to tidy away all the red things. "The fire engine is red". So Ernie puts the fire engine away.

Next Ernie decides to put away all the toys with wheels. The fire engine has wheels. So Ernie tidied the fire engine away.

Needless to say, Bert comes home to find exactly the same mess as before but Ernie had tidied the fire engine away half a dozen times.

 

Imagine that the fire engine was just a picture of a fire engine. You could have different folders on your machine as follows:

  • Photos of red things
  • Photos of vehicles
  • Photos of emergency services

Now you could create a copy of the photo and place it in each of the folders. This means you have three copies of the same file taking up three times the space.

Categorizing photos by making copies of them might not take up too much space but if you tried the same thing with videos you would significantly reduce your disk space.

A hard link takes up no space at all. You could, therefore, store the same video in various different categories (i.e. by year, genre, cast, directors) without reducing your disk space.

How to Create a Hard Link

You can create a hard link using the following syntax:

ln path/to/file /path/to/hard/link

For example, in the image above we have an Alice Cooper music folder called Trash in the path /home/gary/Music/Alice Cooper/Trash. In that folder, there are 10 songs one of which is the classic Poison.

Now Poison is a rock track so we created a folder called Rock under the music folder and created a hard link to Poison by typing the following file:

ln "01 - Poison.mp3" "~/Music/rock/Poison.mp3"

This is a good way to organize music.

How To Tell The Difference Between A Hard Link And A Symbolic Link

You can tell if a file has a hard link by using the ls command:

ls -lt

A standard file without links will look as follows

-rw-r--r-- 1 gary gary 1000 Dec 18 21:52 poison.mp3

The columns are as follows:

  • -rw-r--r-- = permissions
  • 1 = number of links
  • gary = group
  • gary = owner
  • 1000 = file size
  • Dec 18 = date
  • 21:52 = time
  • poison.mp3 = filename

If this was a hard link the output would look as follows:

-rw-r--r-- 2 gary gary 1000 Dec 18 21:52 poison.mp3

Notice that the number of links column shows 2. Every time a hard link is created that number will increase.

A symbolic link will look as follows:

-rw-r--r-- 1 gary gary 1000 Dec 18 21:52 poison.mp3 -> poison.mp3

You can clearly see that one file is pointing to another.

How To Find All Hard Links To A File

All files in your Linux system contain an inode number which uniquely identifies the file. A file and its hard link will contain the same inode.

To see the inode number for a file type the following command:

ls -i

The output for a single file will be as follows:

1234567 filename

To find the hard links for a file you just need to do a file search for all the files with the same inode (i.e. 1234567).

You can do that with the following command:

find ~/ -xdev -inum 1234567

or find / -samefile filename

转载于:https://www.cnblogs.com/anyu686/p/8828635.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值