目录
重定向
0, 1, and 2, known as standard input, standard output, and standard error
0、1 和 2,称为标准输入、标准正确输出和标准错误输出
0,标准输入(键盘)
1,标准输出
2,标准错误,
3+,进程在执行过程中打开的其他文件。
输出重定向
正确输出:1> 等价于 > 覆盖 1>> 等价于>>追加
错误输出:2>等价于 > 覆盖 2>> 等价于>>追加
案例1:输出重定向(覆盖)
[root@localhost ~]# date > date.txt
[root@localhost ~]# cat date.txt
2022年 03月 03日 星期四 20:27:40 CST
案例2:输出重定向(追加)
[root@localhost ~]# date +%F >> date.txt
[root@localhost ~]# cat date.txt
2022年 03月 03日 星期四 20:27:40 CST
2022-03-03
案例3:错误输出重定向
[root@localhost ~]# ls /home/ /aaaa >list.txt 2>error.txt #正确的覆盖输出到list.txt,错误的覆盖输出到error.txt
[root@localhost ~]# cat list.txt
/home/:
alan
[root@localhost ~]# cat error.txt
ls: 无法访问/aaaa: 没有那个文件或目录
案例4:正确的和错误的输出到一个地方
[root@localhost ~]# ls /home/ /aaaa &>list.txt &:and符号
[root@localhost ~]# cat list.txt
ls: 无法访问/aaaa: 没有那个文件或目录
/home/:
alan
脚本中使用重定向
(不使用重定向的情况)
[root@localhost ~]# vim ping1.sh
#!/bin/bash
ping -c1 192.168.242.130
if [ $? -eq 0 ];then
echo "192.168.242.130 is up."
else
echo "192.168.242.130 is down!"
fi
[root@localhost ~]# chmod +x ping1.sh
[root@localhost ~]# ./ping1.sh
PING 192.168.242.130 (192.168.242.130) 56(84) bytes of data.
64 bytes from 192.168.242.130: icmp_seq=1 ttl=64 time=0.014 ms
--- 192.168.242.130 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.014/0.014/0.014/0.000 ms
192.168.242.130 is up.
(使用重定向)
#!/bin/bash
ping -c1 192.168.242.130 &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.242.130 is up." >>/up.txt
else
echo "192.168.242.130 is down!" >>/down.txt
fi
[root@localhost ~]# ./ping1.sh
输入重定向
标准输入: < 等价 0<
例如:默认情况下,cat 命令会接受标准输入设备(键盘)的输入,并显示到控制台,但如果用文件代替键盘作为输入设备,那么该命令会以指定的文件作为输入设备,并将文件中的内容读取并显示到控制台。
[root@localhost ~]# cat /etc/passwd
[root@localhost ~]# cat < /etc/passwd #输入重定向
虽然执行结果相同,但第一行代表是以键盘作为输入设备,而第二行代码是以 /etc/passwd 文件作为输入设备
注意:将/etc/passwd作为cat的输入,读出/etc/passwd的内容
案例
[root@localhost ~]# cat >file <<eof
> 111
> 222
> 333
> eof
[root@localhost ~]# cat file
111
222
333
案例:利用重定向建立多行的文件 脚本创建多行文件
[root@localhost ~]# vim anli.sh
#!/bin/bash
cat >file200.txt <<eof
111
222
333
eof
[root@localhost ~]# chmod +x anli.sh
[root@localhost ~]# ./anli.sh
[root@localhost ~]# cat file200.txt
111
222
333
管道 |
用法:command1 | command2 |command3 |
sort -t":" -k3 -n /etc/passwd #以":"为分隔符,将第三例按数字升序排列
sort -t":" -k3 -nr /etc/passwd #以":"为分隔符,将第三列按数字降序排列
sort -t":" -k3 -nr /etc/passwd|head -2 #以: 分隔,将第三列按字数升序看前2行
参数详解:
sort 排序,默认升序
-t 指定分隔符
-k 指定列
-n 按数值
-r 降序
[root@localhost ~]# netstat -lnpt|awk 'NR==3 {print $4}'# 只显示第三行的第四列
0.0.0.0:22
[root@localhost ~]# netstat -lnpt|awk 'NR==3 {print $4}'|awk -F':' '{print $2}' #在第三行的第四列以':'为分隔符,打印第二列
22
参数传递:xargs
对:ls cp rm 管道不能执行。所以通过xargs。
语法:
cat a.txt | xargs -i cp {} /目录
{}:前面传过来的内容
-i :为了让大括号生效
目录时 -r
解释:前面传过来的东西交给大括号
cat file.txt |xargs ls -l
前面是目录或者目录的路径。 ls - l 后面可以不加大括号,直接执行。
案例
[root@localhost ~]# touch /home/file{1..5}
[root@localhost ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@localhost ~]# cat files.txt |ls -l
总用量 50292
-rw-r--r--. 1 root root 60 3月 12 16:22 files.txt
-rw-r--r--. 1 root root 10928 1月 14 18:21 mysql80-community-release-el7-5.noarch.rpm
-rw-r--r--. 1 root root 51436383 6月 10 2019 mysql-boost-5.7.27.tar.gz
-rw-r--r--. 1 root root 42947 2月 11 17:20 nethogs-0.8.6-6.fc36.x86_64.rpm
[root@localhost ~]# cat files.txt |xargs ls -l
-rw-r--r--. 1 root root 0 3月 12 16:20 /home/file1
-rw-r--r--. 1 root root 0 3月 12 16:20 /home/file2
-rw-r--r--. 1 root root 0 3月 12 16:20 /home/file3
-rw-r--r--. 1 root root 0 3月 12 16:20 /home/file4
-rw-r--r--. 1 root root 0 3月 12 16:20 /home/file5
案例二
[root@youngfit ~]# touch /home/file{1..5}
[root@youngfit ~]# vim files.txt
/home/file1
/home/file2
/home/file3
/home/file4
/home/file5
[root@youngfit ~]# cat files.txt | xargs -i cp -rvf {} /tmp/
‘/home/file1’ -> ‘/tmp/file1’
‘/home/file2’ -> ‘/tmp/file2’
‘/home/file3’ -> ‘/tmp/file3’
‘/home/file4’ -> ‘/tmp/file4’
‘/home/file5’ -> ‘/tmp/file5’
[root@youngfit ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
加-i 参数直接用 {}就能代替管道之前的标准输出的内容;
加 -I 参数 需要事先指定替换字符
常用小命令
[root@youngfit ~]# du -h /etc/ #查看目录及目录中的文件大小
[root@youngfit ~]# du -sh /etc/ #查看目录的总大小
[root@youngfit ~]# ls /etc/ | wc -l #查看目录中有多少个文件