Redirect Input and Output in Linux
Learn how Linux shells redirect stdin, stdout, and stderr with >, >>, <, 2>, 2>&1, and pipes.
Linux programs communicate through input and output streams. A shell normally connects a command's standard input to the keyboard, sends standard output to the terminal, and displays standard error on the terminal as well. Redirection changes those default connections.
Redirection syntax is written after a command and is interpreted by the shell before the command starts. The shell opens files or connects streams, then launches the program with those altered connections.
Standard input, output, and error
A file descriptor is a numeric handle that a Unix-like process uses for an open input or output stream. Linux conventionally assigns these descriptors to the three standard streams:
| Name | Descriptor number | Default source or destination | Typical content |
|---|---|---|---|
| stdin | 0 | Keyboard input | Data a command reads |
| stdout | 1 | Terminal | Normal command results |
| stderr | 2 | Terminal | Diagnostic and error messages |
stdout is intended for normal results, while stderr is intended for warnings, diagnostics, and errors. Because they are separate streams, redirecting stdout does not automatically redirect stderr.
Linux shell redirection operators
| Operator | Stream affected | Behavior | Example | Notes |
|---|---|---|---|---|
> | stdout | Write to a file | ls > ls_output.txt | Creates the file or overwrites its contents |
>> | stdout | Append to a file | date >> ls_output.txt | Preserves existing contents |
< | stdin | Read from a file | mail bob < ls_output.txt | Supplies file contents as standard input |
2> | stderr | Write errors to a file | command 2> errors.txt | Replaces the error file if it already exists |
2>> | stderr | Append errors to a file | command 2>> errors.txt | Preserves earlier errors |
2>&1 | stderr | Send stderr to stdout's current destination | command > all_output.txt 2>&1 | Order matters |
| | stdout to stdin | Connect two commands | ls | less | Uses a pipe instead of a file |
&> and &>> | stdout and stderr | Combine both streams | command &> all_output.txt | Common in Bash, but not POSIX shell syntax |
Redirect standard output to a file
Use > to send stdout to a file:
ls > ls_output.txt
This command creates ls_output.txt if it does not exist. If the file already exists, > removes its previous contents and writes the new output in its place. This behavior is called overwrite.
The command may appear to produce no output because the directory listing went into the file instead of the terminal. Inspect it with cat or less:
cat ls_output.txt
less ls_output.txt
Append standard output
Use >> to add stdout after the existing contents. This is called append:
date >> ls_output.txt
Each execution adds the current date and time as another record. Unlike >, >> does not replace earlier content. It creates the file if necessary and appends when the file already exists.
Redirect standard input from a file
Use < to make a command read a file through stdin. The command receives the file contents as though those contents had been provided through its normal input stream:
mail bob < ls_output.txt
This uses the contents of ls_output.txt as the message body for the mail command. The exact mail utility and its availability vary by Linux distribution.
Input redirection is useful for commands that consume text, data, or messages from stdin. The command must actually be designed to read stdin; some commands ignore stdin unless a particular option is supplied.
Redirect standard error
Prefix a redirection operator with 2 to select stderr, whose descriptor number is 2. For example:
ls /path/that-does-not-exist 2> errors.txt
The diagnostic message is written to errors.txt instead of appearing on the terminal. Use 2>> when errors should be appended:
command 2>> errors.txt
You can save normal output and errors separately:
command > output.txt 2> errors.txt
Here, stdout goes to output.txt and stderr goes to errors.txt. If you use only >, errors can still appear on the terminal because only stdout was redirected.
Combine stdout and stderr
To save both streams in one file using portable shell syntax, redirect stdout first and then duplicate stdout's destination for stderr:
command > all_output.txt 2>&1
2>&1 means “make descriptor 2 go wherever descriptor 1 currently goes.” The order is important:
command > all_output.txt 2>&1
In this form, stdout is first assigned to all_output.txt, and stderr is then connected to that same destination. Reversing the order can leave stderr connected to the terminal:
command 2>&1 > all_output.txt
At the moment 2>&1 is processed, stdout still points to the terminal, so stderr is duplicated there. Later changing stdout does not change the already-created stderr connection.
Bash and some other shells also provide shorthand:
command &> all_output.txt
command &>> all_output.txt
These forms combine stdout and stderr, with overwrite and append behavior respectively. They are convenient but are not POSIX shell syntax, so use > file 2>&1 when portability is important.
Pipes: command-to-command redirection
A pipe connects one program's stdout directly to another program's stdin. The pipe operator is |:
ls | less
In this example, ls does not write its normal output directly to the terminal. The shell sends it through a pipe, and less displays the listing one screen at a time.
A file redirection connects a program to a file, while a pipe connects one program to another:
ls > listing.txt
ls | less
The first command saves output for later inspection. The second passes output immediately to another command, often a filter, formatter, or pager.
Permissions and command status
The shell performs file redirection, so the current user must be able to create or modify the target file and must have suitable permission for its directory. A redirection can fail with “Permission denied” even before the command itself runs.
Choose a writable destination or use appropriate administrative procedures to correct ownership and permissions. Do not assume that redirecting output changes the command's success: redirection only changes stream connections. The command can still succeed or fail independently, and its exit status still reflects its execution.
Troubleshooting redirection
- No output appears on the terminal: stdout was probably redirected with
>. Read the file withcatorless. - Earlier file contents disappeared:
>overwrote the file. Use>>when you need to preserve existing content. - Errors still appear on screen: only stdout was redirected. Use
2> errors.txtfor separate errors or2>&1to combine them. - Combined output is missing errors: check the order. Use
command > output.txt 2>&1. - Permission is denied: verify that the current user can write the target file and directory.
- Input redirection does not work as expected: check the file path and permissions, inspect the file, and verify that the command reads stdin rather than requiring a separate input option.
Practical workflow
- Run the command normally to understand its output and errors.
- Choose whether you need stdout, stderr, or both.
- Use
>for a new replacement file or>>to preserve and add to existing content. - Inspect redirected files with
catorless. - For command-to-command processing, use
|instead of an intermediate file.
For related fundamentals, review Bash, the Linux file structure, and file ownership and permissions.