linux 从入门到跑路–重定向 管道

linux 从入门到跑路

重定向 管道

Linux给程序提供三种I/O设备
标准输入(STDIN)-0默认接受来自键盘的输入
标准输出(STDOUT)-1默认输出到终端窗口
标准错误(STDERR)-2默认输出到终端窗口
I/O重定向:改变默认位置

重定向

重定向会覆盖原文件内容

> 把STDOUT重定向到文件
2> 把STDERR重定向到文件
&> 把所有输出重定向到文件
set –C 禁止将内容覆盖已有文件,但可追加
set +C 允许覆盖

>| file 强制覆盖

#多输出实例
[root@localhost ~]# ll /mytext/
总用量 0
-rwxrwxrwx. 1 root root 0 7月  22 03:52 1
-rw-r--r--. 1 root root 0 7月  22 03:52 10
-rwxrwxrwx. 1 root root 0 7月  22 03:52 2
-rwxrwxrwx. 1 root root 0 7月  22 03:52 3
-rwxrwxrwx. 1 root root 0 7月  22 03:52 4
-rwxrwxrwx. 1 root root 0 7月  22 03:52 5
-rwxrwx---. 1 root root 0 7月  22 03:52 6
-rwxrwx---. 1 root root 0 7月  22 03:52 7
-rwxrwx---. 1 root root 0 7月  22 03:52 8
-rwxrwx---. 1 root root 0 7月  22 03:52 9

[francis@localhost ~]$ cp -vr /mytext/ /app/hello  1>correct.txt 2>error.txt
[francis@localhost ~]$ cat correct.txt 
"/mytext/" -> "/app/hello/mytext"
"/mytext/1" -> "/app/hello/mytext/1"
"/mytext/2" -> "/app/hello/mytext/2"
"/mytext/3" -> "/app/hello/mytext/3"
"/mytext/4" -> "/app/hello/mytext/4"
"/mytext/5" -> "/app/hello/mytext/5"
"/mytext/6" -> "/app/hello/mytext/6"
"/mytext/7" -> "/app/hello/mytext/7"
"/mytext/8" -> "/app/hello/mytext/8"
"/mytext/9" -> "/app/hello/mytext/9"
"/mytext/10" -> "/app/hello/mytext/10"
[francis@localhost ~]$ cat error.txt 
cp: 无法打开"/mytext/6" 读取数据: 权限不够
cp: 无法打开"/mytext/7" 读取数据: 权限不够
cp: 无法打开"/mytext/8" 读取数据: 权限不够
cp: 无法打开"/mytext/9" 读取数据: 权限不够
[francis@localhost ~]$ cp -vr /mytext/ /app/hello 1>correct.txt 2>error.txt &> both.txt
[francis@localhost ~]$ cat both.txt 
"/mytext/1" -> "/app/hello/mytext/1"
"/mytext/2" -> "/app/hello/mytext/2"
"/mytext/3" -> "/app/hello/mytext/3"
"/mytext/4" -> "/app/hello/mytext/4"
"/mytext/5" -> "/app/hello/mytext/5"
"/mytext/6" -> "/app/hello/mytext/6"
cp: 无法打开"/mytext/6" 读取数据: 权限不够
"/mytext/7" -> "/app/hello/mytext/7"
cp: 无法打开"/mytext/7" 读取数据: 权限不够
"/mytext/8" -> "/app/hello/mytext/8"
cp: 无法打开"/mytext/8" 读取数据: 权限不够
"/mytext/9" -> "/app/hello/mytext/9"
cp: 无法打开"/mytext/9" 读取数据: 权限不够
"/mytext/10" -> "/app/hello/mytext/10"

追加重定向
>> 原有内容基础上,追加内容

命令1 | tee[-a ] 文件名| 命令2
把命令1的STDOUT保存在文件中,做为命令2的输入
-a 追加

猜你喜欢

转载自www.cnblogs.com/FrancisDrakeK/p/9347745.html