Skip to content

Redirection

Module
Input, Output and Pipes
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
Prerequisites
On this page

You save a note with >, then use the same operator to add another line. The first line disappears. The second command did exactly what you typed, but you needed a different way to connect its output.

By the end, you can choose between replacing and extending a file, read stdin from a file, and keep diagnostics separate. You can also explain why swapping two redirections changes their result.

Finish Standard streams (B09.1). Work only in this disposable directory:

Terminal window
mkdir -p ~/lab/b09/redirection
cd ~/lab/b09/redirection

Start with an empty directory. Use Reset this lesson before repeating the examples. None of these commands needs administrative privileges.

Terminal window
printf 'first\n' > out.txt

The command prints nothing in the terminal. Bash connects stdout, descriptor 1, to out.txt. This is redirection: changing where a standard connection leads. If the file does not exist, Bash creates it. The Contents panel below shows the saved line first.

B02.4 used > to save snapshot-test text instead of displaying it. B04.1 used it in the lab setup. The same rule applies here: Bash opens the destination before the command runs. The command never receives > or its destination filename as arguments.

You can check that separation using B08.2’s bracket format:

Terminal window
printf '[%s]\n' 'one argument' > args.txt
cat args.txt
Captured output
[one argument]

There is one printed argument after the format, rather than extra lines for > and args.txt. The quotes group the argument; the redirection independently connects stdout. This also explains the shell syntax around B07.5’s printf setup: the format creates characters, while Bash chooses their destination.

Predict first: after this command, will out.txt contain both first and replacement?

Terminal window
printf 'replacement\n' > out.txt
Run it, then compare with your guess

Only the replacement remains. Opening the existing file with > truncated it to zero length before printf wrote its line. A command that later fails can therefore still leave a destination empty.

Use >> to append, meaning write at the end while preserving existing contents:

Terminal window
printf 'added\n' >> out.txt

>> also creates the destination if it does not exist. Neither operator adds a newline itself: printf supplies \n here. Without it, an appended string can continue the last line.

Write, replace, then append
learner@rootvolt:~/lab$ cd ~/lab/b09/redirectionlearner@rootvolt:~/lab/b09/redirection$ printf 'first\n' > out.txtlearner@rootvolt:~/lab/b09/redirection$ printf 'replacement\n' > out.txtlearner@rootvolt:~/lab/b09/redirection$ printf 'added\n' >> out.txtlearner@rootvolt:~/lab/b09/redirection$ 

Contents text of the file

out.txt~/lab/b09/redirection/changed
  1. replacement
  2. addedadded

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The first write creates out.txt; the second replaces its text; the third appends while preserving replacement.
Steps (8)
  1. Type cd ~/lab/b09/redirection
  2. Press Enter
  3. Type printf 'first\n' > out.txt
  4. Press Enter
  5. Type printf 'replacement\n' > out.txt
  6. Press Enter
  7. Type printf 'added\n' >> out.txt
  8. Press Enter

Watch the Contents panel after each write. Each replacing write marks its new line added; the append marks only added, leaving replacement unmarked. The following table is a text equivalent of those file states for screen-reader users and readers following without motion.

Command File contents after it
printf 'first\n' > out.txt first, ending with a newline
printf 'replacement\n' > out.txt replacement, ending with a newline
printf 'added\n' >> out.txt replacement, then added, each ending with a newline

B06.4 used >> through a linked name. B07.1, B07.2 and the B07 practice used it to extend logs. It preserves the existing file’s text; it does not create a separate backup of that text.

Prepare a short result:

Terminal window
printf 'ordinary result\n' > note.txt

Point descriptor 0 at that file with <:

Terminal window
cat < note.txt
Captured output
ordinary result

Bash opens note.txt for input. cat has no file argument, so it reads stdin, reaches the file’s end, and finishes. You do not press Ctrl+D because the file already has an end.

cat note.txt prints the same contents, but cat opens that named argument itself. With cat < note.txt, Bash opens the file and supplies stdin instead. < reads the file; it does not empty it.

cat can read several file arguments. Give it one existing file and one missing file, with separate destinations:

Terminal window
cat note.txt missing.txt > out.txt 2> errors.txt

No text appears in the terminal. > connects stdout to out.txt; 2> connects stderr to errors.txt. The 2 must touch > to select descriptor 2. The space after 2> is optional; this course writes one so the filename stands apart.

