Hello, I’m Liang Xu.

I believe that we usually need to copy and paste data, if it is to open the file to copy and paste, it will inevitably need more mouse and keyboard operations, will be more cumbersome. So is there a copy-and-paste way to get rid of all this tedious work?

The answer is yes, redirection. Redirection is an efficient way to move data around without a lot of mouse and keyboard manipulation. Redirection can be divided into input redirection and output redirection. Since all programs have input or output, input and output redirection is a built-in feature of any programming or scripting language.

Every time you interact with a computer, redirection is bound to happen. Learning how to use redirection will not only help you interact with your computer better, but also increase your productivity, so let Liang Hui explain the common uses of redirection in Linux:

Data flow in Linux

When it comes to Linux redirection, there are three types of data flows:

  • Input information will be taken fromstdinStandard input, usually keyboard or mouse.
  • The output information will be output tostdoutStandard output, a text file or data stream.
  • Error messages will be printed tostderr.

Knowing that these data streams exist gives you more control over the flow of data when you use your Shell.

In Linux, 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/stdoutCopy the code

Redirection output

On Linux, the > character is used to indicate the redirected output. For example, to redirect the output of the ls command to a file:

$ ls > list.txtCopy the code

After the preceding command is executed, the output of the ls command is not displayed on the screen because it has been redirected to the list. TXT file.

Redirection can also be used to copy the contents of files, and not just text files, binary files can also be copied:

$ cat image.png > picture.pngCopy the code

If you want to copy the contents of one file to the end of another, you simply replace the > character with a >> string, like this:

$ cat lxlinux >> alvinCopy the code

Redirect input

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

Input redirection You can redirect input information to a command as a parameter. This feature may be used sparingly, but it comes in handy when the 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 contains a list of packages you need to install, and if you want to install all packages quickly, you can install all packages in package.list at once by executing the following command:

$ sudo dnf install $(<package.list)Copy the code

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

Here-doc redirects a block of input text 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 EOF by default.)

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

$ cat << EOF
> alvin
> lxlinux.net
> EOFCopy the code

The expected output should look like this:

alvin
lxlinux.netCopy the code

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 strings enclosed in quotes:

$ cat <<< alvin
alvin
$ cat <<< "alvin lxlinux.net"
alvin lxlinux.netCopy the code

Redirection error message

By default, error messages go to a stream called stderr, which can be redirected using 2>. For example, to redirect error messages to a file named output.log:

$ ls /nope 2> output.logCopy the code

Redirect data to /dev/null

Just like standard input, standard output, and standard error, there is a file called null in the /dev directory that corresponds to null in the Linux file system. To make it easier to read, people often omit the slash and pronounce it dev null.

/dev/null does not save data, and data written to /dev/null is eventually lost, as if thrown into the void. Therefore, you can use redirection to funnel unwanted data to /dev/null. For example, the output of the find command tends to be verbose, and when searching for files, permissions conflicts are often reported, 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.fileCopy the code

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.fileCopy the code

Make use of redirection

In Bash, redirection is an efficient way to move data. You may not always use redirects, but learning how to use redirects will save you a lot of unnecessary copy-and-paste operations when you need them, and therefore a lot of mouse and keyboard time. Please don’t stick to copy and paste, using redirection can improve your work efficiency, isn’t it sweet?

Finally, I hope you found this article helpful, and if you still have any questions about redirection in Linux, please let me know in the comments!


Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you! The directory is as follows:

Free to everyone, just ask everyone to point to me!

Ebook | Linux development learning roadmap

Also hope to have a small partner can join me, do this e-book more perfect!

Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article

Recommended reading:

  • Dry goods | programmers advanced architect necessary resources free of charge
  • Artifact | support resource site search