[Share Experiences] Brief Explanation of Output Redirection
Tofloor
poster avatar
SuperDavid
Moderator
2024-09-19 10:45
Author

[Tutorials] Brief Explanation of Output Redirection

File Descriptors

All I/O operations of Linux processes involve reading or writing to a file descriptor. A file descriptor is a non-negative integer associated with a file that a process is using.

Since everything in Linux is a file, a file descriptor could point to a regular file, a device like a keyboard or monitor, a pipe, a network link, and so on.

When a process starts, it has three default file descriptors, as follows:

File Descriptor Chinese Name English Name Default Link
0 标准输入 stdin Terminal input device (keyboard)
1 标准输出 stdout Terminal display device (screen)
2 标准错误输出 stderr Terminal display device (screen)

Redirection

A process (or command) receives input through its "standard input" and sends correct results to "standard output" and error messages to "standard error output."

Input/output redirection changes the default links of standard input (0), standard output (1), and standard error output (2) to other files.

Common Redirection Usages:

Usage Description
command > file1 Redirect standard output, overwriting file1. Equivalent to command 1> file1.
command >> file1 Redirect standard output, appending to file1. Equivalent to command 1>> file1.
command 2> file1 Redirect standard error output, overwriting file1.
command 2>> file1 Redirect standard error output, appending to file1.
command 2> /dev/null Redirect standard error output to /dev/null(discard).
command 1> file1 2>&1 Redirect standard output to file1, and redirect standard error output to the same location.
command 1>> file1 2>&1 Append standard output to file1, and redirect standard error output to the same location.
command &> file1 Use the combined redirection operator to redirect both standard output and standard error output.
command < file1 Redirect standard input from file1. Equivalent to command 0< file1.

The advantage of the combined redirection operator is that it simplifies the syntax, though there may be compatibility issues in some situations.


Pipes

The pipe symbol (|) connects the standard output of one command to the standard input of the next command. Data flows through the pipe from one process to another, and each process modifies the data as it flows through.

For example:

ls -il | wc | less


Using Redirection and Pipes Together

Redirection sends standard output to a file or retrieves standard input from a file. A pipe connects the standard output of one process to the standard input of another.

When using both redirection and pipes together:

  • cat /etc/passwd >passwd.out | wc: If the redirection to a file occurs before the pipe symbol, the command after the pipe will receive an empty input since the standard output has already been redirected to a file.
  • cat /etc/passwd | wc >passwd.out: If the pipe is placed before the redirection, the standard output being redirected is the output of the command after the pipe, not the command before it.

If you want to save the standard output to a file and also use it as input for the next command, you can use the tee command.

The tee command reads from standard input and writes to both standard output and a file.

Examples:

ls /etc | tee etc.list | less

This will overwrite the output of ls /etc into the file etc.list and open it with less.

ls /etc | tee -a etc.list

This appends the output of ls /etc to etc.list while displaying it on the screen.

To redirect both standard output and standard error into a pipe, simply redirect standard error to the same location as standard output: command 2>&1 | command. (Note: The combined redirection operator cannot be used in this case.)


Examples

Standard Output (1) Redirection:

user1@user1-PC:~$ date

Thu Dec 14 12:19:06 CST 2023 # Standard output of the date command

user1@user1-PC:~$ date >date.txt; wc date.txt; cat date.txt

1 6 43 date.txt # Output redirected to date.txt, wc reads the file content

Thu Dec 14 12:19:21 CST 2023 # cat reads date.txt

user1@user1-PC:~$ date 1>>date.txt; wc date.txt; cat date.txt

2 12 86 date.txt # Output appended to the file, now contains two lines

Thu Dec 14 12:19:21 CST 2023 # First line of the file

Thu Dec 14 12:19:34 CST 2023 # Second line of the file

user1@user1-PC:~$ date 2>>date.txt; wc date.txt; cat date.txt

Thu Dec 14 12:19:58 CST 2023 # Error output redirected to the file, correct output to the screen

2 12 86 date.txt # No error message, file content still two lines

Thu Dec 14 12:19:21 CST 2023 # First line of the file

Thu Dec 14 12:19:34 CST 2023 # Second line of the file


Standard Error Output (2) Redirection:

user1@user1-PC:~$ asdf

-bash: asdf: command not found # Error output

user1@user1-PC:~$ asdf 2> /dev/null # Error output discarded

user1@user1-PC:~$ asdf 1> asdf.txt; wc asdf.txt; cat asdf.txt

-bash: asdf: command not found # Error not redirected to the file, shown on screen

0 0 0 asdf.txt # File is empty

user1@user1-PC:~$ asdf 2> asdf.txt; wc asdf.txt; cat asdf.txt

1 2 30 asdf.txt # Error output redirected to the file

-bash: asdf: command not found # File content

user1@user1-PC:~$ asdf 2>> asdf.txt; wc asdf.txt; cat asdf.txt

2 4 60 asdf.txt # Appended to file, now contains two lines

-bash: asdf: command not found # First line of the file

-bash: asdf: command not found # Second line of the file


Standard Input Redirection:

user1@user1-PC:~$ wc /etc/passwd

42 73 2543 /etc/passwd # Without redirection, includes the filename in the output

user1@user1-PC:~$ wc

42 73 2543 # With input redirection, no filename in the output


tee Command Examples:

user1@user1-PC:~$ date | tee date.txt ; wc date.txt; cat date.txt

Thu Dec 14 12:36:56 CST 2023 # tee command outputs to both the screen and the date.txt file

1 6 43 date.txt # Content saved to date.txt

Thu Dec 14 12:36:56 CST 2023 # date.txt content matches the screen output

user1@user1-PC:~$ date | tee date.txt | wc ; wc date.txt; cat date.txt

1 6 43 # tee's standard output is piped to wc

1 6 43 date.txt # wc reads date.txt

Thu Dec 14 12:37:27 CST 2023 # date.txt content

user1@user1-PC:~$ date | tee date.txt >date2.txt ; wc date.txt date2.txt; cat date.txt date2.txt

1 6 43 date.txt # Content written to date.txt by tee

1 6 43 date2.txt # tee's standard output redirected to date2.txt

2 12 86 total

Thu Dec 14 12:38:01 CST 2023 # date.txt content

Thu Dec 14 12:38:01 CST 2023 # date2.txt content matches date.txt

Reply Favorite View the author
All Replies

No replies yet