一、重定向概述
1.重定向的含義
將原本要輸出到屏幕的數(shù)據(jù)信息,重新定向到某個(gè)指定的文件中或目錄下的文件中
2.重定向的用途
1.當(dāng)屏幕輸出的信息很重要,而且希望保存重要的信息時(shí);
2.后臺(tái)執(zhí)行中的程序,不希望他干擾屏幕正常的輸出結(jié)果時(shí);
3.系統(tǒng)的例行命令, 例如定時(shí)任務(wù)的執(zhí)行結(jié)果,希望可以存下來(lái)時(shí);
4.一些執(zhí)行命令,我們已經(jīng)知道他可能出現(xiàn)錯(cuò)誤信息, 想將他直接丟棄時(shí);
5.錯(cuò)誤日志與正確日志需要分別輸出至不同的文件保存時(shí)。
二、標(biāo)準(zhǔn)輸入與輸出
三個(gè)標(biāo)準(zhǔn)文件,分別是標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、錯(cuò)誤輸出
| 名稱 | 文件描述符 | 作用 |
|---|---|---|
| 標(biāo)準(zhǔn)輸入(STDIN) | 0 | 默認(rèn)是鍵盤,也可以是文件或其他命令的輸出。 |
| 標(biāo)準(zhǔn)輸出(STDOUT) | 1 | 默認(rèn)輸出到屏幕。 |
| 錯(cuò)誤輸出(STDERR) | 2 | 默認(rèn)輸出到屏幕。 |
進(jìn)程將從標(biāo)準(zhǔn)輸入中得到數(shù)據(jù),將正常輸出打印至屏幕終端,將錯(cuò)誤的輸出信息也打印至屏幕終端。
示例圖:
1)以 cat 命令為例, cat 命令的功能是從命令行給出的文件中讀取數(shù)據(jù),并將這些數(shù)據(jù)直接送到標(biāo)準(zhǔn)輸出。
會(huì)把文件/etc/passwd的內(nèi)容輸出顯示到屏幕上
[root@oldboy ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
cui-01:x:1000:1000::/home/cui-01:/bin/bash
cui:x:1001:1001::/home/cui:/bin/bash
cui-20:x:1002:1003::/home/cui-20:/bin/bash
cui-21:x:1003:1004::/home/cui-21:/bin/bash
oldboy:x:1004:1006::/home/oldboy:/bin/bash
oldgirl:x:1005:1007::/home/oldgirl:/bin/bash
[root@oldboy ~]#
1.輸出重定向
輸出重定向,改變輸出內(nèi)容的位置。輸出重定向有如下幾種方式,如表格所示
| 各種類型 | 操作符號(hào) | 用途 |
|---|---|---|
| 1、標(biāo)準(zhǔn)覆蓋輸出重定向 | > | 將程序輸出的正確結(jié)果輸出到指定的文件中,會(huì)覆蓋文件原有的內(nèi)容 |
| 2、標(biāo)準(zhǔn)追加輸出重定向 | >> | 將程序輸出的正確結(jié)果以追加的方式輸出到指定文件,不會(huì)覆蓋原有文件 |
| 3、錯(cuò)誤覆蓋輸出重定向 | 2> | 將程序的錯(cuò)誤結(jié)果輸出到執(zhí)行的文件中,會(huì)覆蓋文件原有的內(nèi)容 |
| 4、錯(cuò)誤追加輸出重定向 | 2>> | 將程序輸出的錯(cuò)誤結(jié)果以追加的方式輸出到指定文件,不會(huì)覆蓋原有文件 |
| 5、標(biāo)準(zhǔn)輸入重定向 | << | 將命令中接收輸入的途徑由默認(rèn)的鍵盤更改為指定的文件或命令 |
| 6、混合輸出 (覆蓋) | &> | 將輸出正確和錯(cuò)誤的結(jié)果指定到同一文件中(/dev/null ) |
| 7、混合輸出 (追加) | &>> | /將輸出正確和錯(cuò)誤的結(jié)果追加到同一文件中,不覆蓋原有內(nèi)容tmp/t.log |
1)例1: 標(biāo)準(zhǔn)輸出重定向,每次都會(huì)覆蓋文件 (>或1>)

