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
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-03-27 c语言中输入数组元素的值