[Share Experiences] Brief Explanation of Output Redirection
Tofloor
default avatar
SuperDavid
deepin
2024-09-19 02: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
<span>command > file1</span> Redirect standard output, overwriting <span>file1</span>. Equivalent to <span>command 1> file1</span>.
<span>command >> file1</span> Redirect standard output, appending to <span>file1</span>. Equivalent to <span>command 1>> file1</span>.
<span>command 2> file1</span> Redirect standard error output, overwriting <span>file1</span>.
<span>command 2>> file1</span> Redirect standard error output, appending to <span>file1</span>.
<span>command 2> /dev/null</span> Redirect standard error output to <span>/dev/null</span>(discard).
<span>command 1> file1 2>&1</span> Redirect standard output to <span>file1</span>, and redirect standard error output to the same location.
<span>command 1>> file1 2>&1</span> Append standard output to <span>file1</span>, and redirect standard error output to the same location.
<span>command &> file1</span> Use the combined redirection operator to redirect both standard output and standard error output.
<span>command < file1</span> Redirect standard input from <span>file1</span>. Equivalent to <span>command 0< file1</span>.

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 (<span>|</span>) 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:

<span>ls -il | wc | less</span>


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:

  • <span>cat /etc/passwd >passwd.out | wc</span>: 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.
  • <span>cat /etc/passwd | wc >passwd.out</span>: 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 <span>tee</span> command.

The <span>tee</span> command reads from standard input and writes to both standard output and a file.

Examples:

<span>ls /etc | tee etc.list | less</span>

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

<span>ls /etc | tee -a etc.list</span>

This appends the output of <span>ls /etc</span> to <span>etc.list</span> 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: <span>command 2>&1 | command</span>. (Note: The combined redirection operator cannot be used in this case.)


Examples

Standard Output (1) Redirection:

<span>user1@user1-PC:~$ date</span>

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

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

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

<span>Thu Dec 14 12:19:21 CST 2023 # cat reads date.txt</span>

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

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

<span>Thu Dec 14 12:19:21 CST 2023 # First line of the file</span>

<span>Thu Dec 14 12:19:34 CST 2023 # Second line of the file</span>

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

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

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

<span>Thu Dec 14 12:19:21 CST 2023 # First line of the file</span>

<span>Thu Dec 14 12:19:34 CST 2023 # Second line of the file</span>


Standard Error Output (2) Redirection:

<span>user1@user1-PC:~$ asdf</span>

<span>-bash: asdf: command not found # Error output</span>

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

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

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

<span>0 0 0 asdf.txt # File is empty</span>

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

<span> 1 2 30 asdf.txt # Error output redirected to the file</span>

<span>-bash: asdf: command not found # File content</span>

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

<span> 2 4 60 asdf.txt # Appended to file, now contains two lines</span>

<span>-bash: asdf: command not found # First line of the file</span>

<span>-bash: asdf: command not found # Second line of the file</span>


Standard Input Redirection:

<span>user1@user1-PC:~$ wc /etc/passwd</span>

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

<span>user1@user1-PC:~$ wc </etc/passwd</span>

<span> 42 73 2543 # With input redirection, no filename in the output</span>


<span>tee</span> Command Examples:

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

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

<span> 1 6 43 date.txt # Content saved to date.txt</span>

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

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

<span> 1 6 43 # tee's standard output is piped to wc</span>

<span> 1 6 43 date.txt # wc reads date.txt</span>

<span>Thu Dec 14 12:37:27 CST 2023 # date.txt content</span>

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

<span> 1 6 43 date.txt # Content written to date.txt by tee</span>

<span> 1 6 43 date2.txt # tee's standard output redirected to date2.txt</span>

<span> 2 12 86 total</span>

<span>Thu Dec 14 12:38:01 CST 2023 # date.txt content</span>

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

Reply Favorite View the author
All Replies
m***s@outlook.com
deepin
2024-10-17 05:25
#1
It has been deleted!
New Thread

Popular Events

More
国际排名
WHLUG