Explain the usage of Linux redirection in detail

  • 2021-08-17 01:45:11
  • OfStack

I believe that everyone usually needs to copy and paste data. If you open a file for copying and pasting, it will inevitably require more mouse and keyboard operations, which will be tedious. So is there any copy-and-paste method that can save these tedious operations?

The answer is yes, that is, redirection. Redirection is an efficient way to transfer data without a lot of mouse and keyboard operations. Redirection can be divided into two types: input redirection and output redirection. Since all programs have input or output, input and output redirection is a feature that comes with any programming or scripting language.

Whenever you interact with the computer, redirection will inevitably happen. Learning to use redirection can not only make you interact with your computer better, but also improve your work efficiency. Therefore, please ask Liang Xu to explain the common usage of redirection in Linux system:

Data Flow in Linux

When it comes to Linux redirection, we have to mention the following three data streams:

Input information is read from stdin (standard input, usually keyboard or mouse). The output information will be output to stdout (standard output, 1 text file or data stream). Error messages are output to stderr.

Knowing the existence of these data streams, you can better control the flow of data when you use Shell.

In the Linux system, standard input, standard output, and standard error all exist as files. You can see them in the/dev directory:


$ ls /dev/std* 
/dev/stderr /dev/stdin /dev/stdout 

Redirect output

In the Linux system, use the > Character denotes redirected output. For example, redirect the output of the ls command to a file:


$ ls > list.txt 

The output from the ls command does not appear on the screen after executing the above command, because the output has been redirected to the list. txt file.

In addition, redirection has many uses. It can also be used to copy the contents of files, and it is not limited to copying text files. Binary files can also be copied:


$ cat image.png > picture.png 

If you want to copy the contents of one file to the end of another file, you only need to > Character replacement > > String will do, like this:


$ cat lxlinux >> alvi 

Redirect input

In contrast to the redirected output, the redirected input uses the < Characters.

Input redirection can be used as a parameter by redirecting input information to commands. This feature may be less useful, but it comes in handy when a command requires a list of parameters that are stored in a file and you want to quickly copy and paste them from the file to the terminal.

For example, package. list records the list of packages you need to install, and if you want to install all the packages quickly, you can install all the packages in package. list once by executing the following command:


$ sudo dnf install $(<package.list) 

Common uses of input redirection are Here-document (Here-doc for short) and Here-string.

Here-doc redirects the input text block to the standard input stream until a special end-of-file tag is encountered (the end-of-file tag can be any one-only string, but most people use EOF by default).

You can try to enter the following command at the terminal (until the end of the second EOF string):


$ cat << EOF 
> alvin 
> lxlinux.net 
> EOF 

The expected output should look like this:

alvin
lxlinux.net

Here-doc is a common technique used by Bash scriptwriters to dump multiple lines of text to a file or screen.

Here-string is similar to Here-doc, but it has only one string, or several strings enclosed in quotation marks:


$ cat <<< alvin 
alvin 
$ cat <<< "alvin lxlinux.net" 
alvin lxlinux.net 

Redirect error message

Error messages go to a stream called stderr by default, using 2 > You can redirect it. For example, redirect the error message to a file named output. log:


$ ls /nope 2> output.log 

Redirect data to/dev/null

Just like Standard Input, Standard Output, and Standard Error 1, in the Linux file system, there is an empty file corresponding to it, which is called null and is placed in the/dev directory. For the convenience of reading, people often omit the slash and pronounce it directly as dev null.

/dev/null does not save data, and the data written to/dev/null will eventually be lost, just like being thrown into the void. Therefore, you can use redirection to send unwanted data to/dev/null. For example, the output of the find command is often verbose, and permissions conflict errors are often reported when searching files, like this:


$ find ~ -type f 
/home/seth/actual.file 
find: `/home/seth/foggy': Permission denied 
find: `/home/seth/groggy': Permission denied 
find: `/home/seth/soggy': Permission denied 
/home/seth/zzz.file 

At this point, you can redirect the error message to/dev/null to filter out unnecessary information, like this:


$ find ~ -type f 2> /dev/null 
/home/seth/actual.file 
/home/seth/zzz.file 

Make good use of redirection

In Bash, redirection is an efficient method to transfer data. You may not always use redirection, but learning how to use it can save you a lot of unnecessary copy-and-paste operations when you need it, so it also saves you a lot of time operating the mouse and keyboard. Please don't cling to copy and paste, using redirection can improve your work efficiency, don't it smell good?


Related articles: