Linux and Command-Line Basics

$ echo a > f.txt
$ echo b > f.txt
$ echo c >> f.txt
$ cat f.txt

When the commands above are run in order, which of the following is the content shown by the last cat?

  1. AOnly the one line c
  2. BThe two lines b and c
  3. CThe three lines a, b and c
Show answer and explanation

Answer: B. The two lines b and c

">" is a redirection that writes output to a file; if the file already exists, it is emptied before writing (overwritten). ">>" appends to the end of the file. The first line writes a, the ">" in the second line leaves only b, and the ">>" in the third line adds c after it, so f.txt contains the two lines b and c. To avoid wiping out an important file with >, bash can forbid overwriting with set -o noclobber.

  • A. ">>" appends rather than overwrites, so the b written in the second line remains.
  • B. Correct. > leaves only b, and >> appends c.
  • C. The ">" in the second line empties the file before writing, so the a from the first line does not remain.
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. As shown above, in a directory where a.txt exists and nofile does not, the last command is run. Which of the following is the result?
  2. In the session above, which of the following is the value printed by the last command?
  3. In the session above, which of the following are the lines printed by the last grep?
  4. The choices omit the leading spaces of the line. In the session above, which of the following is the line printed by the last pipeline?
  5. You want to keep showing lines on the screen as they are appended later to the server log file /var/log/app.log. Which of the following commands is appropriate?