Learn by reading through in order

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.

> overwrites, >> appends
echo A > out.txtout.txt contents: Aecho B > out.txtout.txt: B (A is gone)echo C >> out.txtout.txt: B / C
> clears the contents and overwrites; >> appends to the end.
SymbolMeaning
>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

① Write the output of echo 'first' to note.txt using >.

② Append a line second to the same note.txt with >>, and confirm there are 2 lines with cat note.txt.

③ Finally write reset to note.txt with >, and confirm with cat note.txt that the contents change to just reset. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

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.

< is input, 1> and 2> route output
wc -l < data.txtfeed data.txtas inputls f 1> out.txtstandard output (1)only → out.txtcat miss 2> err.txtstandard error (2)only → err.txt
< 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

① Create a 3-line file with printf 'x\ny\nz\n' > nums.txt.

② Feed nums.txt as standard input to wc with its line-count option using <, and confirm the line count is shown.

③ Try to open a non-existent file with cat, and route that error message to err.txt with 2>.

④ Confirm with cat err.txt that the error message was saved to the file.

Linux console
0 / 4 completed
Loading Linux Terminal...

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

① Start a record by writing one line hit to access.log with echo output via >.

② Append hit two more times to the same access.log with >>, and confirm there are 3 lines with cat access.log.

③ Pass access.log to wc with its line-count option via <, and show how many records have piled up.

Linux console
0 / 4 completed
Loading Linux Terminal...

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

FormMeaning
> out 2> errRoute standard output to out and errors to err separately
2>&1Send standard error (2) to the same destination as standard output (1)
> all 2>&1Write 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

① Prepare one real file with touch real.txt.

② Run ls real.txt missing.txt > out.txt 2> err.txt to route standard output to out.txt and errors to err.txt separately.

③ Check each one's contents with cat out.txt and cat err.txt.

④ Next run ls real.txt missing.txt > all.txt 2>&1 and confirm with cat all.txt that standard output and errors are merged into the same file.

Linux console
0 / 6 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which symbol adds a new line to the end while keeping the existing file contents?

Q2What does < do for a command?

Q3What happens when you run cat missing.txt 2> err.txt?