Skip to lesson

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.

Beginner to intermediate·13 min·Text, redirection and pipes·Updated 2026-11-24

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:

bash
cd ~/project
echo "first line" > out.txt
echo "second line" > out.txt
cat out.txt
result
second line

The 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:

bash
echo "first line" > log.txt
echo "second line" >> log.txt
echo "third line" >> log.txt
cat log.txt
result
first line
second line
third line

Redirection works with any command. This saves a directory listing into a file:

bash
ls docs > listing.txt
cat listing.txt
result
notes.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:

bash
wc -l docs/notes.txt
wc -l < docs/notes.txt
result
5 docs/notes.txt
5

2> : separate the errors

Errors go to standard error, so a plain > does not capture them:

bash
ls docs missing-folder > result.txt
result
ls: cannot access 'missing-folder': No such file or directory

The 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:

bash
ls docs missing-folder > result.txt 2> errors.txt
cat result.txt
cat errors.txt
result
docs:
notes.txt
tasks.txt
ls: cannot access 'missing-folder': No such file or directory

Joining 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:

bash
ls docs missing-folder > both.txt 2>&1
cat both.txt
result
ls: cannot access 'missing-folder': No such file or directory
docs:
notes.txt
tasks.txt

Order 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:

bash
ls docs missing-folder 2> /dev/null
echo "exit status: $?"
result
docs:
notes.txt
tasks.txt
exit status: 2

The 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:

bash
cat > message.txt <<EOF
Hello,
this text came from a here document.
Your home is $HOME.
EOF
cat message.txt
result
Hello,
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.

0 / 4 understood
01 What is the difference between > and >> ?
02 What does ls /missing 2> errors.txt put into errors.txt?
03 What does command > /dev/null 2>&1 do?
04 How do you give a file to a command as its input?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses