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.txt

When the commands above are run in order, which of the following is the result of the last two cat commands?

  1. ABoth h.txt and s.txt show hello
  2. Bh.txt gives an error that the file is not found, and s.txt shows hello
  3. 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.
Practice this topic Back to topic

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Linux and Command-Line Basics questions

  1. A directory contains six files: a1.txt, a2.txt, a10.txt, b1.txt, c1.txt and a1.csv. Which of the following is the number of files shown when "ls [ab]?.txt" is run in this directory?
  2. After "chmod 754 run.sh" is run on the file run.sh, which of the following is the permission notation shown at the start of the line from "ls -l run.sh"?
  3. touch creates a new file from 666, and mkdir a new directory from 777, with the umask bits removed. Which of the following is the correct pair of permissions when a new file and a new directory are created in a shell whose umask is 027?
  4. The directory d has permissions rwx--x--x (711), and the file f.txt in it has rw-r--r-- (644). A user who is neither the owner nor the superuser runs "ls d" and "cat d/f.txt". Which of the following is the result?
  5. Which of the following correctly describes the sudo command on Linux?