- 標(biāo)準(zhǔn)輸出重定向, 先清空,后寫入, 如果文件不存在則創(chuàng)建
[root@oldboy ~]# cat 1.txt 查看原文件1.txt中的內(nèi)容
123
321
4568988
[root@oldboy ~]# ifconfig ens33 > 1.txt
[root@oldboy ~]# cat 1.txt
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::a863:247c:e43:883a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:66:98:7e txqueuelen 1000 (Ethernet)
RX packets 634 bytes 57895 (56.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 480 bytes 57934 (56.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@oldboy ~]#
2)例2: 標(biāo)準(zhǔn)輸出重定向,往文件的尾部在添加內(nèi)容 (>>)

- 標(biāo)準(zhǔn)追加輸出重定向, 向配置文件末尾追加內(nèi)容
[root@oldboy ~]# echo "崔鑫鵬 你好!" >> 1.txt
[root@oldboy ~]# cat 1.txt
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::a863:247c:e43:883a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:66:98:7e txqueuelen 1000 (Ethernet)
RX packets 634 bytes 57895 (56.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 480 bytes 57934 (56.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
崔鑫鵬 你好!
[root@oldboy ~]#
3)例3: 錯(cuò)誤輸出重定向(2>)和追加(2>>)

1、錯(cuò)誤輸出至一個(gè)文件(2>)
[cui-01@oldboy ~]$ ls
1.txt 22
[cui-01@oldboy ~]$ cd 1.txt
-bash: cd: 1.txt: Not a directory
[cui-01@oldboy ~]$ cd 1.txt 2> cuowu.txt
[cui-01@oldboy ~]$ cat cuowu.txt
-bash: cd: 1.txt: Not a directory
2、錯(cuò)誤輸出追加至同一文件(2>>)
[cui-01@oldboy ~]$ cd 22
-bash: cd: 22: Not a directory
[cui-01@oldboy ~]$ cd 22 2>> cuowu.txt
[cui-01@oldboy ~]$ cat cuowu.txt
-bash: cd: 1.txt: Not a directory
-bash: cd: 22: Not a directory
4)例4: 正確和錯(cuò)誤都輸入到相同位置

[cui-01@oldboy ~]$ find /etc/ &> wode.txt
[cui-01@oldboy ~]$ cat wode.txt
/etc/vmware-tools/GuestProxyData/server/cert.pem
/etc/vmware-tools/GuestProxyData/trusted
find: ‘/etc/vmware-tools/GuestProxyData/trusted’: Permission denied
/etc/audisp
find: ‘/etc/audisp’: Permission denied
/etc/audit
find: ‘/etc/audit’: Permission denied
/etc/postfix
/etc/postfix/access
...
5)例5: 重定向到空設(shè)備/dev/null

#將產(chǎn)生的任何數(shù)據(jù)放入黑洞設(shè)備,則視為丟棄。
[cui-01@oldboy ~]$ find /etc/ 2> /dev/null
/etc/.updated
/etc/aliases.db
/etc/vimrc
/etc/wgetrc
/etc/resolv.conf.B6EY5Z
...
6)例6: 腳本中使用重定向 (了解即可)
[root@oldboy~]# vim ping.sh
ping -c1 10.0.0.1
if [ $? -eq 0 ];then
echo "10.0.0.1 is up."
else
echo "10.0.0.1 is down."
fi
[root@oldboy~]# chmod +x ping.sh
[root@oldboy ~]# ./ping.sh
#改進(jìn)后版
[root@oldboy ~]# vim ping.sh
ping -c1 10.0.0.1 &>/dev/null
if [ $? -eq 0 ];then
echo "10.0.0.1 is up."
else
echo "10.0.0.1 is down."
fi
2.輸入重定向
輸入重定向,即原本從鍵盤等上獲得的輸入信息,重定向由命令的輸出作為輸入。< 等價(jià) 0<
1)例1: 從文件中讀入輸入的操作
1.沒(méi)有改變輸入的方向,默認(rèn)鍵盤
[root@oldboy ~]# mail alice
Subject: hello
1111
2222
3333
. 結(jié)束
EOT
2.檢查是否收到郵件
[root@oldboy ~]# su - alice
[root@oldboy ~]# mail
3.輸入重定向,來(lái)自于文件
[root@oldboy ~]# mail -s "test01" alice < /etc/hosts
2)例2: 利用重定向建立多行數(shù)據(jù)的文件*
手動(dòng)執(zhí)行 shell 命令
[root@oldboy ~]# echo "111" > file1.txt
[root@oldboy ~]# cat file1.txt
111
[root@oldboy~]# cat >file2.txt
111
222
333
^D (Ctrl+D)
[root@xuliangwei ~]# cat >>file3.txt
aaa
bbb
ccc
^D (Ctrl+D)
3)例3: 腳本中打印菜單的一種使用方法。
[root@oldboy ~]# vim vm.sh
cat <<-EOF
+------------------- --- ---- --- -- - + |
| ====================== |
| 虛擬機(jī)基本管理 v5.0 |
| by xuliangwei |
| ====================== |
| 1\. 安裝 KVM |
| 2\. 安裝或重置 CentOS-6.9 |
| 3\. 安裝或重置 CentOS-7.4 |
| 5\. 安裝或重置 Windows-7 |
| 6\. 刪除所有虛擬機(jī) |
| q. 退出管理程序 |
+------------------- --- ---- --- +
EOF
4)例4: 兩條命令同時(shí)重定向,后臺(tái)執(zhí)行符號(hào):&
[root@oldboy~]# ls; date &>/dev/null
[root@oldboy~]# ls &>/dev/null; date &>/dev/null
[root@oldboy ~]# (ls; date) &>/dev/null
#后臺(tái)執(zhí)行
[root@oldboy ~]# (while ; do date; sleep 2; done) &
[1] 6378
[root@oldboy~]# (while ; do date; sleep 2; done) &>date.txt &
三、進(jìn)程管道技術(shù)
1.管道的含義
管道操作符號(hào) "|" ,主要用來(lái)連接左右兩個(gè)命令, 將左側(cè)的命令的標(biāo)準(zhǔn)輸出, 交給右側(cè)命令的標(biāo)準(zhǔn)輸入, 無(wú)法傳遞標(biāo)準(zhǔn)錯(cuò)誤輸出至后者命令
- 管道的作用:管道命令符能讓大家能進(jìn)一步掌握命令之間的搭配使用方法,進(jìn)一步提高命令輸出值的處理效率。
示例圖:

