Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Soft Link and hard Link under linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Creating a hard link under linux is equivalent to a copy of the source file, and the inode values of the generated new file and the source file are the same. When the source file is deleted, the hard link will not fail. The soft link is similar to the shortcut of the source file. The inode of the two is different. The path information of the source file is stored in it. When the source file is deleted, the soft link becomes invalid. If the source file is newly created, the soft link continues to take effect.

Test it

Experimental environment: ubuntu 14.04kernel:3.13.0-24-generic

1. Create a new test file touch test use the stat or ls-I options to view the ubuntu@ubuntu-virtual-machine:~/work/shell/file$ stat test value of test ```ubuntu@ubuntu-virtual-machine:~/work/shell/file$ stat test file: "test" size: 0 block: 0 IO block: 4096 ordinary empty file device: 801h/2049d Inode:4208823 hard link: 1 permission: (0664Accord RWMAT RWMAT -) Uid: (1000 / ubuntu) Gid : (1000 / ubuntu) recent visit: 2018-03-06 15 purl 01.689080893 + 0800 recent changes: 2018-03-06 15 purl 01.689080893 + 0800 recent changes: 2018-03-06 15 51laze 01.689080893 + 0800 creation time:-``inode value is Inode:4208823 2. Create a hard link ```ubuntu@ubuntu-virtual-machine:~/work/shell/file$ ln test hard_test ubuntu@ubuntu-virtual-machine:~/work/shell/file$ stat hard_test file: "hard_test" size: 0 block: 0 IO block: 4096 ordinary empty file device: 801h/2049d Inode:4208823 hard link: 2 permissions: (0664gamma) Uid : (1000 / ubuntu) Gid: (1000 / ubuntu) recent visit: 2018-03-06 15 ubuntu 51V 01.689080893 + 0800 recent changes: 2018-03-06 15 ubuntu 51V 01.689080893 + 0800 recent changes: 2018-03-06 1515 ubuntu 54.857425605 + 0800 creation time:-```you can see that the inode is the same 3. Create a soft connection ```ubuntu@ubuntu-virtual-machine:~/work/shell/file$ ln-s test soft_testubuntu@ubuntu-virtual-machine:~/work/shell/file$ stat soft_test

File: "soft_test"-> "test"

Size: 4 blocks: 0 IO blocks: 4096 symbolic links

Device: 801h/2049d Inode:4208824 hard link: 1

Permission: (0777/lrwxrwxrwx) Uid: (1000 / ubuntu) Gid: (1000 / ubuntu)

Last visit: 2018-03-06 15 56 purl 35.129543502 + 0800

Last modified: 2018-03-06 15 15 5555 04.201563628 + 0800

Last modified: 2018-03-06 15 15 5555 04.201563628 + 0800

Creation time:-

As you can see, inode is different. The soft connection and the source file are two different files, but what it saves is the path of the source file. Now the content is written in the source file. Observe the changes of the three files ```ubuntualubuntuaubuntuaubuntuaubuntuaUBULYUTUBUTUBUTUBUTUBUTUBUBUTUBUBUTUBUTUBUBUTUBUTUBUTUBUBUTUBUTUBUBUTUBUBUTUBUTUBUTUBUTUBUTUBUBUTU UBUTUTUBUTUBUTUBUBUTUBUTUBUTUBUBUTUBUTUBUUTUBUTUBUBUTUBUTUBUTULING. The changes of the three files Ubuntu@ubuntu-virtual-machine:~/work/shell/file$ cat hard_test 123456ubuntur Ubuntur Ubuntur soft_testtest: soft_testtest: there is no such file or directory ubuntu@ubuntu-virtual-machine:~/work/shell/file$ cat soft_ Test123456````4. Delete source files to observe the total amount of ```ubuntu@ubuntu-virtual-machine:~/work/shell/file$ rm testubuntu@ubuntu-virtual-machine:~/work/shell/file$ ll 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-> testubuntu@ubuntu-virtual -machine:~/work/shell/file$ cat soft_test cat: soft_test: without that file or directory ubuntu@ubuntu-virtual-machine:~/work/shell/file$ cat hard_test 123456789``you can see that the soft connection is invalid But the hard link is not affected. Linux has a special instruction readlink, which is to get the address of the soft link. For example, in the example above, you can get the address of the source file that the soft link points to, ubuntu@ubuntu-virtual-machine:~/work/shell/file$ readlink soft_test test.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report