Detailed explanation of Linux redirection usage

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste data sometimes. If you open a file to copy and paste, it will inevitably require more mouse and keyboard operations, which will be cumbersome. So is there a way to copy and paste without these tedious operations?

The answer is yes, it is redirection. Redirection is an efficient method that can complete data transfer 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 native function of any programming or scripting language.

Whenever you interact with a computer, redirects are bound to occur. Learning to use redirection not only allows you to interact better with your computer, but also improves your work efficiency. Therefore, please let Liang Xu explain to you the common usage of redirection in Linux system:

Data Flow in Linux

When talking about Linux redirection, we have to mention the following three data streams:

  • Input is read from stdin (standard input, usually the keyboard or mouse).
  • Output is sent to stdout (standard output, a text file or data stream).
  • Error messages are output to stderr.

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

In Linux systems, 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

Redirecting Output

In Linux systems, use the > character to redirect output. For example, to redirect the output of the ls command to a file:

$ ls > list.txt

After executing the above command, the output information of the ls command will not be displayed on the screen because the output information 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. It is not limited to copying text files, but 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 can just replace the > character with the >> string, like this:

$ cat lxlinux >> alvi

Redirecting Input

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

Input redirection can redirect input information to the command as a parameter. This feature may be rarely used, but when a command requires a list of parameters, and these parameters are all in a file, and you want to quickly copy and paste them from the file into the terminal, this feature can come in handy.

For example, package.list contains a list of packages you need to install, and if you want to quickly install all packages, you only need to execute the following command to install all packages in package.list at once:

$ sudo dnf install $(<package.list)

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 entering the following command in the terminal (until the second EOF string ends):

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

The expected output should be something like this:

alvin
lxlinux.net

Here-doc is a common trick used by Bash scripters to dump multiple lines of text to a file or the screen.

Here-string is similar to here-doc, but it only takes one string, or several strings enclosed in quotes:

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

Redirecting Error Messages

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

$ ls /nope 2> output.log

Redirect data to /dev/null

Just like standard input, standard output, and standard error, in the Linux file system, there is also a file corresponding to it, it is called null and is placed in the /dev directory. For ease of reading, people often omit the slash and read it directly as dev null.

/dev/null does not store data, and data written to /dev/null will eventually be lost, just like being thrown into the void. Therefore, you can use redirection to pipe unwanted data to /dev/null. For example, the output of the find command is often very verbose and often reports permission conflicts when searching for 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 time, you can redirect the error information 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 redirects

Redirection is an efficient way to move data in Bash. You may not always use redirection, but knowing how to use it when you need it can save you a lot of unnecessary copying and pasting operations, thereby saving a lot of time operating the mouse and keyboard. Please don't be obsessed with copying and pasting. Using redirection can improve your work efficiency. Isn't it great?

This is the end of this article on the detailed usage of Linux redirection. For more relevant content on the usage of Linux redirection, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed instructions for using Linux input and output redirection
  • Linux shell pipe command usage and difference from shell redirection
  • Detailed analysis of linux shell data redirection (input redirection and output redirection)
  • Detailed explanation of Linux base shell redirection
  • Detailed explanation of Linux input and output redirection
  • Analysis of 301 redirection code for linux URL

<<:  Use js to write a simple snake game

>>:  js to implement a simple bullet screen system

Recommend

Script example for starting and stopping spring boot projects in Linux

There are three ways to start a springboot projec...

Multi-service image packaging operation of Dockerfile under supervisor

Writing a Dockerfile Configure yum source cd /tmp...

How to get the real path of the current script in Linux

1. Get the real path of the current script: #!/bi...

Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Table of contents 1. Installation preparation 1. ...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...

Pure CSS3 to achieve pet chicken example code

I have read a lot of knowledge and articles about...

JavaScript to switch multiple pictures

This article shares the specific code of JavaScri...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

CSS3 category menu effect

The CSS3 category menu effects are as follows: HT...

Some problems that may be caused by inconsistent MySQL encoding

Stored procedures and coding In MySQL stored proc...

How to use Nginx to carry rtmp live server

This time we set up an rtmp live broadcast server...