2.管道使用
1)例1: 將/etc/passwd 中的用戶按 UID 大小排序
[root@oldboy~]# sort -t":" -k3 -n /etc/passwd
[root@oldboy ~]# sort -t":" -k3 -n /etc/passwd -r
[root@oldboy~]# sort -t":" -k3 -n /etc/passwd |head
2)例2: 統(tǒng)計(jì)當(dāng)前/etc/passwd 中用戶使用的 shell 類型
[root@oldboy ~]# awk -F: '{print $7}' /etc/passwd
[root@oldboy ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@oldboy ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@oldboy ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
4)例4: 打印當(dāng)前所有 IP
[root@oldboy ~]# ifconfig|grep "inet "|awk '{print $2}'
10.0.0.200
127.0.0.1
5)例5: 打印根分區(qū)已用空間的百分比(僅打印數(shù)字)
[root@oldboy~]# df |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'
6
3.管道中的tee技術(shù)
1)tee 的作用:將左邊命令的標(biāo)準(zhǔn)輸出保存至文件或打印至終端, 但不影 響管道數(shù)據(jù)的一個(gè)傳遞
示例圖:

2)tee的使用
[root@oldboy ~]# ifconfig|grep "inet "|tee ip.txt|awk '{print $2}'
10.0.0.200
127.0.0.1
[root@oldboy ~]# cat ip.txt
inet 10.0.0.200 netmask 255.255.255.0 broadcast 10.0.0.255
inet 127.0.0.1 netmask 255.0.0.0
[root@oldboy ~]#
3)重定向與 tee 有他們?cè)谑褂眠^(guò)程中有什么區(qū)別
1、直接將內(nèi)容寫入date.txt文件中
[root@oldboy ~]# date > date.txt
2、命令執(zhí)行會(huì)輸出至屏幕,但會(huì)同時(shí)保存一份至date.txt文件中
[root@oldboy ~]# date |tee date.txt
3)xargs參數(shù)傳遞,主要讓一些不支持管道的命令可以使用管道技術(shù)
[root@oldboy ~]# which cat|xargs ls- l
[root@oldboy ~]# ls |xargs rm -fv
總結(jié)
今天我們主要學(xué)了標(biāo)準(zhǔn)輸入輸出的重定向,重定向符號(hào)有哪些,不同的符號(hào)運(yùn)用在什么地方,怎樣么樣運(yùn)用這些重定向的符號(hào);學(xué)習(xí)了管道技術(shù),主要用來(lái)多命令的結(jié)合使用,輸出我們想要的結(jié)果,管道的應(yīng)用無(wú)處不在,減縮命令的使用,很好用,在管道技術(shù)中有個(gè)tee的運(yùn)用,他的功能可以理解為在運(yùn)用管道技術(shù)時(shí)將我們想保留的中間文件保存到文件中。方便我們查看。