A hard link created under Linux is equivalent to a copy of the source file, the generated new file and the Inode value of the source file are consistent when the source file is deleted, the hard link does not expire
A soft connection is similar to a shortcut to a source file, the inode of the two is different, it saves the path information of the source file, when the deletion of the source file disappears, the soft connection expires, if in the new source file, the soft link continues to take effect
Test it.
Lab environment: Ubuntu 14.04 Kernel:3.13.0-24-generic
1. 新建一个测试文件
touch test
Use the stat or Ls-i option to view the Inode value of test ' [email protected]:~/work/shell/file$ Stat test file: ' Test ' size: 0 Block: 0 IO BLOCK: 4096 plain empty file device: 801h/2049d inode:4208823 Hard Link: 1 permissions: (0664/-rw-rw-r--) Uid: (1000/ubuntu) Gid: (1000/ubu NTU) last visited: 2018-03-06 15:51:01.689080893 +0800 Last modified: 2018-03-06 15:51:01.689080893 +0800 Recent Changes: 2018-03-06 15:51:01.689080893 +0800 creation Time:-"' Inode value is inode:4208823 2. Create a hard link ' [email protected]:~/work/shell/file$ ln test hard_test [email protected]:~/work/shell/file$ Stat Hard_test file: "hard_test" Size: 0 Block: 0 IO block: 4096 plain empty file device: 801h/2049d inode:4208823 Hard Link: 2 permissions: (0 664/-rw-rw-r--) Uid: (1000/ubuntu) Gid: (1000/ubuntu) last visited: 2018-03-06 15:51:01.689080893 +0800 Last modified: 2018-03-06 15:51: 01.689080893 +0800 Last modified: 2018-03-06 15:53:54.857425605 +0800 creation Time:-"You can see that the inode is the same as 3. Create a soft connection "[email protected]:~/work/shell/file$ ln-s test soft_test[email protected]:~/work/shell/file$ Stat Soft_test
File: "Soft_test", "Test"
Size: 4 Block: 0 IO block: 4096 Symbolic link
Device: 801h/2049d inode:4208824 Hard Link: 1
Permissions: (0777/LRWXRWXRWX) Uid: (1000/ubuntu) Gid: (1000/ubuntu)
last visited: 2018-03-06 15:56:35.129543502 +0800
Last modified: 2018-03-06 15:55:04.201563628 +0800
Recent changes: 2018-03-06 15:55:04.201563628 +0800
creation time:-
"' can be seen, the inode is different, the soft connection and the source file is two different files, but it is saved is the path of the source file is now written in the source file, observe the change of three files" [email protected]:~/work/shell/ file$ echo "123" >test[email protected]:~/work/shell/file$ [email protected]:~/work/shell/file$ Cat test123[email protected]:~/work/shell/file$ Cat Soft_test 123[email protected]:~/work/shell/file$ Cat Hard_test 123[email protected]:~/work/shell/file$ echo "456" >>hard_test [email protected]:~/work/ shell/file$ cat hard_test 123456[email protected]:~/work/shell/file$ cat Test 123456[email protected]:~/ work/shell/file$ Cat soft_testtest cat:soft_testtest: No file or directory [email protected]:~/work/shell/file$ cat Soft_ test123456 ' 4. Delete source file Watch "[email protected]:~/work/shell/file$ RM test[email protected]:~/work/shell/file$ ll Total dosage 12drwxrwxr-x 2 ubuntu ubuntu 4096 March 6 16:01./drwxrwxr-x 3 ubuntu ubuntu 4096 March 6 15:25.. /-rw-rw-r--1 ubuntu Ubuntu 12 March 6 16:00 hard_testlrwxrwxrwx 1 ubuntu Ubuntu 4 March 6 15:55 Soft_Test-test[email protected]:~/work/shell/file$ Cat soft_test cat:soft_test: No file or directory [email protected ]:~/work/shell/file$ cat Hard_test 123456789 "can see the soft connection is invalid, but the hard link is not affected
- Linux has a special instruction Readlink, this command is to get the address of a soft connection, such as the above example can be a soft link to the source file address
[email protected]:~/work/shell/file$ readlink soft_test test
Soft connections and hard links under Linux