您的位置:首页 > 其它

【学神】1-8 输入输出重定向、查找及管道

2015-11-11 10:17 295 查看
本节内容: 输入输出重定向. 管道符的应用 主要查找命令

一、重定向

文件描述符:内核(kernel)利用文件描述符(file descriptor)来访问文件。文件描述符是非负整数。打开现存文件或新建文件时,内核会返回一个文件描述符。读写文件也需要使用文件描述符来指定待读写的文件。(linux下一切皆文件。)

1标准输入输出

STDIN 标准输入 如:键盘文件 文件描述符为:0STDOUT 标准输出 屏幕终端 文件描述符为:1STDERR 错误输出 屏幕终端 文件描述符为:2【例】shell中可能经常能看到:xxxx > /dev/null 2>&1 /dev/null 代表空设备文件,是Linux系统中的黑洞文件。放多少东西都填不满。

2输出重定向

> 将命令输出写入文件或设备,而不是命令提示符或句柄< 从文件而不是从键盘或句柄读入命令输入>& 将一个句柄的输出写入到另一个句柄的输入中<& 从一个句柄读取输入并将其写入到另一个句柄输出中>> 将命令输出添加到文件末尾而不删除文件中已有的信息

2.1输出重定向 >

> #代表重定向到哪里1> # 1表示stdout标准输出,系统默认值是1,所以"> /dev/null"等同于"1> /dev/null"【例】
[root@xuegod163 Desktop]# ls /home/ > /dev/null
[ root@xuegod163 Desktop]# cat /dev/null
[root@xuegod163 Desktop]# ls /home/ > a.txt
[root@xuegod163 Desktop]# cat a.txt
mk
[root@xuegod163 Desktop]# ls /home/ 1> b.txt
[root@xuegod163 Desktop]# cat b.txt
mk
2> #表示stderr标准错误【例】
[root@xuegod163 Desktop]# ls /homee/
ls: cannot access /homee/: No such file or directory
[root@xuegod163 Desktop]# ls /homee/ 2> err.txt
[root@xuegod163 Desktop]# cat err.txt
ls: cannot access /homee/: No such file or director
2.2& 表示等同于2>&1 #表示2的输出重定向等同于1输入的路径。 标准错误输出重定向等同于标准输出的路径,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。【例】
[root@xuegod163 Desktop]# ls /home/ /homee/ > all.txt 2>&1
[root@xuegod163 Desktop]# cat all.txt
ls: cannot access /homee/: No such file or directory
/home/:
Mk
[root@xuegod163 Desktop]# ls /home/ /homee/ > /dev/null 2>&1
[root@xuegod163 Desktop]# cat /dev/null
2.3输出重定向>>>>表示追加,即源文件保持不变,在文件的结尾加上>>符号前的内容;【例】
[root@xuegod163 Desktop]#cat /etc/passwd >> a.txt
2.4> 和>> 的区别> 每次都产生新文件。>> 是追加,不产生新的文件。

3输入重定向

wc1.命令格式:wc [选项]文件...2.命令功能:统计指定文件中的字节数、字数、行数,并将统计结果显示输出。该命令统计指定文件中的字节数、字数、行数。如果没有给出文件名,则从标准输入读取。wc同时也给出所指定文件的总统计数。3.命令参数:-c 统计字节数。-l 统计行数。-m 统计字符数。这个标志不能与 -c 标志一起使用。-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。-L 打印最长行的长度。-help 显示帮助信息--version 显示版本信息【例】
[root@xuegod163 Desktop]# wc /etc/passwd
36     60                     1753             /etc/passwd
行     字数(以空格为分隔符)  大小(字节为单位)
[root@xuegod163 Desktop]# wc < /etc/passwd
36   60 1753
<<EOF
【例】
[root@xuegod163 Desktop]# cat  > a.txt
aakds
sdfksf
ctrl+D  #输入的结束
【例】
[root@xuegod163 Desktop]# cat > a.txt <<eof
> mk
> xuegod.cn
> www
> www
> eof
【注】:以<<EOF开始,以EOF结尾 。EOF只是一个统用户一个结果标识符号。换成其他任何字母做标识符也可以,要保证开始的和结尾的要一致,同时区分大小写。

二、管道符

1管道符含义

| 从一个命令中读取输出并将其写入另一个命令的输入中;也称为管道操作符
[root@xuegod163 Desktop]# ps -axu | grep init

2tee命令

