linux 中标准输出重定向、标准错误输出重定向、追加重定向

1、标准输出重定向:1>,  1可以省略

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l a.txt 1> aaa    ## 标准输出
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt

 

2、标准错误输出重定向: 2>, 2不可以省略

root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# ls -l b.txt      ## b.txt不存在
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l b.txt 1> bbb111    ## 标准输出重定向
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l b.txt 2> bbb222    ## 标准错误输出重定向
root@ubuntu01:/home/test# ls
aaa  a.txt  bbb111  bbb222
root@ubuntu01:/home/test# cat bbb111
root@ubuntu01:/home/test# cat bbb222     ## 只有标准错误输出重定向可以写入标准错误信息
ls: cannot access 'b.txt': No such file or directory

 

3、 标准输出、标准错误输出重定向: &>,  相当于包含同时包含 1>  和  2>。

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt &> aaa
root@ubuntu01:/home/test# ls -l b.txt &> bbb
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# cat bbb
ls: cannot access 'b.txt': No such file or directory

 

4、标准输出、标准错误输出追加重定向  >> file 2>&1或者  &>> file, 两者等价

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt >> aaa 2>&1    ## 标准输出、标准错误输出追加重定向
root@ubuntu01:/home/test# ls -l b.txt >> aaa 2>&1
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
ls: cannot access 'b.txt': No such file or directory

 

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt &>> aaa     ## 标准输出、标准错误输出追加重定向
root@ubuntu01:/home/test# ls -l b.txt &>> aaa
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
ls: cannot access 'b.txt': No such file or directory

 

posted @ 2022-03-27 19:58  小鲨鱼2018  阅读(776)  评论(0编辑  收藏  举报