Terminal window
cat out.txt
Captured output
ordinary result
Terminal window
cat errors.txt
Captured output
cat: missing.txt: No such file or directory
Keep the diagnostic separate
learner@rootvolt:~/lab/b09/redirection$ cat note.txt missing.txt > out.txt 2> errors.txtlearner@rootvolt:~/lab/b09/redirection$ cat out.txtordinary resultlearner@rootvolt:~/lab/b09/redirection$ cat errors.txtcat: missing.txt: No such file or directorylearner@rootvolt:~/lab/b09/redirection$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Ordinary text goes into out.txt; the missing-file diagnostic goes into errors.txt.
Steps (8)
  1. Type cat note.txt missing.txt > out.txt 2> errors.txt
  2. Press Enter
  3. Type cat out.txt
  4. Press Enter
  5. Output: ordinary result
  6. Type cat errors.txt
  7. Press Enter
  8. Output: cat: missing.txt: No such file or directory

The diagnostic is displayed only when you read its file. At that point cat errors.txt copies the saved message through stdout. A line’s words do not permanently belong to one stream; the writing command and its connections determine the stream.

2>&1 makes descriptor 2 use descriptor 1’s destination at that point. It does not mean “keep following any later change to descriptor 1.” The ampersand belongs to this duplication syntax.

Predict first: will these two orders put the diagnostic in the same place?

Terminal window
cat note.txt missing.txt > out.txt 2>&1
Run it, then compare with your guess

The command displays nothing. First stdout goes to out.txt; then stderr takes that destination too. Read the combined file:

Terminal window
cat out.txt
Captured output
ordinary result
cat: missing.txt: No such file or directory

Reverse the redirections:

Terminal window
cat note.txt missing.txt 2>&1 > out.txt
Captured output in the terminal
cat: missing.txt: No such file or directory

Stderr first takes stdout’s current destination, the terminal. Then stdout moves to the file; stderr remains connected to the terminal. Inspect the file:

Terminal window
cat out.txt
Captured output
ordinary result
Redirections are applied left to right
learner@rootvolt:~/lab$ cd ~/lab/b09/redirectionlearner@rootvolt:~/lab/b09/redirection$ cat note.txt missing.txt > out.txt 2>&1learner@rootvolt:~/lab/b09/redirection$ cat out.txtordinary result
cat: missing.txt: No such file or directorylearner@rootvolt:~/lab/b09/redirection$ cat note.txt missing.txt 2>&1 > out.txtcat: missing.txt: No such file or directorylearner@rootvolt:~/lab/b09/redirection$ cat out.txtordinary resultlearner@rootvolt:~/lab/b09/redirection$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Redirecting stdout before duplicating it combines the streams; duplicating first leaves stderr at the terminal.
Steps (13)
  1. Type cd ~/lab/b09/redirection
  2. Press Enter
  3. Type cat note.txt missing.txt > out.txt 2>&1
  4. Press Enter
  5. Type cat out.txt
  6. Press Enter
  7. Output: ordinary result cat: missing.txt: No such file or directory
  8. Type cat note.txt missing.txt 2>&1 > out.txt
  9. Press Enter
  10. Output: cat: missing.txt: No such file or directory
  11. Type cat out.txt
  12. Press Enter
  13. Output: ordinary result

The replay’s terminal displays both streams identically. Their saved destinations, rather than colors, establish the difference. This example reads the valid file before trying the missing one; other programs can produce their output in a different order.

Using > to add a line. It replaces the existing text. Use >> when preserving earlier contents is part of the task.

Separating 2 from >. In 2>, the number selects stderr. With a space between them, 2 becomes a command argument and > still redirects stdout.

Reading a file into itself. Do not use the same file as a command’s input and its > destination. Bash empties the destination before the command starts, so the command finds nothing left to read. Use distinct input and output files.

Goal: Create checklist.txt containing Pack cable, append Pack adapter without losing the first line, and read both lines through stdin. Do not pass the filename as an argument to the final cat.

Hint

Choose a replacing write for the initial file, an append for the addition, and an input redirection for the read.

Solution
Terminal window
printf 'Pack cable\n' > checklist.txt
printf 'Pack adapter\n' >> checklist.txt
cat < checklist.txt
Captured output
Pack cable
Pack adapter

The file supplies stdin. The first two commands’ stdout went to the file; the final command’s stdout reaches the terminal.

Terminal window
cd ~/lab/b09
ls -AR redirection
Terminal window
rm -r ~/lab/b09/redirection

Rerun Before you start. If the directory is already absent, skip removal.

  • Bash handles redirection before running the command; the operator and destination are not command arguments.
  • > replaces stdout’s destination file contents; >> appends.
  • < supplies stdin from a file; 2> saves stderr separately.
  • Redirections apply left to right. 2>&1 duplicates stdout’s destination at that point.

Next, Pipelines and tee connects one command’s output directly to another command’s input.

  • On your system: man bash, section REDIRECTION, and man cat.
  • GNU Bash: Redirections, including output redirection, appending, and duplicating file descriptors.
  • Ubuntu 24.04: bash(1) and cat(1).