1.重定向
1.1標準輸入重定向
標準輸入 用數(shù)字“0”表示 默認接受來自鍵盤的輸入
“<” 把輸入重定向給程序和命令
[root@centos7.3 ~]#cat a.txt #查看文件內(nèi)容
total 4
-rw-r--r--. 1 root root 201 May 29 12:47 acl.txt #原文件內(nèi)容
drwxr-s---+ 2 root g1 6 May 29 12:34 dir
[root@centos7.3 ~]#tr 'a-z' 'A-Z' < a.txt > d.txt 把a.txt里的文件重定向到tr命令
在把輸出結(jié)果重定向到d.txt
[root@centos7.3 ~]#cat d.txt #查看d.txt文件
TOTAL 4 #原來a.txt里的內(nèi)容全部被tr命令變成大寫再重定向到d.txt文件中
-RW-R--R--. 1 ROOT ROOT 201 MAY 29 12:47 ACL.TXT
DRWXR-S---+ 2 ROOT G1 6 MAY 29 12:34 DIR
1.2 標準輸出重定向
標準輸出 用數(shù)字“1”表示 默認輸出到終端窗口
“>”把標準輸出定重向到文件或者設備
[root@centos7.3 ~]#ls /home/ > a.txt #把ls命令的標準輸出重定向到 a.txt 文件中
[root@centos7.3 ~]#cat a.txt #查看a.txt文件的內(nèi)容
admin
alece
tom
但是“>”重定向到文件會覆蓋文件中原有的內(nèi)容
[root@centos7.3 ~]#ls /app/ > a.txt #把/app目錄下的文件列表重定向到a.txt
[root@centos7.3 ~]#cat a.txt #再來看一下a.txt文件
acl.txt #里面的內(nèi)容已經(jīng)被覆蓋了
dir
[root@centos7.3 ~]#
所以如果我們要往文件里添加內(nèi)容不覆蓋的話可以用“>>”把標準輸出重定向追加到文件中
[root@centos7.3 ~]#ll /root/ >> a.txt
[root@centos7.3 ~]#cat a.txt
acl.txt
dir #這兩行a.txt文件中原有的內(nèi)容
total 16
-rw-------. 1 root root 1892 May 24 17:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 12 May 29 14:51 a.txt
drwxr-xr-x. 2 root root 6 May 24 18:08 Desktop
drwxr-xr-x. 2 root root 6 May 24 18:08 Documents
drwxr-xr-x. 2 root root 6 May 24 18:08 Downloads
-rw-r--r--. 1 root root 1923 May 24 18:08 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 May 24 18:08 Music
drwxr-xr-x. 2 root root 6 May 24 18:08 Pictures
drwxr-xr-x. 2 root root 6 May 24 18:08 Public
drwxr-xr-x. 2 root root 6 May 24 18:08 Templates
drwxr-xr-x. 2 root root 6 May 24 18:08 Videos
#這是a.txt文件中原有的內(nèi)容這些是我們把/root目錄下的文件列表追加進來的內(nèi)容
[root@centos7.3 ~]#
1.3標準錯誤重定向
標準錯誤 用數(shù)字“2”表示 默認輸出到終端窗口
“2>”把標準錯誤重定向到文件或者設備
[root@centos7.3 ~]#ls /aaa 2> b.txt #把標準錯誤重定向到b.txt文件中
[root@centos7.3 ~]#cat b.txt #查看b.txt文件中的內(nèi)容
ls: cannot access /aaa: No such file or directory #提示找不到/aaa目錄
“2>>”將一個標準錯誤輸出重定向追加到文件中
[root@centos7.3 ~]#ls /bbb 2>> b.txt #把ls /bbb的錯誤輸出重定向追加到b.txt文件中
[root@centos7.3 ~]#cat b.txt #查看追加結(jié)果
ls: cannot access /aaa: No such file or directory #這是上一條/aaa的報錯信息
ls: cannot access /bbb: No such file or directory #這是這一次追加的信息
1.4
我們想把標準輸出和標準錯誤重定向到一個文件或者設備的時候就可以用“&>”把所有的信息都重定向到一個地方
[root@centos7.3 ~]#ls /app/ /ccc &> c.txt #查看/app和/ccc目錄,把所有的輸出都重定向到c.txt文件中
[root@centos7.3 ~]#cat c.txt #查看c.txt文件中的結(jié)果
ls: cannot access /ccc: No such file or directory #這是/ccc的報錯提示標準錯誤
/app/: #這是標準輸出
acl.txt
dir
“>&”這個也可以把所有輸出都重定向到一個文件或者設備和上一條命令的效果一樣
[root@centos7.3 ~]#ls /app/ /ccc >& c.txt
“> 2>&1”這個也可以實現(xiàn)同樣的效果
[root@centos7.3 ~]#ls /app/ /cc > c.txt 2>&1
[root@centos7.3 ~]#cat c.txt
ls: cannot access /cc: No such file or directory
/app/:
acl.txt
dir
如果要追加內(nèi)容可以用“>> 2>&1”來實現(xiàn)
[root@centos7.3 ~]#ls /home /c >> c.txt 2>&1 #查看/home和/c目錄下的文件列表,把所有輸出重定向到c.txt
[root@centos7.3 ~]#cat c.txt
ls: cannot access /cc: No such file or directory #這是幾行是原有的內(nèi)容
/app/:
acl.txt
dir
ls: cannot access /c: No such file or directory #這個幾行是我們剛剛追加進來的內(nèi)容
/home:
admin
alece
tom
“&>>”用這個字符也可以把所有的輸出追加到文件
[root@centos7.3 ~]#ls /home /d &>> c.txt
[root@centos7.3 ~]#cat c.txt
ls: cannot access /c: No such file or directory #原來的內(nèi)容
/home:
admin
alece
tom
ls: cannot access /d: No such file or directory #新追加的內(nèi)容
/home:
admin
alece
tom
2.管道
管道符使用“|”來表示,用來把命令連接起來。
[root@centos7.3 ~]#ls /app/ | tr 'a-z' 'A-Z' #查看/app下的文件列表,再輸出結(jié)果用管道傳給tr命令處理
ACL.TXT #顯示在終端的結(jié)果變成了大寫字母
DIR
“|”管道符可以用來保存不同階段的輸出信息,可以同時查看和保存輸出信息