Soft link Hard link

Source: Internet
Author: User

Links and differences between hard links and soft links

We know that files have filenames and data, which are divided into two parts on Linux:
metadata (metadata) is an attached property of a file, such as file size, creation time, owner, and so on.
User Data is the file data block, the data block is the place to record the real content of the file;

The metadata is in Linux, the inode number in the metadata (the Inode is part of the file metadata but it does not contain the filename, and the inode number, the index node number) is the file's unique identity rather than the filename. The file name is only for the convenience of people's memory and use, the system or program through the inode number to find the right file data block. Figure 1 shows the process by which a program obtains the contents of a file through a filename. Figure 1 Opening a file by file name

To address shared use of files, the Linux system introduces two types of links:
Hard links (hard link)
Soft links (also known as symbolic links, that is, soft link or symbolic link).

Links for Linux systems to solve the shared use of files, but also brings the hidden file path, increase the security of permissions and save storage, and other benefits.

If an inode number corresponds to multiple file names, these files are referred to as hard links .
In other words, a hard link is the same file that uses multiple aliases. Hard links can be created by command link or ln. The following is the creation of a hard link to the file oldfile.

Because a hard link is a file with the same inode number and only a different file name, therefore, the hard link has the following characteristics: The file has the same inode and data block, can only be created for existing files, cannot cross file system for hard link creation, can not create the directory, only to the file creation ; Deleting a hard link file does not affect other files that have the same inode number.

The file is a soft connection if the contents of the file user data block are pointed to the path name of another file .
Soft link is a common file, but the content of the data block is somewhat special. A soft link has its own inode number and user data block. Therefore, the creation and use of soft links do not have many restrictions similar to hard links:

Soft links have their own file attributes and permissions, etc. soft links can be created for files or directories that do not exist; Soft links can be cross file systems; Soft links can be created on files or directories; When you create soft links, the link count I_nlink does not increase; deleting soft links does not affect the file being pointed to, but if the original file being pointed to is deleted, Then the related soft connection is called the dead link (that is, dangling link, if the path file is recreated, the dead link can revert to the normal soft link).

Linux systems control the deletion of files by the number of link numbers, and only if a file does not exist any link, the file is deleted.

In general, each file has two link counters to control
I_count and I_nlink.
I_count adds 1 When a file is occupied by a program.
I_nlink also adds 1 when you have one more hard link to the file.
Delete a file, that is, let this file, no process occupy, while the number of I_link is 0.

Extensions:

1. View the inode number in a Linux system using the command stat or ls-i
Using command MV to move and rename file glibc-2.16.0.tar.xz, the result does not affect the file's user data and inode number, before and after the file moved inode number: 2485677. move or rename a file

stat /home/harris/source/glibc-2.16.0.tar.xz 
  File: '/home/harris/source/glibc-2.16.0.tar.xz '
  size:9990512   	 blocks:19520      IO block:4096   Regular file 
 device:807h/2055d 	 links:1 
 Access: (0600/-rw-------)  Uid: (1000/  Harris)   Gid: (1000/  Harris) 
 ... ... 
 # mv/home/harris/source/glibc-2.16.0.tar.xz/home/harris/desktop/glibc.tar.xz 
 # ls-i -f/home/ Harris/desktop/glibc.tar.xz 
   /home/harris/desktop/glibc.tar.xz

2. The inode number is unique only under each file system, and the number of inode repeats occurs after Linux mounts multiple file systems

DF View the file system types that are mounted in the current system, the usage of the file system Inode, and the file system mount point. find files with the same inode number

#  filesystem     Type       inodes  iused    ifree iuse% mounted on 
 /dev/sda7      ext4      3147760 283483  2864277   10%/ 
 udev           devtmpfs   496088 553 495535 1%/    Dev 
 tmpfs          tmpfs      499006    491   498515    1%/run 
 None           tmpfs      499006      3   499003    1%/run/lock 
 None           tmpfs      499006 
 498991      1%/RUN/SHM Fuseblk  74383900   4786 74379114  1%  /media/diske/dev/sda8 fuseblk 29524592 19939 29504653    1%/media/diskf 

 #  /media/diske/pictures/t3.jpg 
 /media/diskf/123.txt 
 /sync

The 3.Linux system has an inode number that is run out but there is still disk space left over.

4. find soft links and hard links using command found

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.