输出重定向

1 关于标准输入和输出

键盘  0  标准输入

显示屏 1  标准输出

显示屏    2  标准错误输出

2 输出重定向

1)标准输出重定向

  命令 >  文件  以覆盖的方式将命令的正确输出输入到指定文件或者设备中

  命令 >>  文件   以追加的方式将正确命令输出到指定的文件或者系统中

2)标准错误输出重定向

  错误命令 2>文件  以覆盖的方式错误命令输出到指定文件或者设备中

  错误命令 2>文件  以追加的方式将错误的命令输出到指定的文件或者系统中

注意:标准错误输出重定向中  2>文件 和 2>>文件 之间是没有空格的

td@td-Lenovo-IdeaPad-Y410P:~$ ls > test
td@td-Lenovo-IdeaPad-Y410P:~$ cat test
test
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
td@td-Lenovo-IdeaPad-Y410P:~$ ls cangls 2>>test
td@td-Lenovo-IdeaPad-Y410P:~$ cat test
test
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
ls: 无法访问'cangls': 没有那个文件或目录

 3 正确出和错误输出同时保存

1)命令 >  文件   2>&1

     命令 >> 文件  2>&1

上述命令表明 :如果是正确输出,则直接保存在文件中,如果是错误输出则将错误输出重定向到正确输出,依然保存在文件中。

即为表示无论是正确输出还是错误输出都重定向保存在文件中。

td@td-Lenovo-IdeaPad-Y410P:~$ ls >> test.log 2>&1
td@td-Lenovo-IdeaPad-Y410P:~$ cat test
cat: test: 没有那个文件或目录
td@td-Lenovo-IdeaPad-Y410P:~$ cat test.log
test.log
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
td@td-Lenovo-IdeaPad-Y410P:~$ ls cangls >> test.log 2>&1
td@td-Lenovo-IdeaPad-Y410P:~$ cat
^C
td@td-Lenovo-IdeaPad-Y410P:~$ cat test.log
test.log
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
ls: 无法访问'cangls': 没有那个文件或目录

2)命令  &>文件

     命令  &>>文件

td@td-Lenovo-IdeaPad-Y410P:~$ rm test.log
td@td-Lenovo-IdeaPad-Y410P:~$ ls &>>test.log
td@td-Lenovo-IdeaPad-Y410P:~$ cat test.log
test.log
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
td@td-Lenovo-IdeaPad-Y410P:~$ ls cangls &>>test.log
td@td-Lenovo-IdeaPad-Y410P:~$ cat test.log
test.log
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
ls: 无法访问'cangls': 没有那个文件或目录

3)命令>文件1   2>文件2

     命令>>文件1  2>>文件2

将正确出处输入文件1  将错误输出输入文件2,即为单独保存。

td@td-Lenovo-IdeaPad-Y410P:~$ ls >>test1.log 2>>test2.log
td@td-Lenovo-IdeaPad-Y410P:~$ ls cangls >>test1.log 2>>test2.log
td@td-Lenovo-IdeaPad-Y410P:~$ cat test1.log
test1.log
test2.log
Untitled 2.odt
workspace
公共的
模板
视频
图片
文档
下载
音乐
桌面
td@td-Lenovo-IdeaPad-Y410P:~$ cat test2.log
ls: 无法访问'cangls': 没有那个文件或目录
原文地址:https://www.cnblogs.com/td15980891505/p/6193940.html