About command > / dev/null 2 > & 1

About command > / dev / null 2 > & 1

The program will process the external input, and then output the result of the operation to the specified location. For common background programs, the input may come from external files, and the operation results are also written to other files.

In interactive programs (such as bash interacting with Linux), the input is usually the user's keyboard and mouse, and the output is the user's screen. Of course, there is also a need to store the output results in files. For example, delete the interested parts from several G log files. At this time, the redirection of input and output will be used.

File descriptor FD

In the Linux environment, everything is a file. Files provide access to general data, network links, I/O devices, and more. Correspondingly, when a program uses a file, the system will assign a file descriptor to the program. Regardless of the nature of the file, the file descriptor provides a general interface for the interaction between the program and the operating system. Each program will at least these three file descriptors: 0, 1, 2;

When interacting with Linux through commands, the / bin/bash program is used to interpret and execute shell scripts. The file descriptors used by this program are as follows:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-xujm58az-16389564415681) (/ home / idriver /. Config / typora / typora user images / image-20211207151644687. PNG)]

Among them, 0, 1 and 2 mentioned above are:

File descriptortypeDefault locationThe exact name of the open file
0Standard inputUser equipment (keyboard)/dev/pts/30
1standard output screen/dev/pts/30
2Error outputscreen/dev/pts/30

In the screenshot, the u after 0, 1 and 2 indicates the file status mode

  • u: Indicates that the file is open and in read / write mode;
  • r: Indicates that the file is open and in read-only mode;
  • w: Indicates that the file is open and in write mode;
  • -: indicates that the status mode of the file is unknown and locked;
  • Space: indicates that the status mode of the file is unknown and not locked;

redirect

Usually use > or > > for output redirection. The left side is a file descriptor (1 by default) and the right side is a file (of course, it can also be an output device); Use < redirect input, and the file descriptor is on the left (0 by default).

Output redirection

The current directory results are as follows:

# ll
total 8
-rw-r--r-- 1 root root 6 12 July 17:33 out.txt
-rw-r--r-- 1 root root 4 12 July 17:33 out1.txt

Command > filename - > redirect the standard output to a file, or write command 1 > filename;

# ls out.txt test.txt
ls: cannot access test.txt: No such file or directory
out.txt
#Redirect the standard output to the newfile file, and the error output is still output on the console.
# ls out.txt test.txt > newfile
ls: cannot access test.txt: No such file or directory
# cat newfile 
out.txt

Command > > filename - > append the standard output redirection to the file, or write command 1 > > filename;

#Redirect the standard output to the newfile file. At this time, it is found that the content output to the file in the previous step has been overwritten.
# ls out1.txt test.txt > newfile
ls: cannot access test.txt: No such file or directory
# cat newfile 
out1.txt
#Append the standard output redirection to the newfile file. At this time, it is found that the content output to the file in the previous step still exists.
# ls out.txt test.txt >> newfile
ls: cannot access test.txt: No such file or directory
# cat newfile 
out1.txt
out.txt

Command 2 > filename - > redirect the standard error output to a file;

#Redirect the standard error output to the errfile.
# ls out.txt test.txt 2> errfile
out.txt
# cat errfile 
ls: cannot access test.txt: No such file or directory

Command 2 > > filename - > append the standard error output redirection to the file;

#Append the standard error output to the errfile.
# ls out.txt test123.txt 2>> errfile
out.txt
]# cat errfile 
ls: cannot access test.txt: No such file or directory
ls: cannot access test123.txt: No such file or directory

In the above example, there are two types of output after ls out.txt test.txt, in which out.txt is standard output and ls: cannot access test.txt: No such file or director is standard error output. Of course, you can also check redirect error output and standard output to different files.

# ls out.txt test123.txt 2>errfile >newfile
# cat newfile 
out.txt
# cat errfile 
ls: cannot access test123.txt: No such file or directory

input redirection

#The contents of the two documents are as follows:
# cat out.txt 
out
# cat out1.txt 
out1

Command < filename - > take the contents of the file filename as the standard input of the command. You can also write command 0 < filename;

#If there is no file after the cat command, the data will be read from standard input and output from standard output to the screen. End with Ctrl+c.
# cat
1 2 3 4 5
1 2 3 4 5
a b c d e
a b c d e
^C

#Redirect the standard input of cat to out.txt, and then redirect the standard output to newfile.
# cat <out.txt >newfile
# cat newfile 
out

Command < < delimiter - > reads from the standard input, and ends reading when the delimiter delimiter is encountered;

#After typing end, the cat command ends.
# cat <</
> 1 2 3 4 5
> 5 4 3 2 1
> /
1 2 3 4 5
5 4 3 2 1

Redirect binding

With the above foundation, let's look back at command > / dev / null 2 > & 1.

Where > / dev/null redirects the standard output of command to / dev/null. In Linux systems, everything written to / dev/null is lost, so / dev/null is also called a "black hole". 2> & 1 is that the error output will use the same file descriptor as the standard output, that is, the standard error output and the standard output will be output to the same place.

So far, the meaning of command > / dev / null 2 > & 1 is clear, that is, the standard output and standard error output of command are discarded, that is, they are not output to the console or any file.

If you bind first, redirect the standard output to / dev/null. 2> The result of & 1 > / dev/null is that the error output is output to the screen and the standard output is discarded. Because the standard output points to the console output when binding, the error output points to the console, and then points to / dev/null.

If you do not bind, redirect both standard output and error output to / dev/null. 1> / dev/null 2 > / dev/null. In this way, standard output and error output will preempt the pipeline, so it may lead to missing, overwriting, garbled code, etc. when outputting content. In this way, the final situation is unpredictable. Moreover, since the file is opened twice, the two file descriptors will preemptively output content to the file, so the overall IO efficiency is not as high as > / dev/null 2 > & 1.

Keywords: Linux

Added by almightyegg on Thu, 09 Dec 2021 03:38:30 +0200