最近學的linux重定向命令

標準輸出命令字符 其實是 1> ,1可以省略
兩個終端連接在一臺主機上,終端分別爲 1 2
假設在/data下有文件F1.log
1:ls > 2
2:F1.log
主要是將內容重定向至文件中,如果沒有文件,就生成,有就覆蓋
ls > /data/f1
cat /data/f1
F1.log
ls >> /data/f1 當不想覆蓋文件時,再加一個 >
cat /data/f1
F1.log
F1.log
f1

set -C 禁止覆蓋
| 加入| 符號,在set -C 下也可以強制覆蓋
set +C 覆蓋

2> 標準錯誤命 令字符
當錯誤信息使用了標準輸出字符時,會清空文件內容
[root@Centos7 ~]#cat /data/ls.out
anaconda-ks.cfg
Desktop
Documents
Downloads
f1link
f1link2
file
initial-setup-ks.cfg
Music
Pictures
Public
Templates
Videos
[root@Centos7 ~]#errcmd > /data/ls.out
bash: errcmd: command not found...
[root@Centos7 ~]#cat /data/ls.out
這一段爲無內容
下一段是標準錯誤的命令
[root@Centos7 ~]#ls > ls.out
[root@Centos7 ~]#cat ls.out
anaconda-ks.cfg
Desktop
Documents
Downloads
f1link
f1link2
file
initial-setup-ks.cfg
ls.out
Music
Pictures
Public
Templates
Videos
[root@Centos7 ~]#errcmd 2> ls.out
[root@Centos7 ~]#cat ls.out
bash: errcmd: command not found...
同樣,不想覆蓋文件,多加一個> 即是 2>>

可以同時執行兩個命令
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out > /data/3.log 2> /data/4.log
[root@Centos7 ~]#cat /data/3.log
/etc/centos-release
[root@Centos7 ~]#cat /data/4.log
ls: cannot access /etc/f1.out: No such file or directory

將兩個信息放在一個文件的兩個寫法
第一個寫法更爲方便,時間比較後出現,相對較新
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out &> /data/all.log
[root@Centos7 ~]#cat /data/all.log
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release
第二個是傳統寫法
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out > /data/all2.log 2>&1
[root@Centos7 ~]#cat /data/all2.log
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release

tr 標準輸入的一種
tr 可以做字符串轉換的命令
tr 'a-z' 'A-Z' 將輸入的字母小寫的輸出爲大寫的

< 標準輸入的重定向
cat f1
abcd
tr 'a-z' 'A-Z' < f1 (即將f1的內容輸入執行前面的tr命令
ABCD (僅僅只是顯示,可以加入 > 命令將顯示內容放在文件
tr 'a-z' 'A-Z' < f1 > f1 (這樣會清空文件f1,應該將內容重定向至另一個文件中f2
tr 'a-z' 'A-Z' < f1 > f2
cat f2
ABCD

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章