l 交互工具
l 过滤器
l 编辑器
过滤器:能够接受数据,过滤再输出的工具,成为过滤器
对过滤器和进程,存在着输入源与输出源头
输入:过滤器的输入来源
标准输入:stdin(0):默认是键盘
输出:过滤器的数据去向
标准输出:stiout(1):默认是终端屏幕
错误输出:报错信息与标准输出走不通的IO通道
错误输出:stderr(2):默认是终端屏幕
重定向:标准输入,输出与错误输出,都可以用特定符号改变来源或去向
[root@localhost home]# ls -l /dev/std*
lrwxrwxrwx 1 root root 15 Jul 12 04:18 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jul 12 04:18 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Jul 12 04:18 /dev/stdout -> /proc/self/fd/1
使用“<”来重定向输入源
大多数工具都会以其后的文件名为输入源
有一些过滤器一定需要添加“<”以明确输入源
Command < file
从当前文档输入
使用<<让系统将一次键盘的全部输入,先送入虚拟的‘当前文档’,然后一次性输入
需要对字母,符号或字符串作为起始终结标识符
可以选择任意符号作为起始终结标识符
[root@localhost test]# cat > test.file <<!
> this is the test file
> !
[root@localhost test]# cat test.file
this is the test file
[root@localhost test]#
使用>改变数据输出目标
范例:
[root@localhost /]# ls -l > /home/test/test.file
[root@localhost /]# cat /home/test/test.file
total 138
drwxr-xr-x 2 root root 4096 Jun 8 03:21 bin
drwxr-xr-x 4 root root 1024 Jun 7 22:19 boot
drwxr-xr-x 12 root root 3880 Jul 12 04:19 dev
drwxr-xr-x 97 root root 12288 Jul 12 04:19 etc
drwxr-xr-x 4 root root 4096 Jul 12 04:52 home
drwxr-xr-x 14 root root 4096 Jun 8 03:21 lib
drwx------ 2 root root 16384 Jun 7 22:14 lost+found
drwxr-xr-x 2 root root 4096 Jul 12 04:19 media
drwxr-xr-x 2 root root 0 Jul 12 04:19 misc
drwxr-xr-x 2 root root 4096 Mar 30 2007 mnt
drwxr-xr-x 2 root root 0 Jul 12 04:19 net
drwxr-xr-x 2 root root 4096 Mar 30 2007 opt
dr-xr-xr-x 90 root root 0 Jul 12 04:18 proc
drwxr-x--- 17 root root 4096 Jun 9 06:02 root
drwxr-xr-x 2 root root 12288 Jun 8 03:21 sbin
drwxr-xr-x 4 root root 0 Jul 12 04:18 selinux
drwxr-xr-x 2 root root 4096 Mar 30 2007 srv
drwxr-xr-x 11 root root 0 Jul 12 04:18 sys
drwxrwxrwt 7 root root 4096 Jul 12 04:53 tmp
drwxr-xr-x 14 root root 4096 Jun 7 22:16 usr
drwxr-xr-x 21 root root 4096 Jun 7 22:23 var
test.file文件已有内容会被消除
使用>进行输出重定向,文件的原内容会被覆盖
ls –l >listfile
使用>>,可以将输入追加入文件
ls –l > listfile
系统错误与标准输出使用的IO管道不同
默认情况下,系统报错会显示到终端屏幕上
使用2> 将报错讯息重定向到一个文件
[root@localhost test]# ls-l 2> test.file
[root@localhost test]# cat test.file
-bash: ls-l: command not found
使用2>>将报错讯息追加到一个文件
[root@localhost test]# cat test.file
-bash: ls-l: command not found
-bash: ls-l: command not found
使用>和2> 可以将一次操作的正确,错误输出,被单独地送到不同的地方:
[root@bogon ~]# find /etc -name passwd 2> stderr >stdout
正确输出到stdout中,错误输出到stderr中
使用&>将所有输入都送向同一个地方:
使用 | 将前一个过滤器的输出直接送入后一个过滤器的输入
[root@bogon ~]# ls -l /etc | more
允许多重管道
[root@bogon ~]# ls -l /etc | more | grep fstab
-rw-r--r-- 1 root root 534 Jun 7 22:14 fstab
注意管道前过滤器的输出与管道后过滤器的输入数据类型匹配
l ls -l |more
分屏显示当前目录下的文件
l Cat <fileA >fileB
拷贝fileA到fileB
l Cat file.* >file cat fileA fileB > fileAB
将数个小文件,合并成一个文件