功能说明:读取标准输入的数据,并将其内容输出成文件。注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取。格式:tee只输出到标准输出,因为没有指定文件嘛。格式:tee file输出到标准输出的同时,保存到文件file中。如果文件不存在,则创建;如果已经存在,则覆盖之。(If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously
contained is overwritten unless the `-a' option is used.)格式:tee -a file输出到标准输出的同时,追加到文件file中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。格式:tee -输出到标准输出两次。(A FILE of `-' causes `tee' to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)格式:tee file1 file2 -输出到标准输出两次,同时保存到file1和file2中。【例】
[root@xuegod163 Desktop]# w | tee w.txt     #同时会打印出w命令
[root@xuegod163 Desktop]#w > b.txt         #同时不打印出w命令
三、查找命令which 查看可执行文件的位置 whereis 查看可执行文件的位置 及相关文件locate 配合数据库缓存,快速查看文件位置grep 过滤find 实际搜寻硬盘查询文件名称

1 which 和whereis

#which CHMMAND#whereis CHMMANDwhich语法:
[root@xuegod163 ~]# which 可执行文件名称
例如:
[root@xuegod163 ~]# which passwd

/usr/bin/passwd
which是通过 PATH环境变量到该路径内查找可执行文件,所以基本的功能是寻找可执行文件whereis语法:
[root@xuegod163 ~]# whereis [-bmsu] 文件或者目录名称
参数说 明:
-b : 只找二进制文件
-m: 只找在说明文件manual路径下的文件
-s : 只找source源文件
-u : 没有说明文档的文件
例如:
[root@xuegod163 ~]# whereis passwd

passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

将和passwd文件相关的文件都查找出来

[root@xuegod163 ~]# whereis -b passwd

passwd: /usr/bin/passwd /etc/passwd

只将二进制文件 查找出来
【例】
which whereis主要的区别
[root@xuegod163 ~]# which  ls
alias ls='ls --color=auto'
/bin/ls
[root@xuegod163 ~]# which  find
/usr/bin/find
[root@xuegod163 ~]# whereis ls     #更详细的显示命令由哪个包安装的
ls: /bin/ls /usr/share/man/man1/ls.1.gz  /usr/share/man/man1p/ls.1p.gz
2 locate [root@ xuegod163 ~]# updatedb #使用前,用此命令更新数据库,否则最新创建的文件找不到。 晚上2:00左右自动更新。 在计划任务中有。locate 配合数据库缓存,快速查看文件位置 【例】
[root@xuegod163 ~]# touch /etc/mk.txt
[root@xuegod163 ~]# locate /etc/mk.txt
[root@xuegod163 ~]# ll /etc/mk.txt
-rw-r--r-- 1 root root 0 Jan 28 21:37 /etc/mk.txt
[root@xuegod163 ~]#
[root@xuegod163 ~]# updatedb   #手动更新
[root@xuegod163 ~]# locate /etc/mk.txt
/etc/mk.txt
和find相比,whereis查找的速度非常快,这是因为linux系统会将 系统内的所有文件都记录在一个数据库文件中,当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通 过遍历硬盘来查找,效率自然会很高。

3查看帮助文档

man updatedb



4 grep过滤

【例】
[root@xuegod163 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@xuegod163 ~]# grep --color root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
说明:find搜索文件系统、实时搜索格式:find [目录] [条件] [动作](1). [目录]不输入代表当前目录
#find /home    #查找home目录下
(2). [条件]主要有用户和组:-user -group -nouser(没有拥有组的文件) -nogroup例10: -user 查找/home下拥有者为mk的文件:
[root@xuegod163 ~]# find /home/ -user mk
1. 按类型-type ( f 文件 , d 目录 , l 连接 , p 管道 ,c 字符文件 ,b 块文件 ,s socket文件 )
[root@xuegod163 Desktop]# find /home/  -type d
[root@xuegod163 Desktop]# find /dev/ -type b

2. 按名字

-gid n :寻找群组ID为n的文件
-group name :寻找群组名称为name的文件
-uid n :寻找拥有者ID为n的文件
-user name :寻找用户者名称为name的文件
-name file :寻找文件名为file的文件(可以使用通配符)【例】查看home包括关键词user的文件和文件夹
[root@xuegod163 Desktop]# useradd  user1
[root@xuegod163 Desktop]# useradd vipuser3
[root@xuegod163 Desktop]# touch /home/user1/aaauserccc
[root@xuegod163 Desktop]# find /home/ -name *user*  #  *匹配所有字符

3. 按大小

-size +NM 大于N兆 -NG 小于NGB
[root@xuegod163 Desktop]# find /boot/ -size +2M
[root@xuegod163 Desktop]# find /boot/ -size -2M

4. 按时间

