Q1Which symbol adds a new line to the end while keeping the existing file contents?
Redirection — > >> < 2>
Overwrite a file with >, append with >>, feed standard input with <, and send only error output to a separate file with 2> — hands-on in a browser terminal.
Sending Output to a File — > and >>
A command's results normally print straight to the screen, but redirection switches that output to a file.
Write command > file and what would have gone to the screen is written into the file instead.
> empties the file before writing (overwrite).
When you want to keep what's already there and add to the end, use >> (append).
Mixing them up can erase data you need, so let's keep the difference clear.
> clears the contents and overwrites; >> appends to the end.| Symbol | Meaning |
|---|---|
> | Write standard output to a file (empty it first, overwrite) |
>> | Append standard output to the end of a file |
< | Feed a file's contents as standard input |
1> | Write only standard output to a file (same as >) |
2> | Write only standard error to a separate file |
echo 'line 1' > out.txt # overwrite (create new)
echo 'line 2' >> out.txt # append
cat out.txt # line 1 and line 2
echo 'reset' > out.txt # overwrite (previous content is gone)
cat out.txt # just reset
Input and Error Routing — < and 2>
< is the opposite of output: it feeds a file's contents as the command's standard input.
Write command < file and, instead of typing on the keyboard, the file's contents are streamed into the command.
Unlike wc -l nums.txt, with < nums.txt the filename isn't added to the output — only the contents are fed in.
A command's output comes in two kinds: the normal results (standard output) and error messages (standard error).
Standard output is numbered 1 and standard error is numbered 2; you route standard output with 1> (same as >) and standard error with 2>.
With 2> you can send just the errors to a separate file, which is handy when you want to record results and errors separately.
< feeds a file as input; 1> sends only standard output and 2> only standard error to a separate file.printf 'x\ny\nz\n' > nums.txt # a 3-line material
wc -l < nums.txt # feed nums.txt as input → 3
cat missing.txt 2> err.txt # route the error to err.txt
cat err.txt # check the error message
> clears the file on the spot
> empties the file's contents before the command runs.
When you want to add lines to existing content, always use >> (append).
Likewise, 2> overwrites the target file with error output.
Combine Them — Build a Log and Count It
In real work, you'll often start a record with >, keep adding lines with >>, and finally check the count with wc.
Let's combine the symbols so far to practice simple log creation and counting.
Combining Multiple Redirections — > 2> 2>&1
You can specify several redirections on a single command at once.
To split standard output and error output into separate files, line them up like > destination 2> error-destination.
To merge both into one file instead, use 2>&1 to send standard error (2) to the same destination as standard output (1).
2>&1 goes after you've set the destination with >.
| Form | Meaning |
|---|---|
> out 2> err | Route standard output to out and errors to err separately |
2>&1 | Send standard error (2) to the same destination as standard output (1) |
> all 2>&1 | Write both standard output and errors together to all |
touch real.txt # prepare a real file
ls real.txt missing.txt > out.txt 2> err.txt # output and errors separately
cat out.txt # real.txt (standard output)
cat err.txt # error for missing.txt
ls real.txt missing.txt > all.txt 2>&1 # both into one file
cat all.txt # both output and errors
Knowledge Check
Answer each question one by one.
Q2What does < do for a command?
Q3What happens when you run cat missing.txt 2> err.txt?