Differences between the ln command soft link and hard link

Source: Internet
Author: User

Command: ln usage

Command name: ln

Permission: All Users

Usage: ln [Options] source Dist, where the option format is:

[-Bdfinsvf] [-s backup-suffix] [-V {numbered, existing, simple}]

[-- Help] [-- version] [--]

Note: there is a so-called link in the Linux/Unix File System. we can regard it as the alias of the file, and the link can be divided into two types: hard link) symbolic Link means that a file can have multiple names, while a soft link generates a special file, the content of this file points to another file. Hard links exist in the same file system, but soft links can span different file systems.

Ln source dist is used to generate a connection (DIST) to the source. As for hard link or soft link, it is determined by the parameter.

No matter whether it is a hard link or soft link, it will not copy the original file, it will only occupy a very small amount of disk space.

Parameter format:

-F: when the link is closed, the file with the same name as dist is deleted first.

-D: allows system administrators to hard link their directories.

-I: When deleting an archive with the same name as Dist, first ask

-N: dist is considered as a normal file During Soft link.

-S: Symbolic Link)

-V: display the file name before the link

-B: Back up the files that will be overwritten or deleted during the link.

-S Suffix: adds the backup file to the end of suffix.

-V method: Specifies the backup method.

-- Help: displays auxiliary instructions

-- Version: displays the version.

Example:

Generate a symbolic link: ZZ

Ln-s YY ZZ

Generate a hard link: ZZ

Ln YY xx

Note:

By default, the ln command establishes a hard link.
-S does not mean soft, but symbolic, so it should be a symbolic link.

First, the ln command will keep the synchronization of each link file. That is to say, other files will change the same no matter which one you have changed;
Second, ln links are divided into two types: symbolic links and hard links,The symbolic link is ln-s *****. It will only generate an image of a file at the position you selected and will not occupy disk space.,Hard link ln ***, without the parameter-S, it will generate a file of the same size as the source file at the position you selectedFiles remain in sync, whether it is a symbolic link or a hard link.

Differences Between Soft link and hard link:

Before talking about the differences between the two, we should first clarify that in Linux, the kernel allocates an inode (index node) for each newly created file, and each file has a unique inode number. The file attributes are stored in the index node. when accessing the file, the index node is copied to the inner to implement fast file access. A link is a way to establish a connection between a shared file and several directory items of the users who access it. Linux contains two types of links: hard link and soft link. Soft link is also called symbolic link ).

1. Hard link:

The hard link is a white pointer pointing to the file index node, and the system does not re-allocate inode for it.

Method: ln sourcefile hardfile

After creating a hard link, run the LS-il command to check that the number of links between sourcefile and hardfile has changed to 2, and the size of the files displayed by sourcefile and hardfile is the same. That is, sourcefile and hardfile are two names of the same file. They have the same index node number and file attributes and create a hard link to the file sourcefile, creates a new pointer for the file index node of sourcefile in the current directory. Delete any of them, such as RM hardfile. Only one pointer is deleted at a time.

However, you cannot create hard links between files in different file systems !!!

2. Soft link (symbolic link)

Soft links overcome hard links without any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks.

Method: ln-s sourcefile softfile

Differences:

A. A public inode number is used for the hard-link original file and the link file, indicating that they are the same file. The soft-link original file and the link file have different inode numbers, indicates that they are two different files;

B. The soft link on the file property clearly states that it is a link file, but the hard link is not written, because in essence the hard link file is completely equal to the original file;

C. The number of links is different. The number of soft links does not increase, but the number of hard links increases;

D. the file size is different. The displayed size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, here, the size of the soft link is different from that of the original file. For example, the size of sourcefile is 48B, while that of filesoft is 5B or 4B. 5 is actually the size of "filesoft;

E. Soft links can be cross-file systems, but hard links cannot.

F.The symbolic link is ln-s *****. It will only generate an image of a file at the position you selected and will not occupy disk space.,Hard link ln ***, without the parameter-S, it will generate a file of the same size as the source file at the position you selectedFiles remain in sync, whether it is a symbolic link or a hard link.

G. when creating a soft link, because the link file contains the path information of the original file, when the original file is moved from a directory to another directory (or RM sourcefile), then access the linked file, the system cannot be found. Hard links do not have this defect, so you can move them as needed. In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path.

In short, creating a soft link is to create a new file. When you access a linked file, the system will find that it is a linked file. It reads the linked file and finds the file to be accessed.

Another explanation :::

1. When a bucket has several hard links, deleting one of them will not operate on the bucket, so other hard links will not be affected.
This method is similar to the pointer in the C language. When a bucket has several pointers pointing to it, deleting a pointer does not result in the bucket being recycled.

2. the symbolic link can be understood as the index pointing to the index of the original file. it is different from the original file. so if there are three files, A is the original file, B is the hard link of A, and C is the symbolic link of. no matter the file or directory content changes, it will change the content of other files. for the deletion of the original file, C will become unavailable, but it has no impact on B. deleting B and C will not affect any files.

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.