-mtime -atime -ctime查看
touch b.sh
date -s "2015-01-30"
find /tmp/ -mtime 1
-mtime +2    查看不含今天的2天前被修改的所有文件,如:今天是9月6日,则找出3日及3日之前,被修改的内容。
-atime n :将n*24小时内存取过的的文件列出来“访问时间(access time)”
-ctime n :将n*24小时内改变、新增的文件或者目录列出来“改变时间(change time)”
-mtime n :将n*24小时内修改过的文件或者目录列出来“修改时间(modification time)”
-newer file :把比file还要新的文件列出来例12:改变和修改之间的区别在于是改文件的属性还是更改它的内容。chmod a-w myfile,那么这是一个改变;改变的ctimeecho foo >> myfile,那么这是一个修改。mtime :“修改时间ctime,改变是文件的索引节点发生了改变;mtime 修改是文本本身的内容发生了变化。【例】 ls(1) 命令可用来列出文件的 atime、ctime 和 mtime。
ls -lc filename         列出文件的 ctime
ls -lu filename         列出文件的 atime
ls -l filename          列出文件的 mtime
【例】
对比atime、ctime 和 mtime 的不同点
[root@xuegod163 ~]# vim a.txt   #写入aaaa
[root@xuegod163 ~]# date -s '13:42'
Sat Dec 27 13:42:00 CST 2014
[root@xuegod163 ~]# chmod +x a.txt
[root@xuegod163 ~]# date -s '13:44'
Sat Dec 27 13:44:00 CST 2014
[root@xuegod163 ~]# cat a.txt
aaaaaaa
[root@xuegod163 ~]# ll a.txt
-rwxr-xr-x 1 root root 8 Dec 27 13:40 a.txt
[root@xuegod163 ~]# ll -c a.txt
-rwxr-xr-x 1 root root 8 Dec 27 13:42 a.txt
[root@xuegod163 ~]# ll -u a.txt
-rwxr-xr-x 1 root root 8 Dec 27 13:44 a.txt

6. 按权限:-perm

[root@xuegod163 ~]# find /boot/ -perm 755  #等于0775权限的文件或目录
-perm  -
suid 4   suid 2  sticky  1
[root@xuegod163 ~]# find /tmp -perm -777   #至少有777权限的文件或目录
【例】高级权限
[root@xuegod163 ~]# mkdir ccc
[root@xuegod163 ~]# chmod 777 ccc
[root@xuegod163 ~]# mkdir test
[root@xuegod163 ~]# chmod 1777 test
[root@xuegod163 ~]# touch b.sh
[root@xuegod163 ~]# chmod 4777 b.sh
[root@xuegod163 ~]# find /root/ -perm 777   #只能找到777权限的文件
[root@xuegod163 ~]# find /root/ -perm 1777  #只能找到1777权限的文件
[root@xuegod163 ~]# find /root/ -perm  4777  #只能找到4777权限的文件
[root@xuegod163 ~]# find /root/ -perm  -777  # 找到所有777权限的文件

7. 按查找的目录深度

[root@xuegod163 ~]# find /boot/ -maxdepth 1 #只查找目录第一层的文件和目录
[root@xuegod163 ~]# find /boot/ -maxdepth 2
8. 多条件结合【实例如下】-a -o ! 或 -and -or -not在/root找其他人可以写的文件
[root@xuegod163 ~]# find -type f  -and -perm -002
[root@xuegod163 ~]# find -type f  -and -perm /o+w
./b.sh
主要动作-print: find命令将匹配的文件输出到标准输出。 -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为 'command' { } \;,注意{ }和\;之间的空格。 -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。【例】找到一个目录下的所有文件并删除
[root@xuegod163 ~]# mkdir test2
[root@xuegod163 ~]# cp /etc/passwd test2/
[root@xuegod163 ~]# cp -r /boot/ test2/
[root@xuegod163 ~]# find /root/test2 -type f -exec rm {} \;
参数解释: -exec 执行命令 rm 要执行的命令 {} 表示find -type f 查找出来了文件内容 \; {} 和 \;之间要有空格。 固定语法,就是以这个结尾

当我们用whereis和locate无法查找到我们需要的文件时,可以使用find,但是find是在硬盘上遍历查 找,因此非常消耗硬盘的资源,而且效率也非常低,因此建议大家优先使用whereis和locate。
locate 是在数据库里查找,数据库大至每天更新一次。
whereis 可以找到可执行命令和man page
find 就是根据条件查找文件。
which 可以找到可执行文件和别名(alias) 学神-IT-教育51cto技术交流群:468845589 快来上我们公开课吧! 学神MK老师:1273815479 学神ZY老师:3054384936 学神IT-VIP-1508-公瑾编译提供本文出自 “学神IT教育-讲师MK” 博客,请务必保留此出处http://xuegodlinux.blog.51cto.com/10844319/1711707
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: