Shell script learning-9 (LN command learning)

Source: Internet
Author: User

[Starting text]

Ln is a magic command. It can create a file shadow or enter another place through one channel. :) In fact, all these tricks are implemented through soft connections and hard connections.

Let us assume that you have read the article "Principles of hard connection and soft connection-" Do not be afraid of Linux programming "7" and have a thorough understanding of its principles. The following is an example of how to start the ln command!

 

1. I want to create a hard link for the source.txt file and use the name as linksource.txt

[Rocrocket @ wupengchong test] $ cat source.txt
Hello! Source!
[Rocrocket @ wupengchong test] $ ll source.txt
-RW-r-1 rocrocket 14 source.txt
[Rocrocket @ wupengchong test] $ ln source.txt linksource.txt
[Rocrocket @ wupengchong test] $ ll-I source.txt linksource.txt
1178119-RW-r-2 rocrocket 14 linksource.txt
1178119-RW-r-2 rocrocket 14 source.txt

We can see that the command format for creating a hard link is "ln target Link name". We use ln source.txt linksource.txtto create a hard link for the source.txt file. Then, we use ll-I, that is, the inode numbers of LS partitions are exactly the same, all of which are 1178119, which means they all point to the same data block. This is the hard link.

However, you must note that hard links cannot be created across partitions, that is, they cannot be created across file systems, even for the same type of file systems. Therefore, hard links can only be created in one partition.

2. I want to create a soft link to the source.txt file named softsource.

[Rocrocket @ wupengchong test] $ ln-s source.txt softsource
[Rocrocket @ wupengchong test] $ ll-I source.txt softsource
1178211 lrwxrwxrwx 1 rocrocket 10 softsource-> source.txt
1178119-RW-r-2 rocrocket 14 source.txt

As you can see, creating a soft link also uses the ln command, but the-s option must be added. The command format for creating a soft link is "ln-s target Link name ". Then, we can use the lscommand to export the inode numbers of softsourceand source.txt to a soft chain, which means they point completely to two different databases. Moreover, careful friends can observe that the first character in the permission column of the soft link file is l, which is also one of the differences between soft links and common files.

Stopped.

3. The two examples just now are linked files. What about the link directory? I 'd like to try it!

[Rocrocket @ wupengchong test] $ ln tempdir linkdir
LN: 'temp ': hard link not allowed for directory

I want to hard link a directory tempdir, but an error is returned! Yes, hard links are not allowed to be linked to directories. (I will analyze the cause later)

[Rocrocket @ wupengchong test] $ ln-s temp linkdir

The creation of soft links of directories is allowed. Check that I have established a soft link linkdir for the temp directory. Later, I can access the temp directory by using Cd linkdir. Like this:

[Rocrocket @ wupengchong test] $ CD linkdir/
[Rocrocket @ wupengchong linkdir] $ ls
A001.txt a002.c A. Out sixunhuan. c
[Rocrocket @ wupengchong linkdir] $ CD ../temp/
[Rocrocket @ wupengchong temp] $ ls
A001.txt a002.c A. Out sixunhuan. c
[Rocrocket @ wupengchong temp] $

Let's check the attributes of these two directories:

[Rocrocket @ wupengchong test] $ ll | grep-E "Temp | linkdir"
Lrwxrwxrwx 1 rocrocket 4 2008-10-23 09:49 linkdir-> temp
Drwxr-XR-x 2 rocrocket 4096 temp

We can see that linkdir is a soft link, while temp is a directory.

4 Why does ln not allow hard link to a directory, but soft link to a directory?

I found a good answer to this question on the Internet. This friend explained clearly.(I can't find the source and original author information of the following articles, so I cannot identify the original author information. If you have any suggestion, please contact me quickly. I will add the author information in time. Thank you)

"There are two restrictions on hard connections in Linux: they cannot span the file system and do not allow common users to make hard connections to directories. As for the first limit, it is easy to understand, and the second limit is not so easy to understand. Using the LS-l command on any directory, we can see that the number of connections is at least 2, which also indicates that there is a hard connection in the system, the command ln-D can also allow the Super User to make a hard connection to the directory, which means that the system restricts the hard connection to the directory, it is not logically not allowed or technically unavailable. Why is the operating system restricted?

If a hard connection to the directory is introduced, loops may be introduced in the directory, and the system will be stuck in an infinite loop during directory traversal. Maybe you will say that the symbolic connection can also introduce a loop, so why not restrict the symbolic connection of the directory? In Linux, each file (directory is also a file) corresponds to an inode structure. The inode data structure contains the file type (directory, common file, information, that is, the operating system can determine the symbolic connection when traversing the directory, now that you can determine the symbolic connection, you can take some measures to prevent a large loop. The system stops traversing after eight consecutive symbolic connections, this is why directory symbolic connections do not enter an endless loop. However, due to the data structure and algorithm restrictions in the operating system, hard connection cannot prevent such an endless loop ."

5 The ln command contains the-n option. Its official explanation is as follows:-N, -No-dereference treat destination that is a symlink to a directory as if it were a normal file. what does this mean?

There is a similar post on shuimu, and jianingy gives a clear example, as follows:

Step 1: mkdir A B
Step 2: ln-sf a c (c-> A at this time)
Step 3: If ln-sf B C is executed"Inside"Create a symbolic link for B, but obviously this is not your intention.
If you replace step 3 with Ln-snf B c, the result is changed to C-> B.

Related Article

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.