輸入重定向用於改變命令的輸入,輸出重定向用於改變命令的輸出。輸出重定向更爲常用,它經常用於將命令的結果輸入到文件中,而不是屏幕上。輸入重定向的命令是 < ,輸出重定向的命令是 > 。另外還有錯誤重定向命令 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 ~]#