linux 中標准輸出重定向、標准錯誤輸出重定向、追加重定向


1、標准輸出重定向:1>,  1可以省略

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt -rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l a.txt 1> aaa ## 標准輸出
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt

 

2、標准錯誤輸出重定向: 2>, 2不可以省略

root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# ls -l b.txt      ## b.txt不存在
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l b.txt 1> bbb111    ## 標准輸出重定向
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l b.txt 2> bbb222    ## 標准錯誤輸出重定向
root@ubuntu01:/home/test# ls
aaa  a.txt  bbb111  bbb222
root@ubuntu01:/home/test# cat bbb111
root@ubuntu01:/home/test# cat bbb222     ## 只有標准錯誤輸出重定向可以寫入標准錯誤信息
ls: cannot access 'b.txt': No such file or directory

 

3、 標准輸出、標准錯誤輸出重定向: &>,  相當於包含同時包含 1>  和  2>。

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt -rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt &> aaa
root@ubuntu01:/home/test# ls -l b.txt &> bbb
root@ubuntu01:/home/test# cat aaa -rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# cat bbb
ls: cannot access 'b.txt': No such file or directory

 

4、標准輸出、標准錯誤輸出追加重定向  >> file 2>&1或者  &>> file, 兩者等價

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt >> aaa 2>&1    ## 標准輸出、標准錯誤輸出追加重定向
root@ubuntu01:/home/test# ls -l b.txt >> aaa 2>&1
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
ls: cannot access 'b.txt': No such file or directory

 

root@ubuntu01:/home/test# ls
a.txt
root@ubuntu01:/home/test# ls -l a.txt
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
root@ubuntu01:/home/test# ls -l b.txt
ls: cannot access 'b.txt': No such file or directory
root@ubuntu01:/home/test# ls -l a.txt &>> aaa     ## 標准輸出、標准錯誤輸出追加重定向
root@ubuntu01:/home/test# ls -l b.txt &>> aaa
root@ubuntu01:/home/test# ls
aaa  a.txt
root@ubuntu01:/home/test# cat aaa
-rw-r--r-- 1 root root 6 3月  27 19:52 a.txt
ls: cannot access 'b.txt': No such file or directory

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM