VMware ESXi and vSphere Cluster Management
Linux Input and Output Redirection
Learn how Bash redirects stdin, stdout, and stderr using operators such as >, >>, <, 2>, 2>&1, pipes, and /dev/null.
Linux commands communicate through data streams, not only by displaying text in a terminal. A command can read data from one source and send normal results or diagnostic messages to different destinations.
Redirection is shell syntax that changes where a command's input comes from or where its output goes. The shell processes redirections before it starts the command. The command generally does not need to know whether its input or output is connected to a terminal, a file, or another command.
These examples target a typical Linux shell such as Bash. Other shells may have small syntax differences.
The Three Standard Streams
A file descriptor is a numeric reference to an open input or output stream. Linux commands conventionally begin with three standard streams:
| Stream | Abbreviation | Descriptor number | Default destination or source | Typical content |
|---|---|---|---|---|
| Standard input | stdin | 0 | Terminal keyboard | Data supplied to a command |
| Standard output | stdout | 1 | Terminal | Normal command results |
| Standard error | stderr | 2 | Terminal | Error messages and diagnostics |
By default, all three streams are associated with the terminal. Standard output and standard error may look identical on screen, but they are separate streams and can be redirected independently.
Redirecting Standard Output to a File
The > operator sends standard output to a file:
ls > ls_output.txt
If ls_output.txt does not exist, the shell creates it. If it already exists, > replaces its contents. This is called overwriting.
The directory listing may no longer appear in the terminal because stdout is now going to the file. Inspect the saved result with cat:
cat ls_output.txt
Appending Standard Output
The >> operator sends stdout to the end of a file. It creates the file if necessary but preserves existing content. This is called appending.
date >> ls_output.txt
tail ls_output.txt
Each execution adds the current date after the existing lines, making >> useful for simple logs.
Redirecting Standard Input from a File
The < operator supplies a file as standard input:
cat < ls_output.txt
Here, cat reads from stdin, and the shell connects stdin to ls_output.txt. The command receives the file's contents without requiring you to type them manually. This pattern also works with programs that normally read input from the keyboard.
For example, a mail utility can receive the contents of a file as its message body:
mail bob < ls_output.txt
The exact behavior of mail depends on the installed utility and local mail configuration. If it is unavailable, demonstrate input redirection with another command that reads stdin, such as cat.
Redirecting Standard Error
Because stderr has descriptor number 2, place 2 before the redirection operator to redirect only diagnostic output:
command 2> errors.txt
This creates or overwrites errors.txt with stderr. To append errors instead, use 2>>:
command 2>> errors.txt
Separating normal results from failures is useful when a command produces valid data while also reporting inaccessible files or other problems.
find /etc /path-that-does-not-exist > results.txt 2> errors.txt
The command's normal output goes to results.txt, while its error messages go to errors.txt.
Combining Standard Output and Standard Error
To place both stdout and stderr in one file, redirect stdout first and then point stderr at stdout:
find /etc /path-that-does-not-exist > command.log 2>&1
The redirections are processed from left to right:
> command.logconnects stdout, descriptor 1, tocommand.log.2>&1connects stderr, descriptor 2, to the current destination of stdout.
The result is one log containing both streams. Bash also provides shorthand forms:
command &> command.log
command &>> command.log
&> combines and overwrites; &>> combines and appends. The more explicit > file 2>&1 form is widely recognized and makes the descriptor behavior clear.
Why Redirection Order Matters
These two commands are not equivalent:
command > file 2>&1
command 2>&1 > file
In the first command, both streams reach file. In the second, stderr is first connected to the terminal's current stdout, and only then stdout is redirected to file. As a result, stderr usually remains on the terminal.
Discarding Output with /dev/null
/dev/null is a special device file that discards data written to it. It is useful when output is expected and genuinely unneeded.
command > /dev/null
command 2> /dev/null
command > /dev/null 2>&1
The first command hides stdout, the second hides stderr, and the third hides both. For example:
find / -name example 2> /dev/null
This hides permission and traversal errors while searching. Use it carefully: discarding stderr can also hide important failures.
Redirection Operators at a Glance
| Syntax | Affected stream | Effect | Whether existing file content is retained | Example |
|---|---|---|---|---|
> file | stdout | Write to a file | No; overwrites | ls > list.txt |
>> file | stdout | Write after existing content | Yes; appends | date >> log.txt |
< file | stdin | Read input from a file | Not applicable | cat < list.txt |
2> file | stderr | Write errors to a file | No; overwrites | command 2> errors.txt |
2>> file | stderr | Append errors to a file | Yes; appends | command 2>> errors.txt |
> file 2>&1 | stdout and stderr | Send both streams to one file | No; overwrites | command > all.txt 2>&1 |
&> file | stdout and stderr | Bash shorthand for combined output | No; overwrites | command &> all.txt |
/dev/null | Any redirected stream | Discard data | Not applicable | command > /dev/null |
Redirection Versus Pipelines
File redirection connects a stream to a file. A pipe, written with |, connects one command's stdout directly to another command's stdin.
ls | sort
In this example, ls writes normal output to stdout, and the pipe supplies that data as stdin to sort. The sorted result is then written by sort to its stdout, which remains connected to the terminal.
| Technique | Syntax | Data destination | Common use case |
|---|---|---|---|
| Output redirection | command > file | A file | Save command results |
| Input redirection | command < file | Command stdin receives a file | Provide prepared input |
| Pipeline | producer | consumer | The next command's stdin | Filter or transform output immediately |
Safe Usage and Shell Behavior
File Replacement and Permissions
The shell opens redirection targets before the command runs. If the target cannot be created or written, the shell reports an error and the command may not run. Common causes include a read-only directory, an unwritable existing file, or a path that does not exist.
Choose a writable location or correct permissions when appropriate. Do not assume that placing sudo before a command also gives the shell permission to create a redirected file; the shell performs the redirection itself. For example, sudo command > /protected/output can still fail because your shell tries to open the file before sudo starts command.
Quoting Paths
Quote a destination path containing spaces so the shell treats it as one path:
command > "output file.txt"
Single quotes can also protect a path when it contains shell-special characters:
command > 'daily output.txt'
Placement of Redirections
In common shells, redirections can appear before or after command arguments. These forms are equivalent:
ls > listing.txt
> listing.txt ls
Keeping redirections at the end is often easier for beginners to read. Descriptor combinations should remain ordered deliberately, especially when using 2>&1.
Troubleshooting Redirection
- No normal results appear in the terminal: stdout may have been redirected. Use
cat,less, or another viewer on the destination file. - A file lost its previous contents:
>overwrote it. Use>>when existing content must be preserved. - Errors still appear on screen after
> output.txt: only stdout was redirected. Use2> errors.txtor> output.txt 2>&1. - Permission denied appears: the user cannot write the target file or directory. Select a writable path or use suitable privileges correctly.
- A combined log is incomplete: the redirections may be reversed. Use
> file 2>&1, not2>&1 > file. - A path with spaces is split into several arguments: quote it, for example
command > "output file.txt". - The mail example fails: the mail utility, its options, or local mail delivery may not be configured. Check that utility's documentation or use another stdin-reading command.
Exam-Relevant Summary
- stdin is descriptor
0, stdout is descriptor1, and stderr is descriptor2. >redirects stdout and overwrites a file;>>redirects stdout and appends.<supplies a file as stdin.2>and2>>redirect or append stderr.> file 2>&1sends stdout and stderr to the same destination because redirections are processed left to right./dev/nulldiscards redirected data.- A pipe,
|, connects one command's stdout to another command's stdin; it is not the same as saving output to a file.
Once these streams are understood, commands can be composed to save results, provide prepared input, separate diagnostics, create logs, or pass data directly between programs. See Linux input and output redirection for this topic's reference page.