The difference between hard links and soft links

摘自:
what-is-the-difference-between-a-hard-link-and-a-symbolic-link

A picture is worth a thousand words.

One example wins a hundred pictures.
Create two new files:

touch blah1
touch blah2

add something to it

echo "Cat"  > blah1
echo "Dog" > blah2

Check out what's been added:

cat blah1; cat blah2
Cat
Dog

Create hard and soft links

ln blah1 blah1-hard
ln -s blah2 blah2-soft

Check out what we've created

ls -l 
blah1
blah1-hard
blah2
blah2-soft -> blah2

Changing the name of blah1 has no effect

mv blah1 blah1-new
cat blah1-hard
Cat

blah1-hard points to the inode, so the contents of the file have not changed.

mv blah2 blah2-new
ls blah-soft
blah2-soft
cat blah2-soft
cat: blah-soft: No such file or directory

The content of the file cannot be found, because the soft link is the name pointed to, and the name has been modified, so the corresponding file cannot be found.
So:
blah1 is removed, but blah1-hard still has the content.
blah2 is removed, but blah2-soft is a link to a file that doesn't exist.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325232725&siteId=291194637
Recommended