Linux and Command-Line Basics
$ echo hello > a.txt
$ ln a.txt h.txt
$ ln -s a.txt s.txt
$ rm a.txt
$ cat h.txt
$ cat s.txtWhen the commands above are run in order, which of the following is the result of the last two cat commands?
- ABoth h.txt and s.txt show hello
- Bh.txt gives an error that the file is not found, and s.txt shows hello
- Ch.txt shows hello, and s.txt gives an error that the file is not found
Show answer and explanation
Answer: C. h.txt shows hello, and s.txt gives an error that the file is not found
A hard link made with ln is another name that points to the same inode (the numbered record that manages a file's contents and attributes) as the original file. rm a.txt removes only the name a.txt; because the name h.txt remains, the contents are not deleted and cat h.txt shows hello. A symbolic link made with ln -s is a separate file whose contents are the path "a.txt", so once a.txt is gone there is nothing to follow and cat s.txt gives a "No such file or directory" error. A symbolic link whose target has disappeared stays behind as a broken (dangling) link.
- A. A symbolic link opens the file by following its path, so once a.txt is gone it cannot be read through s.txt.
- B. The behavior of hard links and symbolic links is reversed. As long as one name remains, the contents can be read through a hard link.
- C. Correct. A hard link is a name for the same inode and survives, while the symbolic link becomes broken.