Redirection
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.
Before you start
Section titled “Before you start”Finish Standard streams (B09.1). Work only in this disposable directory:
mkdir -p ~/lab/b09/redirectioncd ~/lab/b09/redirectionStart with an empty directory. Use Reset this lesson before repeating the examples. None of these commands needs administrative privileges.
Save stdout instead of displaying it
Section titled “Save stdout instead of displaying it”printf 'first\n' > out.txtThe 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:
printf '[%s]\n' 'one argument' > args.txtcat args.txt[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.
Choose replacement or append
Section titled “Choose replacement or append”Predict first: after this command, will out.txt contain both first and replacement?
printf 'replacement\n' > out.txtRun 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:
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.
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
replacementaddedadded
Steps (8)
- Type
cd ~/lab/b09/redirection - Press Enter
- Type
printf 'first\n' > out.txt - Press Enter
- Type
printf 'replacement\n' > out.txt - Press Enter
- Type
printf 'added\n' >> out.txt - 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.
Supply stdin from a file
Section titled “Supply stdin from a file”Prepare a short result:
printf 'ordinary result\n' > note.txtPoint descriptor 0 at that file with <:
cat < note.txtordinary resultBash 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.
Send diagnostics to a different file
Section titled “Send diagnostics to a different file”cat can read several file arguments. Give it one existing file and one missing file, with separate destinations:
cat note.txt missing.txt > out.txt 2> errors.txtNo 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.
cat out.txtordinary resultcat errors.txtcat: missing.txt: No such file or directorylearner@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$ Steps (8)
- Type
cat note.txt missing.txt > out.txt 2> errors.txt - Press Enter
- Type
cat out.txt - Press Enter
- Output:
ordinary result - Type
cat errors.txt - Press Enter
- 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.
Apply redirections from left to right
Section titled “Apply redirections from left to right”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?
cat note.txt missing.txt > out.txt 2>&1Run 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:
cat out.txtordinary resultcat: missing.txt: No such file or directoryReverse the redirections:
cat note.txt missing.txt 2>&1 > out.txtcat: missing.txt: No such file or directoryStderr first takes stdout’s current destination, the terminal. Then stdout moves to the file; stderr remains connected to the terminal. Inspect the file:
cat out.txtordinary resultlearner@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$ Steps (13)
- Type
cd ~/lab/b09/redirection - Press Enter
- Type
cat note.txt missing.txt > out.txt 2>&1 - Press Enter
- Type
cat out.txt - Press Enter
- Output:
ordinary result cat: missing.txt: No such file or directory - Type
cat note.txt missing.txt 2>&1 > out.txt - Press Enter
- Output:
cat: missing.txt: No such file or directory - Type
cat out.txt - Press Enter
- 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.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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
printf 'Pack cable\n' > checklist.txtprintf 'Pack adapter\n' >> checklist.txtcat < checklist.txtPack cablePack adapterThe file supplies stdin. The first two commands’ stdout went to the file; the final command’s stdout reaches the terminal.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b09ls -AR redirectionrm -r ~/lab/b09/redirectionRerun 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>&1duplicates stdout’s destination at that point.
Next, Pipelines and tee connects one command’s output directly to another command’s input.
Further reading
Section titled “Further reading”- On your system:
man bash, section REDIRECTION, andman cat. - GNU Bash: Redirections, including output redirection, appending, and duplicating file descriptors.
- Ubuntu 24.04: bash(1) and cat(1).