重定向

# 标准覆盖输出重定向
[root@localhost ~]# echo zls>/tmp/1.txt   认识   一串
[root@localhost ~]# cat /tmp/1.txt
zls

[root@localhost ~]# echo 321>/tmp/2.txt   因为不认识文件描述符,所以是输入 空值
[root@localhost ~]# cat /tmp/2.txt               空值
 
[root@localhost ~]# echo zls1>/tmp/3.txt  认识   一串
[root@localhost ~]# cat /tmp/3.txt
zls1

[root@oldboy ~]# echo 123 >2. | echo 456 >2. | echo 789 >2.   文件夹管道
[root@oldboy ~]# cat 2.
123
[root@oldboy ~]# echo 123 >>2. | echo 456 >>2. | echo 789 >>2.
[root@oldboy ~]# cat 2.   管道符左边的标准输出--管道符右边的标准输入
456
789
123

[root@oldboy ~]# >2.     清空文件 ,不能清空目录
[root@oldboy ~]# >>2.    内容不变
[root@oldboy ~]# cat >2.  写入--覆盖(不写就是清空)
[root@oldboy ~]# cat>2.   清空
^C
[root@oldboy ~]# echo>2.   什么都没写入
[root@oldboy ~]# echo >2.  写入空行
[root@oldboy ~]# cat >2.
空行

# 标准追加输出重定向
[root@localhost ~]# echo 456 >> /var/log/messages  (不能用> ,用了文件被截断)
[root@localhost ~]# echo 123 >> /var/log/messages

# 错误输出重定向
[cdx@localhost ~]$ find /etc/ -type d   查找/etc 下的 所有目录

[cdx@localhost ~]$ curl http://www.baidu.com > zls.txt
[cdx@localhost ~]$ curl http://www.baidu.com &> zls.txt  &(都,包括123..)
小心,有些命令输出,既不是标准输出又不是错误输出,这时候

# 将标准输出和错误输出都输出到相同的文件中
[cdx@localhost ~]$ find /etc/ -type d > /tmp/100.txt 2>&1  过程和效率不一样,错误输出 输出到1里面,跟着1一起输出到文件里

[cdx@localhost ~]$ find /etc/ -type d &> /tmp/101.txt

[cdx@localhost ~]$ diff

#将错误输出,重定向到黑洞[root@localhost ~]# ls / /ooo 2>/dev/null
ls 可以同时查看两个目录

[root@localhost ~]# mail -s "$(date +%F-%T)_test" 133411023@qq.com < /etc/passwd
[root@localhost ~]# mail xxx (主题)
.                            (结束)
[root@localhost ~]# echo $((date +%F-%T)_test) | mail

[root@oldboy opt]# find . -perm /000 | wc -l  (提示没用被计算在内)
find: warning: you have specified a mode pattern /000 (which is equivalent to /000). The meaning of -perm /000 has now been changed to be consistent with -perm -000; that is, while it used to match no files, it now matches all files.
31  (统计 -1),显示了当前目录 '.'
[root@oldboy opt]# find . -perm /000  2>/dev/null |wc -l
31
[root@oldboy opt]# find . -perm /000 | wc -l &>/dev/null 
find: warning: you have specified a mode pattern /000 (which is equivalent to /000). The meaning of -perm /000 has now been changed to be consistent with -perm -000; that is, while it used to match no files, it now matches all files. (统计被丢到了黑洞),
管道符只会处理正确的结果,不会处理错误的结果,如果管道符左边有错误信息的话,那么会直接输出,这里的提示归为错误信息,
[root@oldboy opt]# findfind . -perm /000 | wc -l 2>/dev/null 
find: warning: you have specified a mode pattern /000 (which is equivalent to /000). The meaning of -perm /000 has now been changed to be consistent with -perm -000; that is, while it used to match no files, it now matches all files.
31 (wc -l) 的错误信息被丢到黑洞,管道符左边的错误信息正常输出,把(wc -l)的错误信息丢到黑洞
原文地址:https://www.cnblogs.com/syy1757528181/p/12813428.html