8.5-输入输出重定向

输入重定向用于改变命令的输入,输出重定向用于改变命令的输出。输出重定向更为常用,它经常用于将命令的结果输入到文件中,而不是屏幕上。输入重定向的命令是 < ,输出重定向的命令是 > 。另外还有错误重定向命令 2> 以及追加重定向命令 >>

> 输出重定向
把输出的内容重定向文件里,会覆盖之前原有内容

[root@evan-01 ~]# cat 1.txt 
123
[root@evan-01 ~]# echo "12345678" > 1.txt 
[root@evan-01 ~]# cat 1.txt 
12345678
[root@evan-01 ~]#

>> 追加重定向
把要追加的内容追加到文件里

[root@evan-01 ~]# cat 1.txt 
12345678
[root@evan-01 ~]# echo "654321" >> 1.txt 
[root@evan-01 ~]# cat 1.txt 
12345678
654321
[root@evan-01 ~]# 

2> 错误重定向
会把错误信息添加到文件里

[root@evan-01 ~]# hahaha
-bash: hahaha: command not found
[root@evan-01 ~]# hahaha 2> 2.txt
[root@evan-01 ~]# cat 2.txt 
-bash: hahaha: command not found
[root@evan-01 ~]#

2>> 错误追加重定向
会把错误信息追加到文件里

[root@evan-01 ~]# cat 2.txt 
-bash: hahaha: command not found
[root@evan-01 ~]# heiheihei 2>> 2.txt
[root@evan-01 ~]# cat 2.txt 
-bash: hahaha: command not found
-bash: heiheihei: command not found
[root@evan-01 ~]#

>+2> 输出重定向和错误重定向合起来(也支持追加错误重定向)>+2> == &>

[root@evan-01 ~]# ls [12].txt aaa.txt &> a.txt 
[root@evan-01 ~]# cat a.txt
ls: cannot access aaa.txt: No such file or directory
1.txt
2.txt
[root@evan-01 ~]#

输出重定向和错误重定向分开

[root@evan-01 ~]# ls [12].txt aaa.txt >1.txt 2> a.txt
[root@evan-01 ~]# cat 1.txt
1.txt
2.txt
[root@evan-01 ~]# cat a.txt 
ls: cannot access aaa.txt: No such file or directory
[root@evan-01 ~]# 

< 输入重定向(极少用,了解)
把右边的内容,输入的左边的命令去执行

[root@evan-01 ~]# wc -l < 1.txt
2
[root@evan-01 ~]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章