Linux commands (i): soft links and hard links

Source: Internet
Author: User

Today, I decided to start learning Linux commands and shell scripts, and use Linux to learn (command line, Shell and other knowledge points) This series of records of their own mental process, content is not in order, only recorded their own feel necessary, simple do not remember!

First point of knowledge: hard links and soft links

Definition of link: In Linux, if you want to maintain two or more copies of the same file, in addition to saving multiple separate physical copies, you can also use only one copy of the physical copy and multiple copies of the virtual copy of the way, this virtual copy is called a link. ---"Linux command line and script encyclopedia"

The above explanation is from the book, can be said to be explained very clearly, according to my own understanding is: In the Linux system, if you want to back up a file, in addition to copying a copy of the original file, but also the way to use the virtual file, compared to window, is actually a more way, because Windows

We usually use the same method of copying the original file to back up the file. But if I just look at this explanation, I still don't quite understand what this virtual means, what is the difference between it and the physical copy? After testing and research, the following conclusions are drawn:

Links are also broken down into soft links and hard links:

1, Hard Link: Hard link is equivalent to copying a copy of the original file, it is identical to the original file, including the file index node number, so it will occupy storage space, if the original file is deleted, it still exists;

2, Soft link: Soft link just creates a pointer to the original file, it has a new file index node number, does not occupy the storage space, if the original file is deleted, it will also expire;

Common points: When the original file content is changed, the content of the linked file will also be modified synchronously;

Ii. introduction of the order

There are two ways to create a linked file, one is the CP command (copy), one is the ln command (link), both can be implemented, from the CP we can know that the link is actually a form of backup files (personal view!). )

CP Command:

Create a hard link: cp-l src des

Create a soft link: cp-s src des

ln command:

Create a hard link: ln src des

Create a soft link: ln-s src des

Here need to say some of their own learning process in the course of the mind: the beginning of the creation of the link is not clear src and Des, do not know exactly which file to create a copy, when the parameters of the src and des exactly which is created after the file, mainly because there is no system of reading,

Just from other people's blog, so until read the book only understand, Src and des means, to the source file src to create a copy des, so when you pass these two parameters, if two files are not in the same directory, we can also be in the same directory as the file to pass in , this place

Really be my own understanding stupid cry!!!

Iii. Command and Combat

Above is the basic concept of the explanation, but also the necessary theoretical part, the following is the practice part, said the right, with the command to verify the not to be seen?

Said the use of the way, the following directly on the command: I take the ln command as an example, CP as long as the attention of the parameters on the line, not to illustrate!

1. Create an empty file as the source file

2. Create a hard link for srcfile.txt

After creating a hard-link desfile1.txt, it is found that the file index values of these two files are the same, stating that they are exactly the same two files.

3. Create a soft link for srcfile.txt

You can see that the file type of the soft link file is L, and the following forms are not the same, there is a symbol such as the source file, and the index value is also inconsistent with the source file, and is newly assigned!

This verifies what is said in the link definition!

4. Modify the contents of the source file to view the contents of the hard link file and the soft link file

From the above practice can be seen, when the source file content modified, the corresponding soft link file and the contents of the hard link file are also modified!

5. Delete source files, view linked files

As can be seen from the above example: when the source file is deleted, the hard-connected files still exist, and valid, the content can also be viewed, but the soft link file is invalid, when viewing the content will be prompted to find the file!

This is not verifying the contents of the link definition!!!

6. Create the source file again

As can be seen from the above example: when I create a new soft link to the file, the soft link file recovery, and point to the new file, while the contents of the file will also change with the new file, but the original hard-link file or delete a copy of the file, not a copy of the new file.

So the soft link will change with the file it points to, as long as the point to the filename is not changed, even if the deletion and new will make it valid, as long as the file name exists:

But the hard link is a physical copy of the source file, if the original file is deleted, it is still valid, create an identical file, it is still the most original source file;

From this can be seen, this soft link is really a bit of a heart, like some boys looking for a girlfriend, divided a feel himself dead, and then found a live again, do not want to hard link, find a on this until the everlasting! haha ~ ~

Well, for the introduction of soft links and hard links to here, the same can be implemented with the CP command Oh!!

Linux commands (i): soft links and hard links

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.