Linux Command Line · Lesson 14 of 43
Redirecting input and output
Send command output to files with > and >>, feed files into commands with <, separate errors with 2>, and discard output with /dev/null.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will save the output of any command to a file, add to files without erasing them, keep errors apart from results, and silence noisy commands. Redirection is what turns one-off commands into reusable tools, and it is the foundation for pipes in the next lesson.
Three streams
Every program you run has three standard channels, each with a number called a file descriptor:
| Number | Name | Default destination |
|---|---|---|
| 0 | standard input (stdin) | your keyboard |
| 1 | standard output (stdout) | your screen |
| 2 | standard error (stderr) | your screen |
Normal results go to stdout, and error messages go to stderr. Both appear on the screen, which hides the fact that they are separate. Redirection lets you send each stream somewhere else.
> : write output to a file
The greater-than sign sends standard output into a file. If the file exists, it is replaced; if not, it is created:
cd ~/project
echo "first line" > out.txt
echo "second line" > out.txt
cat out.txtsecond lineThe second command replaced the first line. This is a classic way to lose data, so look twice before you write > over a file that matters. Setting set -o noclobber in Bash makes > refuse to overwrite existing files.
>> : append
Two signs add to the end of the file instead:
echo "first line" > log.txt
echo "second line" >> log.txt
echo "third line" >> log.txt
cat log.txtfirst line
second line
third lineRedirection works with any command. This saves a directory listing into a file:
ls docs > listing.txt
cat listing.txtnotes.txt
tasks.txt< : read input from a file
A less-than sign makes a file the standard input of a command. Many commands read from a file name or from standard input, and the result differs slightly. wc -l shows the file name when given one, and only the number when reading input:
wc -l docs/notes.txt
wc -l < docs/notes.txt5 docs/notes.txt
52> : separate the errors
Errors go to standard error, so a plain > does not capture them:
ls docs missing-folder > result.txtls: cannot access 'missing-folder': No such file or directoryThe error still appeared on screen, while the listing of docs went into the file. Use 2> to capture errors in a file of their own:
ls docs missing-folder > result.txt 2> errors.txt
cat result.txt
cat errors.txtdocs:
notes.txt
tasks.txt
ls: cannot access 'missing-folder': No such file or directoryJoining and discarding streams
To send both streams to the same file, redirect output first and then point stderr at the same place with 2>&1. In Bash, &> is a shorthand:
ls docs missing-folder > both.txt 2>&1
cat both.txtls: cannot access 'missing-folder': No such file or directory
docs:
notes.txt
tasks.txtOrder matters: 2>&1 > file sends errors to the screen, because the copy is made before the file is attached. Write > file 2>&1.
The special file /dev/null accepts everything and keeps nothing, so it is the way to silence a command:
ls docs missing-folder 2> /dev/null
echo "exit status: $?"docs:
notes.txt
tasks.txt
exit status: 2The exit status, held in $?, is 2 here because ls failed for one of its arguments. Hiding errors is useful when you expect them, but it also hides real problems, so use it with care.
Here documents
A here document feeds several lines to a command, ending at a marker word of your choice. It is how scripts embed text:
cat > message.txt <<EOF
Hello,
this text came from a here document.
Your home is $HOME.
EOF
cat message.txtHello,
this text came from a here document.
Your home is /home/learner.Quote the marker (<<'EOF') to stop the shell from expanding variables inside the text.
Common mistakes
Overwriting with >. Use >> when you mean to add, and be careful with > on important files.
Reading and writing the same file. sort file > file empties the file before sort reads it. Write to a new file, then move it back.
Expecting > to capture errors. Use 2> or > file 2>&1.
Putting 2>&1 before the redirect. The order decides where errors go.
Exercise
Create summary.txt containing three of your favourite commands, one per line, using one > and two >> redirections. Then run ls docs nothing-here so that the listing goes to ok.txt and the error goes to bad.txt, and read both. Finally, count the lines of summary.txt using < so that wc prints only the number.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.