Hard links and soft link is an important concept in the Linux file system, which relates to the index node in the file system (inode). Inode is one of the four basic concepts in Linux virtual file system (VFS). Through the analysis of the relation and the difference between hard links and soft links, we can better understand Linux VFS.
We know that the file has file name and data, which is divided into two parts: user data and metadata. The user data, namely the file data blocks (data block), data block is a place that store the real content. metadata is additional properties of file, such as: file size, created time of file, owner information of file. In Linux, the inode in the metadata, (inode is a part of the file metadata but which do not contain a file name, inode, namely the inode number) is uniquely identifies of the file rather than the file name. The file name is only for memory and is convenient for people to use. through the inode number, system can find the correct file data block. The below figure shows how the program obtain the contents of the file by file name.
Checking the inode number in the Linux system can use “stat” or “ls -i” command . The following example will use MV command to move and
rename one file, which do not affect the user data of file and inode number.
1 [root@mylinux ~]# stat inodetest
2 File: `inodetest’
3 Size: 22 Blocks: 8 IO Block: 4096 regular file
4 Device: ca01h/51713d Inode: 286730 Links: 1
5 Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
6 Access: 2014-01-14 22:48:58.000000000 +0800
7 Modify: 2014-01-14 22:48:58.000000000 +0800
8 Change: 2014-01-14 22:48:58.000000000 +0800
9 [root@mylinux ~]# mv inodetest /tmp
10 [root@mylinux ~]# ls -i /tmp/inodetest
11 286730 /tmp/inodetest
12 [root@mylinux ~]#
In order to solve the problem of file sharing, Linux system introduce two links: hard link (hard link) and soft link (also called a symbolic link). A link will resolve file sharing for the Linux system. If a inode is correspond to multiple file name, said these files as hard links. In other words, a hard link is the same file with multiple aliases. Hard links can be created by the command link or ln.
The following example is to create hard link:
1 [root@mylinux tmp]# link inodetest itest
2 [root@mylinux tmp]# ln inodetest lnitest
3 [root@mylinux tmp]# ls -i
4 286730 inodetest 286730 itest 286730 lnitest
Because of the hard link have the same inode, just only have the different file name, hard links have the following characteristics:
• Files with the same inode and data block
• Only on the existing file to create
• Do not cross file system to create hard links
• Cannot create hard link for directory
• Delete a hard link file does not affect other have the same inode file.
Examples:
1 [root@mylinux hardlink]# ls -li
2 total 0
3 //Only on the existing file to create hard link
4 [root@mylinux hardlink]# link hlinko hlinkn
5 link: cannot create link `hlinkn’ to `hlinko’: No such file or directory
6 [root@mylinux hardlink]# echo “this is hard links test” > hlinko
7 [root@mylinux hardlink]# cat hlinko
8 this is hard links test
9 [root@mylinux hardlink]# stat hlinko
10 File: `hlinko’
11 Size: 24 Blocks: 8 IO Block: 4096 regular file
12 Device: ca01h/51713d Inode: 811019 Links: 1
13 Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
14 Access: 2014-01-14 23:17:03.000000000 +0800
15 Modify: 2014-01-14 23:16:54.000000000 +0800
16 Change: 2014-01-14 23:16:54.000000000 +0800
17 [root@mylinux hardlink]# link hlinko hlinkn
18 //Files with the same inode and data block
19 [root@mylinux hardlink]# ls -li
20 total 8
21 811019 -rw-r–r– 2 root root 24 Jan 14 23:16 hlinkn
22 811019 -rw-r–r– 2 root root 24 Jan 14 23:16 hlinko
23 //Do not cross file system to create hard links
24 [root@mylinux hardlink]# ln /dev/cpu/0/cpuid /tmp
25 ln: creating hard link `/tmp/cpuid’ => `/dev/cpu/0/cpuid’: Invalid cross-device link
26 //hard link not allowed for directory
27 [root@mylinux hardlink]# mkdir test
28 [root@mylinux hardlink]# ln test test.new
29 ln: `test’: hard link not allowed for directory
Soft link is a common file, there is only a little special for the content of data block. A soft link has its own inode number and the user data block , pls see the below figure. So soft link is created without many restrictions similar with hard links:
• A soft link has its own file attributes and authority
• You can create a soft link to the file or directory does not exist
• A soft link can cross file system
• Soft links can be created to the file or directory
• Create a soft link, link count i_nlink does not increase
• Removing the soft link does not affect the pointed file, but the original file if it is to be deleted, then the soft links is called the dead link
• Samples:
1 [root@mylinux softlink]# ls -l
2 total 0
3 //create a soft link to the file or directory does not exist
4 [root@mylinux softlink]# ln -s softo softn
5 [root@mylinux softlink]# ls -li
6 total 0
7 811022 lrwxrwxrwx 1 root root 5 Jan 15 11:26 softn -> softo //dead link
8 [root@mylinux softlink]# cat softn
9 cat: softn: No such file or directory
10 //create the file that soft link pointed
11 [root@mylinux softlink]# echo “this is soft link test” >> softo
12 [root@mylinux softlink]# cat softn
13 this is soft link test
14 [root@mylinux softlink]# ln -s softdiro softdirnew //create soft link to directory
15 [root@mylinux softlink]# mkdir -p softdiro/test
16 [root@mylinux softlink]# ls -li
17 total 8
18 811024 lrwxrwxrwx 1 root root 8 Jan 15 11:29 softdirnew -> softdiro
19 811025 drwxr-xr-x 3 root root 4096 Jan 15 11:29 softdiro
20 811022 lrwxrwxrwx 1 root root 5 Jan 15 11:26 softn -> softo
21 811023 -rw-r–r– 1 root root 23 Jan 15 11:28 softo
22
23 That’s all…