Linux中的应用工具分为三种:
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
从当前文档输入
使用<
需要对字母,符号或字符串作为起始终结标识符
可以选择任意符号作为起始终结标识符
[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-x2 root root4096 Jun8 03:21 bin
drwxr-xr-x4 root root1024 Jun7 22:19 boot
drwxr-xr-x 12 root root3880 Jul 12 04:19 dev
drwxr-xr-x 97 root root 12288 Jul 12 04:19 etc
drwxr-xr-x4 root root4096 Jul 12 04:52 home
drwxr-xr-x 14 root root4096 Jun8 03:21 lib
drwx------2 root root 16384 Jun7 22:14 lost+found
drwxr-xr-x2 root root4096 Jul 12 04:19 media
drwxr-xr-x2 root root0 Jul 12 04:19 misc
drwxr-xr-x2 root root4096 Mar 302007 mnt
drwxr-xr-x2 root root0 Jul 12 04:19 net
drwxr-xr-x2 root root4096 Mar 302007 opt
dr-xr-xr-x 90 root root0 Jul 12 04:18 proc
drwxr-x--- 17 root root4096 Jun9 06:02 root
drwxr-xr-x2 root root 12288 Jun8 03:21 sbin
drwxr-xr-x4 root root0 Jul 12 04:18 selinux
drwxr-xr-x2 root root4096 Mar 302007 srv
drwxr-xr-x 11 root root0 Jul 12 04:18 sys
drwxrwxrwt7 root root4096 Jul 12 04:53 tmp
drwxr-xr-x 14 root root4096 Jun7 22:16 usr
drwxr-xr-x 21 root root4096 Jun7 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 root534 Jun7 22:14 fstab
注意管道前过滤器的输出与管道后过滤器的输入数据类型匹配
重定向,管道的常用范例
lls -l |more
分屏显示当前目录下的文件
lCat fileB
拷贝fileA到fileB
lCat file.* >filecat fileA fileB > fileAB
将数个小文件,合并成一个文件