Redirecting Output in Linux
This post explains how output redirection works in Linux, covering the standard streams stdout and stderr, the redirection operators >, >>, 2>, and 2>&1, and how to use /dev/null. Practical examples show how to save, append, and discard command output in everyday workflows.
When you run a command in Linux, the output typically appears directly in your terminal. That's useful for quick checks, but what if you want to save that output to a file, or pass it to another command for further processing? That's where output redirection comes in, and it's one of the most powerful everyday skills you'll use as a Linux user.
Understanding Standard Streams
Before diving into redirection, it helps to understand how Linux thinks about input and output. Every process has three standard streams:
- stdin (0): Standard input, where a program reads data from
- stdout (1): Standard output, where normal output is sent
- stderr (2): Standard error, where error messages are sent
By default, both stdout and stderr are connected to your terminal. Redirection lets you change that destination to a file, another command, or even nowhere at all.
Redirecting Standard Output with >
The most common form of redirection uses the > operator to send command output to a file instead of the terminal.
ls -la > filelist.txtThis runs ls -la and saves everything it would have printed to your screen into a file called filelist.txt. If that file already exists, it gets overwritten. If it doesn't exist, Linux creates it for you.
You can verify it worked by reading the file back:
cat filelist.txtAppending Output with >>
If you want to add output to a file without erasing what's already there, use the double arrow >> operator:
echo "Backup started: $(date)" >> backup.log
echo "Backup complete: $(date)" >> backup.logEach time you run a command with >>, the new output is added to the end of the file. This is especially handy for logging or building up a report over time.
Redirecting Standard Error with 2>
Sometimes a command produces errors alongside normal output. Error messages come from stderr, which is stream number 2. To capture errors separately, use 2>:
ls /nonexistent-directory 2> errors.txtThe error message gets saved to errors.txt instead of cluttering your terminal. This is incredibly useful in scripts where you want to log errors without stopping the script's normal output.
Combining stdout and stderr
You can redirect both streams to the same file using 2>&1, which means "send stderr to wherever stdout is going":
ls /real-dir /fake-dir > output.log 2>&1Or, on modern systems, you can use the shorthand &>:
ls /real-dir /fake-dir &> output.logBoth approaches capture all output, errors included, into one file. This is the format you'll see most often in shell scripts.
Sending Output to /dev/null
Sometimes you want to run a command and deliberately throw away the output. Linux provides a special device called /dev/null for exactly that purpose. Anything redirected there is silently discarded:
ping -c 4 google.com > /dev/null 2>&1This runs the ping but shows you nothing at all. It's useful in scripts when you only care whether a command succeeded or failed, not what it printed.
Piping vs. Redirecting
One thing beginners sometimes confuse: the pipe operator | is related but different. Pipes send the output of one command directly into the input of another command, without writing to a file:
ps aux | grep nginxRedirection sends output to a file. Piping sends output to another command. Both are essential skills, and you'll often use them together in the same one-liner.
What's Next
Now that you understand how to redirect output, you're ready to take the next step: working with pipes and filters to chain commands together. In the next post, we'll look at tools like grep, sort, cut, and awk that become dramatically more powerful when combined with pipes. These are the building blocks of real shell scripting.