Understanding and learning of Linux soft links and hard links

Source: Internet
Author: User
Tags parent directory

To understand the concept of soft links and hard links in Linux, first understand how Linux manages files.

The following notes are part of the content from Internet search, personal understanding to integrate the proceeds.

1. File System (Vamei Source: http://www.cnblogs.com/vamei/archive/2012/09/09/2676792.html)

Files are organized into the file system, which is usually a tree structure. Linux has a root directory/, which is the top of the tree structure. The end of the fork in the tree represents a file, and the fork of the tree is a directory (directory, which is the equivalent of the folder we see in the Windows interface). What you see in Figure 1 is the entire tree of files. If we intercept part of the tree, say, starting from the directory Vamei, it actually forms a filesystem.

To find a file, you need to know all the directory names from the tree root to the file, in addition to the file name of the file. The directory name and file name of all paths starting from the root directory make up a path (path). For example, we look for a file in Linux file.txt, not only to know the file name (file.txt), but also to know the full path, that is, the absolute path (/home/vamei/doc/file.txt). From the root directory/, that is, the top of the tree structure, go through the directory home, Vamei, Doc, finally see the file file.txt. The entire file system layer hierarchy (hierarchy), Vamei is the home subdirectory, and home is the parent directory of Vamei.

In Linux, we use the LS command to display all files in the directory, such as $ls/home/vamei/doc

2. Document management (Ruan Yi Feng Source: http://www.ruanyifeng.com/blog/2011/12/inode.html)

The file is stored on the hard disk, and the minimum storage unit for the hard disk is called "Sector" (Sector). Each sector is stored 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read each sector, so the efficiency is too low, but the one-time continuous reading of multiple sectors, that is, one time to read a block. This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block.

File data is stored in "blocks", then obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of stored file meta information is called Inode, and the Chinese name is "Index node".

Each file has a corresponding inode, which contains some information about the file.

Contents of the 2.1 inode

The inode contains meta information for the file, specifically the following:

* Number of bytes in the file

* User ID of the owner of the file

* The group ID of the file

* file read, write, execute permissions

* File timestamp, total three: CTime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, atime refers to the time when the file was last opened.

* Number of links, that is, how many filenames point to this inode

* Location of File data block

The inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two zones. One is the data area, the file data is stored, and the other is the Inode area (inode table), which holds the information contained in the Inode.

The size of each inode node is typically 128 bytes or 256 bytes. The total number of inode nodes, given at the time of formatting, is usually set to one inode per 1KB or 2KB each. Assuming that the size of each inode node in a 1GB hard disk is 128 bytes, and one inode is set per 1KB, the Inode table will be about 128MB in size, accounting for 12.8% of the entire drive.

Each inode has a number, and the operating system uses Inode numbers to identify different files.

It is worth repeating that the Unix/linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify.

On the surface, the user opens the file by file name. In fact, this process inside the system is divided into three steps: First, the system finds the inode number corresponding to the file name, and secondly, obtains the inode information through the inode number, and finally, according to the Inode information, finds the block of the file data and reads the data.

With the Ls-i command, you can see the inode number corresponding to the file name:

The special role of the Inode

Because the inode number is separated from the file name, this mechanism causes some unix/linux system-specific phenomena.

1. Sometimes the file name contains special characters and cannot be deleted properly. At this point, delete the Inode node directly, you can play the role of deleting files.

2. Move or rename the file, just change the file name without affecting the inode number.

3. After opening a file, the system will identify the file with the Inode number and no longer consider the filename. Therefore, it is generally not possible for the system to know the file name from the inode number.

The 3rd makes software updates simple and can be updated without shutting down the software, without requiring a restart. Because the system through the Inode number, identify the running files, not through the file name. Update, the new version of the file with the same file name, the generation of an inode, will not affect the running files. The next time you run this software, the file name will automatically point to the new version of the file, the old file's inode is recycled.

Hard Links

In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name. However, the Unix/linux system allows multiple filenames to point to the same inode number.

This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. This is referred to as a "hard link".

After running the above command, the source file is the same as the inode number of the destination file, pointing to the same inode. One of the inode information is called the "number of links," which records the total number of filenames pointing to the inode, which increases by 1.

Conversely, deleting a file name causes the "number of links" in the Inode node to be reduced by 1. When this value is reduced to 0, indicating that no file name points to the Inode, the system reclaims the inode number and its corresponding block region.

Since files under Linux are identified by an index node (inode), a hard link can be considered a pointer to a pointer to the inode of the original file, and the system does not reassign the Inode and create the file; that is, the hard-link file and the original file are actually the same file, but the names are different. Each add a hard link, the number of links in the file Inode is added 1, delete a hard link, the inode link number minus 1, the file content is still there, until the Inode link number is 0, only delete the inode corresponding file.

Here, by the way, the "number of links" to the catalog file. When you create a catalog, two catalog entries are generated by default: "." and ".". The inode number of the former is the inode number of the current directory, which is equivalent to the "hard link" of the current directory, and the inode number is the inode number of the parent directory of the current directory, which is equivalent to the "hard link" of the parent directory. Therefore, the total number of "hard links" in any directory is always equal to 2 plus the total number of subdirectories (with hidden directories).

You cannot create links between files in different file systems

You cannot establish hard links across partitions (because different partitions use different super blocks to store inode information)

Soft links

In addition to hard links, there is a special case.

The inode number for file A and file B is different, but the content of file A is the path to file B. When you read file A, the system automatically directs the visitor to file B. Therefore, regardless of which file you open, the final read is file B. At this point, file A is referred to as the "soft link" of File B (soft link) or "Symbolic link" (symbolic).

This means that file a depends on file B and if file B is deleted, opening file A will cause an error: "No such file or directory". This is the biggest difference between soft links and hard links: file a points to file B's file name, not file B's inode number, and file B's Inode "link count" does not change.

The ln-s command can create a soft link.

Qing Feng Source: http://witmax.cn/linux-links.html

Summary of differences

The difference between a soft link and a hard link is not only conceptually, it is also different in implementation, and is organized as follows:

    • For hard links, the original file and the hard link file share an inode number, which means they are the same file, and for soft links, the original file and the soft link file have different inode numbers, indicating that they are two different files;
    • Soft links on file attributes are explicitly written out to be linked files, and hard links are not written out, because in essence the hard-link file and the original file are completely equal relations;
    • The number of links is not the same, the number of links to soft links will not increase;
    • File size is not the same, hard-link file display size is the same as the original file, because it is the same, and here the soft link display size and the original file is different, file1 size is 48B, and File1soft is 5B, which 5 is actually "file1" size.
    • Under the same file system, you can create a soft link or a hard link (or a different directory from the file system).

Understanding and learning of Linux soft links and hard links

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.