The ln command-linux command five-minute series 18

Source: Internet
Author: User

This original article belongs to "Linux greenhouse" blog, the blog address is http://roclinux.cn. The author of the article is rocrocket.

In order to prevent the vicious reproduction of some websites, special in each article before adding this information, but also hope that readers understand.

===

[Start of body]

LN is a magical command that can create a shadow of a file or go to another place through a channel. :) In fact, all of these tricks are implemented through soft joins and hard connections.

Let's assume that you have read the "principle of hard connect and soft connect"-"Don't be afraid of the seven of Linux programming", and have a thorough understanding of its principles. Let's start with an example of the LN command!

1 I want to build a Source.txt file hard link, name to do Linksource.txt

[email protected] test]$ cat Source.txt
Hello! source!
[email protected] test]$ ll Source.txt
-rw-rw-r–1 rocrocket rocrocket 2008-10-23 09:25 source.txt
[Email protected] test]$ LN source.txt linksource.txt
[Email protected] test]$ ll-i source.txt linksource.txt
1178119-rw-rw-r–2 rocrocket rocrocket 2008-10-23 09:25 linksource.txt
1178119-rw-rw-r–2 rocrocket rocrocket 2008-10-23 09:25 source.txt

As you can see, the command format in which we establish a hard link is "ln Target link name", and we use ln source.txt linksource.txt to create a hard link to a source.txt file. After that, we use ll-i, the Ls-li command, to view the file's information, where the-I option indicates the Inode node ID for each file that is listed. We can see that the inode numbers of Source.txt and Linksource.txt are exactly the same, all 1178119, which means they all point to the same block of data. This is the hard link.

Note, however, that hard links are not allowed to be built across partitions, which is not allowed across file systems, even for the same type of file system. So hard links can only be built within a single partition.

2 I want to create a soft link to a source.txt file, named Softsource.

[Email protected] test]$ ln-s source.txt Softsource
[Email protected] test]$ ll-i source.txt Softsource
1178211 lrwxrwxrwx 1 rocrocket rocrocket 2008-10-23 09:32 Softsource
1178119-rw-rw-r–2 rocrocket rocrocket 2008-10-23 09:25 source.txt

As you can see, creating a soft link is also using the ln command, but you must add the-s option to establish a soft-link command format of "ln-s Target link name". We can then use the LS command to see that the inode numbers of the soft links softsource and source.txt are not the same, which means that they point completely to two different databases quickly. Moreover, the careful friend can observe the soft link file's permission bar first character is L, this is also the soft chain distinguishes from the ordinary file one place.

If we delete the Source.txt file at this time, the soft link softsource will be programmed with a red font, which indicates a warning that this is a problematic file and it cannot find the file it identifies Source.txt.

3 Just two examples are linked files, the link directory, I want to try!

[Email protected] test]$ LN tempdir linkdir
ln: ' temp ': Hard link isn't allowed for directory

I want to hard link a directory tempdir, but error! Yes, hard links are not allowed to link to directories. (later to analyze the cause, Wulf)

[Email protected] test]$ Ln-s temp Linkdir

The establishment of the soft link of the directory is allowed, see, I built a temp directory of the soft link linkdir, I can completely CD linkdir to enter the temp directory. Like this:

[Email protected] test]$ CD linkdir/
[[email protected] linkdir]$ ls
A001.txt a002.c a.out sixunhuan.c
[Email protected] linkdir]$ CD. /temp/
[[email protected] temp]$ ls
A001.txt a002.c a.out sixunhuan.c
[Email protected] temp]$

Let's look at the property information for these two directories:

[Email protected] test]$ ll|grep-e "Temp|linkdir"
lrwxrwxrwx 1 rocrocket rocrocket 4 2008-10-23 09:49 Linkdir-Temp
Drwxr-xr-x 2 rocrocket rocrocket 4096 2008-08-07 16:08 Temp

You can see that linkdir is a soft link, and temp is a directory.

4 Why does LN not allow a hard link to a directory, while allowing a soft link to the directory?

This problem, I found a good answer on the Internet. The friend explained it very clearly. (I can not find the source of the following articles and the original author information, it is not possible to identify the original author information, if there are people who know, can contact me quickly, I will promptly add the author information.) Thank you)

"There are two restrictions on hard connections in a Linux system: You cannot cross the file system and do not allow ordinary users to make hard connections to the directory. As for the first limitation, it is well understood, and the second one is not so well understood. We can see in any directory with the ls-l command is at least 2 of the number of connections, which also shows that there is a hard connection in the system, and the command ln-d can also allow the superuser to make a hard connection to the directory, which shows that the system restricts the hard connection to the directory is only a hard rule, is not logically disallowed or technically non-line. So why should the operating system limit it?

If a hard connection to a directory is introduced, it is possible to introduce loops into the directory, and the system will fall into an infinite loop when the directory is traversed. Perhaps you would say that symbolic connections can also introduce loops, so why not restrict the symbolic connection of the directory? The reason is that in the Linux system, each file (directory is also a file) corresponds to an inode structure, where the Inode data structure contains the file type (directory, ordinary files, symbolic connection files and so on) information, which means that the operating system can traverse the directory to determine the symbolic connection, Since you can determine the symbolic connection of course, you can take some measures to prevent entering a large cycle, the system in the continuous encounter 8 symbols connected to stop the traversal, which is why the directory symbol connection will not enter the cause of the dead loop. But for the hard connection, because of the data structure and the algorithm limitation in the operating system, this kind of dead loop can't be prevented at present. ”

The 5 ln command has an-n option in it, and its official explanation is this:-n,–no-dereference treat destination That's a symlink to a directory as if it were a normal File. What does this mean, please?

There is a similar post on the water wood, Jianingy gives a clear example, as follows:

First step: mkdir a B
Step two: Ln-sf a C (at this time C-a)
Step three: If you execute LN-SF b C, you will create a B symbolic link in the C directory, but obviously this is not your intention.
If you replace the third step with: LN-SNF b c Then the result becomes c->b

The ln command-linux command five-minute series 18

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.