Unix: Why soft link file still pointing to original file when original file is deleted?
Solution 1:
Why is file1_soft1 file is still linked to file1 file when file1 file does not exist anymore?
Windows file systems do not monitor links to see if the file linked to has been deleted.
Doing so would add some considerable overhead:
Maintaining a list of all links
Periodically checking the list to see if the file linked to has been deleted.
Windows:
- The Windows commands used to create file soft links (mklink and shortcut) can both create links to non-existent targets.
Unix:
-
The Unix command to create soft links (ln -s) can also create links to non-existent targets.
$ ll total 0 $ ln -s target dummy $ ll total 1 lrwxrwxrwx 1 DavidPostill None 6 Jan 21 16:19 dummy -> target $ cat dummy cat: dummy: No such file or directory $
If you delete a file for which a symbolic link still exists, the
rm
will succeed but the symbolic link would remain and any attempt to reference it will return a 'file not found' error.
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- mklink - Create a symbolic link to a directory or a file, or create a hard file link or directory junction.
shortcut - Create a windows shortcut (.LNK file).
An A-Z Index of the Bash command line for Linux - An excellent reference for all things Bash command line related.
- ln - Make links between files, by default, it makes hard links; with the -s option, it makes symbolic (or "soft") links.
Solution 2:
This is because soft link points to the path, not to the file itself.
Your assumption that the link is still pointing to the original file is wrong. It's pointing to the original path.
It doesn't matter what is there at this path nor if anything at all. If you create new file file1
(or maybe even a directory with that name) the link will point to it.