重定向与管道
----------小命令大作为
>:重定向标准输入出定向命令
用的最多的就是向某个文件中写入内容
[[email protected] ~]# echo "Hello,world">1.txt
[[email protected] ~]# cat 1.txt
Hello,world
把某个命令的结果重定向到某个文件中
[[email protected] ~]# cat /etc/passwd >2.txt
[[email protected] ~]# cat 2.txt
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
>>:追加标准输出重定向
[[email protected] ~]# echo "王鹏健,java高级工程师" >> 1.txt
[[email protected] ~]# cat 1.txt
Hello,world
王鹏健,java高级工程师
如果不用追加重定向的话就会将文件原来的内容替换掉,如
[[email protected] ~]# echo "马强,在北京" >1.txt
[[email protected] ~]# cat 1.txt
马强,在北京
[[email protected] ~]#
2>:重定向错误标准输出
如果命令有错误,也会执行命令,并将错误信息输出在文本里面,如:
[[email protected] ~]# ls /etc/pas 2> 1.txt
[[email protected] ~]# cat 1.txt
ls: cannot access /etc/pas: No such file or directory
[[email protected] ~]#
2>>:追加重定向错误标准输出
将命令错误信息追加到文本里面
[[email protected] ~]# echo "马强,在北京" >1.txt
[[email protected] ~]# ls /etc/pas 2>>1.txt
[[email protected] ~]# cat 1.txt
马强,在北京
ls: cannot access /etc/pas: No such file or directory
&>:将重定向标准输出或者错误重定向到同一文件夹内
错误输出
[[email protected] ~]# ls /var2 &> 1.txt
[[email protected] ~]# cat 1.txt
ls: cannot access /var2: No such file or directory
[[email protected] ~]#
正确输出
[[email protected] ~]# ls /var &> 1.txt
[[email protected] ~]# cat 1.txt
account
cache
crash
cvs
db
empty
games
gdm
lib
local
lock
log
nis
opt
preserve
run
spool
tmp
www
yp
[[email protected] ~]#
<:输入重定向
可以将结果作为另一个命令的使用,如
将/etc/passwd 文件里面的内容全部转换成大写
tr ‘a-z’ ‘A-Z’ </etc/passwd
[[email protected] ~]# tr ‘a-z‘ ‘A-Z‘ </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
UUCP:X:10:14:UUCP:/VAR/SPOOL/UUCP:/SBIN/NOLOGIN
OPERATOR:X:11:0:OPERATOR:/ROOT:/SBIN/NOLOGIN
GAMES:X:12:100:GAMES:/USR/GAMES:/SBIN/NOLOGIN
GOPHER:X:13:30:GOPHER:/VAR/GOPHER:/SBIN/NOLOGIN
FTP:X:14:50:FTP USER:/VAR/FTP:/SBIN/NOLOGIN
NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGIN
DBUS:X:81:81:SYSTEM MESSAGE BUS:/:/SBIN/NOLOGIN
USBMUXD:X:113:113:USBMUXD USER:/:/SBIN/NOLOGIN
VCSA:X:69:69:VIRTUAL CONSOLE MEMORY OWNER:/DEV:/SBIN/NOLOGIN
RTKIT:X:499:497:REALTIMEKIT:/PROC:/SBIN/NOLOGIN
AVAHI-AUTOIPD:X:170:170:AVAHI IPV4LL STACK:/VAR/LIB/AVAHI-AUTOIPD:/SBIN/NOLOGIN
ABRT:X:173:173::/ETC/ABRT:/SBIN/NOLOGIN
HALDAEMON:X:68:68:HAL DAEMON:/:/SBIN/NOLOGIN
GDM:X:42:42::/VAR/LIB/GDM:/SBIN/NOLOGIN
<<:在当前目录下产生文档,如
[[email protected] ~]# cat >> 1.txt << end
> 马强在北京,王鹏健是高级JAVA工程师
> 乔四是高级硬件工程师
> end
[[email protected] ~]# cat 1.txt
马强在北京,王鹏健是高级JAVA工程师
乔四是高级硬件工程师
‘|’ 管道符号,可以将前面命令输出的结果作为后面命令的输入
命令1|命令2|命令3|
如:显示出/eyc/passwd 文件中的第一位
cat /etc/passwd |cut -d: -f1
root
bin
daemon
adm
lp
sync
shutdown
halt
uucp
operator
games
gopher
ftp
nobody
dbus
usbmuxd
vcsa
rtkit
avahi-autoipd
abrt
haldaemon
并将返回的结果按照字母表顺序进行排序
[[email protected] ~]# cat /etc/passwd |cut -d: -f1| sort -n
abrt
adm
apache
avahi-autoipd
bin
daemon
dbus
ftp
games
gdm
gopher
haldaemon
halt
lp
mailnull
mysql
nobody
ntp
operator
postfix
pulse
root
rtkit
saslauth
shutdown
smmsp
sshd
sync
tcpdump
usbmuxd
uucp
vcsa
www
再把输出结果以大写字母方式显示:
[[email protected] ~]# cat /etc/passwd |cut -d: -f1| sort -n| tr ‘a-z‘ ‘A-Z‘
ABRT
ADM
APACHE
AVAHI-AUTOIPD
BIN
DAEMON
DBUS
FTP
GAMES
GDM
GOPHER
HALDAEMON
HALT
LP
MAILNULL
MYSQL
NOBODY
NTP
OPERATOR
POSTFIX
PULSE
ROOT
RTKIT
SASLAUTH
SHUTDOWN
SMMSP
SSHD
SYNC
TCPDUMP
USBMUXD
UUCP
VCSA
WWW
[[email protected] ~]#
获取/etc/passwd 文件中的第10行
[[email protected] ~]# head -10 /etc/passwd |tail -1
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
获取/etc/passwd 文件中的倒数第8行,
21 abrt:x:173:173::/etc/abrt:/sbin/nologin
22 haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
23 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
24 ntp:x:38:38::/etc/ntp:/sbin/nologin
25 apache:x:48:48:Apache:/var/www:/sbin/nologin
26 saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
27 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
28 pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
29 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
30 tcpdump:x:72:72::/:/sbin/nologin
31 mysql:x:500:500:mysq:/home/mysql:/bin/bash
32 www:x:501:501::/home/www:/bin/false
33 mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
34 smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
[[email protected] ~]# tail -8 /etc/passwd |head -1
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[[email protected] ~]#