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 @   小鲨鱼2018  阅读(839)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-03-27 c语言中输入数组元素的值
点击右上角即可分享
微信分享提示