Linux hard link and soft link, linux Link

Source: Internet
Author: User

Linux hard link and soft link, linux Link

In a Linux File System, disk blocks are divided into three parts. A part is called a data zone and used to store the file content. Another part is called inode table, which is used to store file attributes. The third part is called a super block, which is used to store the attributes of the file system. The content and attributes of the file are stored separately. The attributes of the file are stored in a data structure called inode, and inode is stored in the inode table, each inode has a unique number corresponding to its position in the inode table.

There are two types of links in Linux: Hard Link and Soft Link, also known as Symbolic Link ).

 

Hard Link

Hard links actually have another name of the file. The link is no different from the original file. They share an inode, and inode contains all the attributes of a file. When a hard chain is created for a file, the kernel does not allocate a new inode.

huey@huey-K42JE:~/huey/linux/cmdline$ touch f1huey@huey-K42JE:~/huey/linux/cmdline$ ln f1 f1-hardhuey@huey-K42JE:~/huey/linux/cmdline$ ls -litotal 010272788 -rw-rw-r-- 2 ng ng 0 Apr 20 15:28 f110272788 -rw-rw-r-- 2 ng ng 0 Apr 20 15:28 f1-hard

The system did not assign a new inode for the f1-hard, And the inode for the f1-hard is consistent with f1, which shows that they are two names for the same file. After a link is created, the number of links for f1 and f1-hard also increases from 1 to 2. Let's look at the process of using the rm command:

huey@huey-K42JE:~/huey/linux/cmdline$ ls -litotal 010272788 -rw-rw-r-- 2 ng ng 0 Apr 20 15:33 f110272788 -rw-rw-r-- 2 ng ng 0 Apr 20 15:33 f1-hardhuey@huey-K42JE:~/huey/linux/cmdline$ rm f1huey@huey-K42JE:~/huey/linux/cmdline$ ls -litotal 010272788 -rw-rw-r-- 1 ng ng 0 Apr 20 15:33 f1-hardhuey@huey-K42JE:~/huey/linux/cmdline$ rm f1-hardhuey@huey-K42JE:~/huey/linux/cmdline$ ls -litotal 0

When you delete a file, the system only deletes the File Link count by one until the File Link count is 0.

There are two important restrictions on hard links:
1) hard links cannot reference files outside the file system. That is, the link cannot reference files that are not in the same disk partition as the link.
2) hard links cannot reference directories (in some Linux versions, superusers are allowed to use the-d option to create links to referenced Directories ).

 

Symbolic Link

Symbolic Links are created to overcome the limitations of hard links. A symbolic link is used to create a special type of file, which contains a text pointer to the referenced file or directory. From this point of view, symbolic links and Windows shortcuts are very similar. The file to which the symbolic link points is almost the same as the symbolic link itself. For example, if some content is written into the symbolic link, the content is also written into the reference file. When a symbolic link is deleted, only the symbolic link is deleted and the referenced file is not deleted. If the Referenced File is deleted before the symbolic link, the link still exists, but does not point to any file. In this case, this link is called a bad link. The ls command usually uses different colors to display bad links, such as red.

A disadvantage of a soft link over a hard link is that the soft link contains the path information of the referenced file, so if the Referenced File is moved to another 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 Parameters

-- 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 a Super User to create a hard link to the directory (Note: Due to system restrictions, it is very likely that the system will fail, even the Super User)

-F, -- force
If the specified Link name already exists, the file with the same name will be overwritten.

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

-I, -- interactive
If the specified Link name already exists, the system prompts whether to overwrite the file with the same name.

-S, -- symbolic
Create a symbolic link instead of a hard link

-S, -- suffix = SUFFIX
Use SUFFIX as the SUFFIX of the backup file

-V, -- verbose
Display link Process Information

-- Help display this help and exit
Print help information

-- Version
Print Version Information

 

Ln instance

A) create a hard link

ln testfile testfile-hard

B) Create a symbolic link

ln -s testfile testfile-sym

C) force create a link. If the specified Link name already exists, overwrite the file with the same name.

ln -f testfile testfile-hard

D) create a backup for the overwritten File

ln -bf testfile testfile-hard

E) create a backup for the overwritten file and use ".~ [Num] ~ "As suffix

ln -f --backup=numbered testfile testfile-hard

F) create a backup for the overwritten file and specify the suffix of the backup file as ". bak"

ln -bf -S '.bak' testfile testfile-hard

 

-N option of ln

Run the man ln command. The meaning of the-n option is "treat destination that is a symlink to a directory as if it were a normal file". How can this problem be solved?

Suppose there are two directories a and B, and there is a soft link c pointing to directory:

huey@huey-K42JE:~/huey/linux/cmdline$ mkdir a bhuey@huey-K42JE:~/huey/linux/cmdline$ ln -s a c

If you want to change soft link c to B directory, execute the following command:

huey@huey-K42JE:~/huey/linux/cmdline$ ln -sf b c

This does not achieve the expected results, but creates a soft link to directory B in the Reference Directory (that is, directory a) of directory c. In this case, the-n option will overwrite soft-link c as a normal file:

huey@huey-K42JE:~/huey/linux/cmdline$ ln -sfn b c

 

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.