Hard link and symbolic link

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

posted @ 2018-04-14 05:12  anyu686  阅读(128)  评论(0编辑  收藏  举报