Linux hard links and soft links

Source: Internet
Author: User

In the Linux file system, the disk blocks are divided into 3 parts. Part is called the data area, which is used to store the file contents. The other part, called the Inode table, is used to store file attributes. The third part, called the Super block, is used to store the properties of the file system itself. The contents and attributes of the file are stored separately, and the attributes of the file reside in a data structure called an inode, where the inode is stored in the Inode table, and each inode has a unique number corresponding to its position in the Inode tables.

There are two kinds of links in Linux: Hard links and soft links (Soft link), and soft links are also called Symbolic links (symbolic link).

Hard Links

The hard link actually already exists another name of the file, the link is not different from the original file, they share an inode, and the inode contains all the attributes of a file. When you create a hard chain for a file, the kernel does not allocate a new inode.

Touch F1 LN F1 F1-hard ls-liTotal 010272788-rw-rw-r--2 ng ng 0 Apr 15:28 f110272788-rw-rw-r--2 ng ng 0 Apr 15:28 F1-hard

The system does not allocate a new Inode,f1-hard inode for F1-hard to F1, which shows that they are two names of the same file. After creating the link, the number of links for F1 and F1-hard also increased from 1 to 2. Let's look at the procedure for using the RM command:

Ls-li RM F1 Ls-li RM F1-hard Ls-liTotal 0

When the file is deleted, the system will only reduce the number of links to a file until the number of links to the file is 0 o'clock, the system will not delete the file contents of the disk.

There are two important limitations to hard links:
1) Hard links cannot refer to files outside of their own file system. That is, the link cannot reference a file that is not on the same disk partition as the link.
2) Hard links cannot reference directories (some Linux versions allow Superuser to create a link to a reference directory using the-D option).

Symbolic Links

Symbolic links are created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains a text pointer to a reference file or directory. From this point of view, symbolic links are similar to Windows shortcuts. The file that the symbolic link points to has almost no difference from the symbolic link itself. For example, by writing some content into a symbolic link, the content is also written into the reference file. When you delete a symbolic link, only the symbolic link is removed and the reference file is not deleted. If you delete the reference file before the symbolic link, the link still exists, but it does not point to any files. At this point, this link is called a bad link. The LS command generally uses different colors to display bad links, such as red.

The disadvantage of soft links compared to hard links is that because a soft link contains path information for a referenced file, if the reference file is moved to a different directory, the link becomes a bad link.

ln command format

ln [OPTION] ... [-T] TARGET link_name (1st form)
ln [OPTION] ... TARGET (2nd form)
ln [OPTION] ... TARGET ... DIRECTORY (3rd form)
ln [OPTION] ...-t DIRECTORY TARGET ... (4th form)

ln command Arguments

--backup[=control]
If the specified link name already exists, create a backup file for the file with the same name

-B
Similar to the--backup option, but does not accept parameters.

-D,-F,--directory
Allow super users to create a hard link to a directory (note: Due to system limitations, it is likely to fail, even for super users)

-F,--force
If the specified link name already exists, overwrite the file with the same name

-N,--no-dereference
If the specified link name is a symbolic link to a reference directory, it is treated as a normal file

-I.,--interactive
If the specified link name already exists, prompt to overwrite the file with the same name

-S,--symbolic
Create symbolic links rather than hard links

-S,--suffix=suffix
Use SUFFIX as a suffix for backup files

-V,--verbose
Show information about the link process

--help Display this Help and exit
Printing Help information

--version
Print version Information

ln instance

A) Create a hard link

ln testfile Testfile-hard

b) Creating Symbolic Links

Ln-s testfile Testfile-sym

c) Force the creation of the link, overwriting the file with the same name if the specified link name already exists

Ln-f testfile Testfile-hard

d) Create a backup of the overwritten files

LN-BF testfile Testfile-hard

e) Create a backup of the overwritten file and use ". ~[num]~" as the suffix

Ln-f--backup=numbered testfile Testfile-hard

f) Create a backup of the overwritten file and specify the suffix of the backup file as ". Bak"

Ln-bf-s '. Bak ' testfile testfile-hard

The-n option for Ln

With the Man ln command, the definition of the-n option is "treat destination, which is a symlink to a directory as if it were a normal file", how to understand?

Suppose there is a, B two directories, and a soft link to the A directory C:

mkdir a B Ln-s a C

Now, if you want to change the soft link C to point to the B directory, execute the following command:

LN-SF b C

This does not achieve the desired effect, but instead creates a soft link to the B directory in the C reference directory (that is, the A directory). In this case, the-n option overrides soft link C as a normal file:

LN-SFN b C

Linux hard links and soft links

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.