Read

Module 3 · Text, redirection and pipes

Redirecting output: > and >>

Send what a command prints into a file instead of the screen, and learn the one-character difference between overwriting and appending.

What you will learn

  • Understand that every command writes to standard output, which normally is the terminal.
  • Use > to write output to a file and >> to append to it.
  • Avoid the classic mistakes: clobbering a file and redirecting a file onto itself.

When ls prints a listing it is not writing to the screen on purpose. It writes to a channel called standard output (stdout), and the shell connected that channel to your terminal before the command started. Redirection is the act of connecting it somewhere else. The command does not know or care: ls behaves identically whether its output ends up on screen or in a file.

> creates or overwrites

command > file runs the command with stdout pointing at file. If the file does not exist it is created; if it exists its content is replaced, with no confirmation. The shell does this before running the command, which has two consequences: the file is created even if the command prints nothing or fails, and if you redirect ls into a file inside the directory being listed, the new file shows up in the listing.

>> appends

command >> file also creates the file if needed, but when it exists the output is added after the current content. This is what you want for logs, notes you build up over time, or collecting results from several commands into one place. The difference between > and >> is a single character and it is the difference between keeping and losing data, so make a habit of pausing before you press Enter on a >.

~% mkdir demo; cd demo
demo% echo first > notes.txt
demo% cat notes.txt
first
demo% echo second >> notes.txt
demo% cat notes.txt
first
second
demo% echo third > notes.txt
demo% cat notes.txt
third
demo% ls > listing.txt
demo% cat listing.txt
listing.txt
notes.txt

Useful habits

  • > file on its own, with no command, creates an empty file or truncates an existing one to zero bytes. It is a quick way to clear a log.
  • The shell's noclobber option (set -C) makes > refuse to overwrite existing files; >| forces it. It works in this VM's ash shell too.
  • Spaces around > are optional: echo hi >f, echo hi > f and echo hi>f are the same.
  • Redirection only captures standard output. Error messages still reach the screen; the next lesson deals with them.
OperatorFile missingFile exists
>createdcontent replaced
>>createdoutput added at the end
>|createdreplaced even with noclobber on

Commands in this lesson

CommandWhat it does
command > fileWrite stdout to file, replacing its content.
command >> fileAppend stdout to the end of file.
> fileCreate an empty file or truncate it to zero bytes.
set -CTurn on noclobber: > will not overwrite existing files.
command >| fileOverwrite even when noclobber is on.

Quiz

  1. `notes.txt` has three lines. After `echo hi > notes.txt`, how many lines does it have?

    • Four
    • One
    • Three; echo fails because the file exists
  2. Which command adds a line to the end of log.txt without touching what is already there?

    • `echo done > log.txt`
    • `echo done >> log.txt`
    • `echo done >| log.txt`
  3. What is wrong with `sort data.txt > data.txt`?

    • Nothing; the file ends up sorted
    • The shell truncates data.txt before sort reads it, so the result is an empty file
    • sort refuses to read and write the same file
  4. What does a bare `> empty.txt` do?

    • Prints an error: no command given
    • Creates empty.txt (or empties it) with zero bytes
    • Shows the content of empty.txt

Practice

  1. In /root/lab/l22 there are a few files. Save the listing of that directory (the output of ls) into /root/lab/l22/listing.txt.

  2. /root/lab/l22/log.txt contains the lines `line 1` and `line 2`. Append a third line, `line 3`, keeping the first two.

  3. /root/lab/l22/notes.txt has old content. Replace all of it so that the file contains only the line `fresh start`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.