转载:Linux: What’s the difference between a soft link and a hard link?

Link:https://www.moreofless.co.uk/linux-difference-soft-symbolic-link-and-hard-link/

This example shows the difference between a soft (also known as a symbolic link) and a hard link on a Linux system:

echo "hello" > a  ---> create a file called "a"
ln a b            ---> create hard link called "b" to "a"
ln -s a c         ---> create soft link called "c" to "a"

No surprises here, printing the contents of files a, b and c produce the same result:

cat a ---> hello
cat b ---> hello
cat c ---> hello

Now we remove the original “a” file:

rm a

And the difference between the two links become obvious:

cat a ---> No such file or directory
cat b ---> hello
cat c ---> No such file or directory

The soft link is essentially a pointer to the original file and when the original file is deleted the soft link does not point to anything and so “no such file or directory” is reported. The hard link acts more like a mirror of the original file, it actually points to the same “node” in the filesystem that the original “a” file points to, so when we delete the original file “a” the file “c” still points to the same (and still existing) node in the filesystem.

posted @ 2019-12-24 13:11  逆火狂飙  阅读(180)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东