Basic Linux tutorial-data stream redirection
Introduction
I started to feel a little unfamiliar with the concept of redirection, but I have seen it many times through the previous course.>
Or>>
And know that they direct the standard output to a file or append it to a file. This is actually a redirection, which redirects the data originally output to the standard output to a file, because the standard output (/dev/stdout
) Itself is also a file. It is no problem for us to direct command output to another file.
1. Data Stream redirection
The following is a brief review of the two redirection operations that are frequently used earlier:
$ echo 'hello shiyanlou' > redirect $ echo 'www.shiyanlou.com' >> redirect$ cat redirect
Of course, the previous<
And<<
There is no problem with operations. As you understand, the difference between them is that the direction of redirection is inconsistent,>
From left to right,<
Right to left.
1. Simple redirection
Before learning more about Linux redirection, You need to first know some basic things. As mentioned above, Linux provides three special devices by default for terminal display and output, which arestdin
(Standard input, corresponding to your input on the terminal ),stdout
(Standard output, corresponding to terminal output ),stderr
(Standard error output, corresponding to terminal output ).
File descriptor |
Device Files |
Description |
0 |
/dev/stdin |
Standard Input |
1 |
/dev/stdout |
Standard output |
2 |
/dev/stderr |
Standard Error |
File descriptor: The file descriptor is a non-negative integer in form. In fact, it is an index value that points to the record table for opening files for each process maintained by the kernel. When the program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In program design, some underlying programming is usually centered around the file descriptor. However, the file descriptor concept is often only applicable to operating systems such as UNIX and Linux.
There is also a symbol-
Which can be used as the previous command at the same time.
These file descriptors can be used as follows:
By default, the standard input of the terminal is used as the command input and standard output as the command output.
$ Cat (press Ctrl + C to Exit)
Redirects cat continuous output (in heredoc mode) to a file
$ mkdir Documents$ cat > Documents/test.c\~ <<EOF#include <stdio.h>int main(){ printf("hello world\n"); return 0;}EOF
Use a file as the command input and standard output as the command output.
$ cat Documents/test.c\~
Use the data transmitted from the echo command through the pipeline as the input of the cat command, and use the standard output as the output of the command.
$ echo 'hi' | cat
Redirects the echo command output from the default standard output to a common file.
$ echo 'hello shiyanlou' > redirect$ cat redirect
Do not confuse pipelines with redirection. By default, pipelines connect the output of the previous command to the input of the next command. Redirection usually requires a file to establish a connection between the two commands, let's take a closer look at the similarities and differences between the third operation and the last two operations.
2. Standard Error redirection
Redirecting standard output to a file is a very practical operation. Another practical operation is to redirect standard errors. Both standard output and standard errors are displayed on the screen of the Pseudo Terminal, therefore, the output of a common command usually contains both standard output and standard error results. For example:
# Use the cat command to read two files at the same time, one of which exists and the other does not exist $ cat Documents/test. c \~ Hello. c # You can see that in addition to correctly outputting the content of the previous file, an error message is displayed at the end. # Below we will redirect the output to a file. Based on our previous experience, $ cat Documents/test. c \~ Hello. c> somefile
Unfortunately, the error message still appears, because, as I said above, both standard output and standard errors point to terminal screens, but they are not the same. Sometimes we want to hide some errors or warnings. What should we do. This requires the file descriptor mentioned above:
# Redirect a standard error to the standard output, and then redirect the standard output to the file. Note that you need to write the redirection to the file before $ cat Documents/test. c \~ Hello. c> somefile 2> & 1 # Or only use the special redirection symbol "&" provided by bash to redirect both standard errors and standard output to the file $ cat Documents/test. c \~ Hello. c &> somefilehell
Note that you should add&
Otherwise, shell redirects to a file named 1.
3. Use
tee
Command to redirect to multiple files at the same time
You may also have this requirement. In addition to redirecting the output to a file, you also need to print the information on the terminal, so you can usetee
Command to achieve:
$ echo 'hello shiyanlou' | tee hello
4. Permanent redirection
You can see that all the previous redirection operations are temporary, that is, they are only valid for the current command. How can they be "permanent", for example, in a script, you need to redirect all the output of some commands. Do you need to add a temporary redirection operation to each command? Of course, no, we can useexec
Command to achieve "permanent" redirection.exec
The command is used to replace the current Shell with the specified command, replace the current process with a process, or specify a new redirection:
# First enable a sub-Shell $ zsh # Replace the redirection of the current process with exec, redirects the standard output to a file $ exec 1> somefile # the output of the command you run will be redirected to the file until you exit the current sub-shell, or cancel exec redirection (which will tell you how to do it later) $ ls $ exit $ cat somefile
5. Create output file descriptor
By default, the Shell can contain nine open file descriptors, which are also provided by default.0
,1
,2
In addition, we can also use 3-8 file descriptors, but they are not opened by default. You can use the following command to view the file descriptors opened in the Current Shell Process:
$ cd /dev/fd/;ls -Al
Same useexec
Command to create a new file descriptor:
$ Zsh $ exec 3> somefile # first go to the directory and check it. Otherwise, you may not get the correct result and then return to the last directory $ cd/dev/fd /; ls-Al; cd-# Note that there should be no space between the following command> and, if there is a space, an error occurs. $ echo "this is test"> & 3 $ cat somefile $ exit
6. Disable the file descriptor.
As shown in the file descriptor 3 we opened above, you can disable it using the following operations:
$ exec 3>&-$ cd /dev/fd;ls -Al;cd -
7. Completely SHIELD command output
In Linux, there is a device file that becomes a "black hole", so the data imported into it will be "swallowed up ".
In UNIX-like systems,/dev/null, or an empty device, is a special device file, which is usually used to discard unwanted output streams, or as an empty file for the input stream. These operations are usually completed by redirection. Read it to get an EOF immediately.
We can use/dev/null
Blocking command output:
$ cat Documents/test.c\~ nefile 1>/dev/null 2>&1
The above operation will not result in any output.
8. Use xargs to split the parameter list
Xargs is a common command for UNIX and UNIX-like operating systems. It is used to convert the parameter list into small segments and pass it to other commands to avoid the problem that the parameter list is too long.
This command is useful sometimes, especially when it is used to process the results that generate a large number of output results, such as find, locate, and grep. For detailed usage, see the man documentation.
$ cut -d: -f1 < /etc/passwd | sort | xargs echo
The above command is used/etc/passwd
File:
After sorting the first field, useecho
Command to generate a list.
This article permanently updates the link address: