Linux redirection usage


Hello, I’m Liang Xu.

I believe that you usually need to copy and paste data, if you open the file to copy and paste, it will inevitably need more mouse and keyboard operation, it will be more cumbersome. So is there a copy and paste method that can save these tedious operations?

The answer is yes, and that’s 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, redirection of input and output is a function of any programming language or scripting language.

Whenever you interact with a computer, redirection is inevitable. Learning to use redirection can not only make you interact better with your computer, but also improve your work efficiency. Therefore, please let Liangxu explain the common usage of redirection in Linux system to you

Data flow in Linux

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

  • Enter information fromstdinRead from (standard input, usually keyboard or mouse).
  • The output information will be output tostdout(standard output, a text file or data stream).
  • Error messages are output to thestderr

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

In Linux system, standard input, standard output and standard error exist as files. You can do it in/devYou can see them under the directory:

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

redirect output

In Linux system, using>The character represents the redirection output. For example, thelsThe output of the command is redirected to a file:

$ ls > list.txt

After executing the above command, it will not be displayed on the screenlsThe output of the command, because the output has been redirected tolist.txtIt’s in the file.

In addition, redirection has many uses. It can also be used to copy the contents of a file. 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, you just need to>Replace characters with>>String, like this:

$ cat lxlinux >> alvin

redirect input

As opposed to the redirection output, the redirection input uses the<Character.

Input redirection can be used as a parameter to redirect input information to a command. This function may be less used, but when the command needs a parameter list, and these parameters are stored in a file, and then you want to quickly copy and paste them from the file to the terminal, this function can be used.

For example,package.listThe list of packages you need to install is recorded in. If you want to install all packages quickly, just execute the following command to install them at one timepackage.listAll the bags in the bag:

$ sudo dnf install $(

The 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 marker is encountered (the end of file marker can be any unique string, but most people use it by default)EOF)。

You can try the following command on the terminal (until the second one)EOFEnd of string:

$ cat << EOF
> alvin

The expected output should be as follows:


Here doc is a common technique used by bash script writers 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 quoted strings:

$ cat <<< alvin
$ cat <<< "alvin"

Redirection error message

By default, the error message will be calledstderrStream, using2>It can be redirected. For example, redirect the error message to aoutput.logIn the following file:

$ ls /nope 2> output.log

Redirect data to / dev / null

Just like standard input, standard output and standard error, in Linux file system, there is a file corresponding to null, which is called nullnull, put in/devUnder the directory. For the convenience of reading, people often omit the slash and read it asdev null

/dev/nullData is not saved and is written/dev/nullAll data will be lost in the end, just like being thrown into the void. Therefore, you can use redirection to send unwanted data to/dev/null. For example,findThe output of the command is often very lengthy, and the error of permission conflict is often reported when searching the file, such as this:

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

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

$ find ~ -type f 2> /dev/null

Make good use of redirection

In Bash, redirection is an efficient way to transfer data. You may not always use redirection, but learning how to use redirection can save you a lot of unnecessary copy and paste operations when you need it, so it also saves a lot of time to operate the mouse and keyboard. Please don’t stick to copy and paste, using redirection can improve your work efficiency, isn’t it fragrant?

Finally, I hope the content of this article is helpful to your work. If you have any questions about redirection in Linux system, please leave a message to me!

Official account: good Linux

Linux redirection usage

What’s the gain? I hope the old fellow will take three strikes to show this article to more people.

Recommended Today

The use of springboot Ajax

Ajax overview What is Ajax? data Ajax application scenarios? project Commodity system. Evaluation system. Map system. ….. Ajax can only send and retrieve the necessary data to the server, and use JavaScript to process the response from the server on the client side. data But Ajax technology also has disadvantages, the biggest disadvantage is that […]