第五章 管道符与重定向
输出重定向
查看vim 进程
[root@localhost ~]# ps -aux |grep vim
root 3653 0.0 0.4 229788 9528 pts/0 S+ 11:39 0:00 vim time.txt
root 3690 0.0 0.1 221680 2348 pts/2 S+ 11:40 0:00 grep --color=auto vim
[root@localhost ~]#
# ls /proc/ |grep 3653 查看vim进程编号
[root@localhost ~]# ls /proc/ |grep 3653
3653
# ls /proc/3653 展示3653 进程的文本详细信息
[root@localhost ~]# ls /proc/3653
arch_status cpuset loginuid oom_adj sessionid timens_offsets
attr cwd map_files oom_score setgroups timers
#ll /proc/3653/fd 软连接文件信息
[root@localhost ~]# ls /proc/3653/fd
0 1 2 4
[root@localhost ~]# ll /proc/3653/fd
总用量 0
lrwx------. 1 root root 64 9月 16 13:03 0 -> /dev/pts/0
lrwx------. 1 root root 64 9月 16 13:03 1 -> /dev/pts/0
lrwx------. 1 root root 64 9月 16 13:03 2 -> /dev/pts/0
lrwx------. 1 root root 64 9月 16 13:03 4 -> /root/.time.txt.swp
[root@localhost ~]#
#> 覆盖文件
[root@localhost ~]# echo 1111111111111111111 > time
#>> 追加文件
[root@localhost ~]# echo 111111111111111111dddd1 >> time
[root@localhost ~]# mkdir -v 98901 ## -v 打印process处理过程信息
[root@localhost ~]# cat time
mkdir: 已创建目录 '98901'
[root@localhost ~]# mkdir -v 98901 > time ## -v 打印出的程序处理过程信息 覆盖到time文件
mkdir: 无法创建目录 “98901”: 文件已存在
[root@localhost ~]# mkdir -v 98901 >> time
mkdir: 无法创建目录 “98901”: 文件已存在
[root@localhost ~]# cat time ## > == 1> 1 是标准的输出stdout
[root@localhost ~]# mkdir -v 98901 >> time
mkdir: 无法创建目录 “98901”: 文件已存在
[root@localhost ~]# mkdir -v 98901 2>> time
[root@localhost ~]# cat time
mkdir: 无法创建目录 “98901”: 文件已存在
[root@localhost ~]# mkdir -v 98901 2>> time ## 2>> 1 是标准的错误输出stderr
[root@localhost ~]# cat time
mkdir: 无法创建目录 “98901”: 文件已存在
mkdir: 无法创建目录 “98901”: 文件已存在
[root@localhost ~]#
&>
[root@localhost ~]# ls /home/ /aaaaaaaaaaaaaa &> 1.txt ## 正确的和错误的输出都放到1.txt文件
[root@localhost ~]# cat 1.txt
ls: 无法访问 '/aaaaaaaaaaaaaa': 没有那个文件或目录
/home/:
tanghl
user01
user02
user03
user04
user100
user200
user300
[root@localhost ~]#
1> stdout.txt 2> stderr.txt
[root@localhost ~]# ls /home/ /aaaaaaaaaaaaaa 1> stdout.txt 2> stderr.txt #分别放到不同的文件
[root@localhost ~]# cat stdout.txt
/home/:
tanghl
user01
user02
user03
user04
user100
user200
user300
[root@localhost ~]# cat stderr.txt
ls: 无法访问 '/aaaaaaaaaaaaaa': 没有那个文件或目录
[root@localhost ~]#
&> /dev/null
[root@localhost ~]# ls /home/ /aaaaaaaaaaaaaa &> /dev/null ## &> /dev/null 垃圾桶
[root@localhost ~]#
输入重定向
<
进程管道Piping
管道符命令可以将多条命令组合起来,完成复杂的处理任务
| 管道符
[root@localhost ~]# cat /etc/passwd | tail -3
user100:x:1505:1506::/home/user100:/bin/bash
user200:x:1506:1507::/home/user200:/bin/bash
user300:x:1507:1508::/home/user300:/bin/bash
[root@localhost ~]#
[root@localhost ~]# cat /etc/passwd | grep user0
user01:x:1001:1001::/home/user01:/bin/bash
user02:x:1502:1502::/home/user02:/bin/bash
user03:x:1503:1503::/home/user03:/bin/bash
user04:x:1504:1505::/home/user04:/bin/bash
[root@localhost ~]#
tee 管道
# cat /etc/passwd |tee file.txt | tail -3 既展示前三行文本 又会将文本输出到file.txt文件
[root@localhost ~]# cat /etc/passwd |tee file.txt | tail -3
user100:x:1505:1506::/home/user100:/bin/bash
user200:x:1506:1507::/home/user200:/bin/bash
user300:x:1507:1508::/home/user300:/bin/bash
|xargs 参数传递
| rm -rvf 递归的可视化强制删除
删除失败因为管道符不支持部分命令
[root@localhost ~]# cat file.txt | rm -rvf
[root@localhost ~]# ls
1.txt 98901 模板 图片 下载 桌面 dead.letter file2 stderr.txt time
989 公共 视频 文档 音乐 anaconda-ks.cfg file1 file.txt stdout.txt time.txt
[root@localhost ~]#
[root@localhost ~]# ls file*
file1 file2 file3 file4 file.txt
[root@localhost ~]# cat file.txt | rm -rvf
[root@localhost ~]# ls file*
file1 file2 file3 file4 file.txt
[root@localhost ~]# cat file.txt |xargs rm -rvf
已删除 '/root/file1'
已删除 '/root/file2'
已删除 '/root/file3'
[root@localhost ~]# ls file*
file4 file.txt
[root@localhost ~]# cat file.txt
/root/file1
/root/file2
/root/file3
[root@localhost ~]# pwd
/root
[root@localhost ~]#
浙公网安备 33010602011771号