The meaning of linux 2>&1

illustrate:

In the linux system, in order to facilitate the files opened by the kernel management system, an index file will be generated in the /proc directory. This index file is the file descriptor (FileDescription) corresponding to each file opened;

  • Example:
[root@node2 proc]# systemctl start nginx
[root@node2 proc]# ps -ef|grep nginx
root      16442      1  0 21:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     16443  16442  0 21:37 ?        00:00:00 nginx: worker process

Then the directories fd of 16442 and 16443 will be generated in the /proc directory, which are the file descriptor information:

[root@node2 fd]# pwd
/proc/16442/fd

insert image description here
The kernel can manage files directly based on the file descriptor. For example, we now remove the access.log file corresponding to the file descriptor 5: you
insert image description here
can see that the file corresponding to 5 is the moved /tmp/access.log, that is The file descriptor can directly link the target file, no matter how the target file is changed, the file descriptor will not change.

For the linux system, it has 3 default file descriptors, namely 0, 1,
2 0: STDIN_FILENO means standard input
1: STDOUT_FILENO means standard output
2: STDERR_FILENO means standard error output
Open a new file by default, its file descriptor for 3.

The meaning of 2>&1

For example, an error is reported when executing a command:

[root@node2 sh]# ls -ltr aaa
ls: 无法访问aaa: 没有那个文件或目录
[root@node2 sh]# ls -ltr aaa >aa.log 2>&1
[root@node2 sh]# cat aa.log
ls: 无法访问aaa: 没有那个文件或目录
[root@node2 sh]# 
  • 2 means standard error output, &1 means 1 file descriptor, that is, standard output, > means redirection
    Then, 2>&1 means redirect standard error output to standard output;
  • The meaning of ls -ltr aaa >aa.log 2>&1
    ls -ltr aaa >aa.log means to redirect the standard output of the "ls -ltr aaa" command to the file aa.log, and the 2> on the right &1 is to send the error output to the standard output, so the overall meaning is to redirect the standard error output and standard output together to the aa.log file.
    So can't the command 2>&1 on the right be written to the left?
[root@node2 sh]# ls -ltr aaa 2>&1 >aa.log
ls: 无法访问aaa: 没有那个文件或目录
[root@node2 sh]# 

This is equivalent to separating the command ls -ltr and >aa.log, and the function of writing logs to aa.log cannot be realized.
Give a few examples to feel the use of descriptors:

1. ls -ltr 2>1 will not report that there is no 2 file, but will generate an empty file 1

[root@node2 test]# ls -ltr 2
ls: 无法访问2: 没有那个文件或目录
[root@node2 test]# ls -ltr 2>1
总用量 0
-rw-r--r-- 1 root root 0 4月  24 21:51 1
[root@node2 test]# 

2. ls -ltr 2 2>1: output the error report without the file 2 to the file 1

[root@node2 test]# ls -ltr 2 2>1
[root@node2 test]# cat 1
ls: 无法访问2: 没有那个文件或目录
[root@node2 test]# 

3. ls -ltr 2 2>&1, the error will be output to the standard output, and the file 1 will not be generated

[root@node2 test]# ls -ltr 2 2>&1
ls: 无法访问2: 没有那个文件或目录
[root@node2 test]# ll
总用量 0
[root@node2 test]# 

4. ls -ltr 2 >aa.log 2>&1 , the error will be redirected to the standard output, and the standard output will be redirected to the aa.log file at the same time:

[root@node2 test]# ls -ltr 2 >aa.log 2>&1
[root@node2 test]# ls -ltr
total usage 4
-rw-r–r-- 1 root root 47 April 24 21:58 aa .log
[root@node2 test]# cat aa.log
ls: Unable to access 2: No such file or directory
[root@node2 test]#

Guess you like

Origin blog.csdn.net/weixin_42808782/article/details/116104295