grep: 文件内容过滤
find: 文件查找,针对文件名
1、命令文件
# which ls //从PATH环境变量 (echo $PATH)
# whereis vim
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin
2、任意文件
A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:天天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb
# locate ifcfg-eth0
# locate ifcfg-enp0s25
locate
updatedb后才能查找到 很是麻烦 不建议使用
若是没有 locate 使用YUM直接安装是不行的。 要查一下在哪一个包里 yum provides locate -→ mlocate → 直接YUM mlocate便可
B. find
find [options] [path...] [expression] [action]
===expression=== 熟用*通配符
按文件名:
[root@tianyun ~]# find /etc -name "ifcfg-eth0" name 名字 后面-print 已省略
[root@tianyun ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
[root@tianyun ~]# find /etc -iname "ifcfg-eth*"
按文件大小:
[root@tianyun ~]# find /etc -size +5M //大于5M
[root@tianyun ~]# find /etc -size 5M
[root@tianyun ~]# find /etc -size -5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动做 不是平时用的ls
ll - h 查看大小
指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-eth0" maxdepth 3 最大3层 a要知足2个条件 而且
按时间找(atime,mtime,ctime):
[root@tianyun ~]# find /etc -mtime +5 //修改时间超过5天
[root@tianyun ~]# find /etc -mtime 5 //修改时间等于5天
[root@tianyun ~]# find /etc -mtime -5 //修改时间5天之内
按文件类型:
[root@tianyun ~]# find /dev -type f //f普通
[root@tianyun ~]# find /dev -type d //d目录
[root@tianyun ~]# find /dev -type l //l连接
[root@tianyun ~]# find /dev -type b //b块设备
[root@tianyun ~]# find /dev -type c //c字符设备
[root@tianyun ~]# find /dev -type s //s套接字
[root@tianyun ~]# find /dev -type p //p管道文件
按文件权限:
[root@tianyun ~]# find . -perm 644 -ls .是当前目录 精确查找644 *通常都是精确
[root@tianyun ~]# find . -perm -644 -ls -是包含到意思
带不带- 本身对比一下 查看。 带-表示只要6就能够
[root@tianyun ~]# find . -perm -600 -ls
[root@tianyun ~]# find . -perm -222 -ls //全局可写
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky
==找到后处理的动做 ACTIONS: (默认动做-print)==
-print
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
[root@tianyun ~]# find /etc -name "ifcfg*" 后能够加|xargs -h
[root@tianyun ~]# find /etc -name "ifcfg*" -print
[root@tianyun ~]# find /etc -name "ifcfg*" -lsshell
exec命令用于调用并执行指令的命令
查找带root带文件 复制到tmp下
find /etc -name “root*” -exec cp -rf {} /tmp \; 复制到当前文件下 /tmp换成.
[root@tianyun ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; exec为执行一条shell命令 {}为前面的东西\; 格式
[root@tianyun ~]# find /etc -name "ifcfg*" -delete
扩展知识:find结合xargs
[root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf !!!!!!!!!!!!!重点 找到以后删除处理xargs 参数传递处理找出后删除
[root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
案例1: 分别找出file5 和除了file5的文件
[root@tianyun ~]# mkdir dir1
[root@tianyun ~]# touch dir1/file{1..20}
[root@tianyun ~]# find /root/dir1 -name "file5"
[root@tianyun ~]# find /root/dir1 ! -name "file5" !为取反
[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" 便是file5又是file9
/root/dir1/file5
/root/dir1/file9
扩展 不经常使用
[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" -ls
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@tianyun ~]# find /root/dir1 -name "file5" -ls -o -name "file9" -ls
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@tianyun ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls \为转译 不加会报错
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’数据库
2010-11-27 星期六 晴朗当你在命令行执行:
$find . -name 'core' -type f -exec rm {} /;
时,find -exec 命令会对每一个匹配的文件执行一个单独的rm操做(execute a separate rm for each one), 正如你手动敲入下面命令:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...可是使用这种方式,若是有100个文件匹配了,那么就须要启100个进程,一个进程处理一个rm命令。通常来讲,其越多进程,意味着越耗性能。咱们能够换个思路,咱们将要删除文件看成参数传递给rm不就能够了吗?也就是说:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...改为:
rm ./bin/core ./source/shopping_cart/core ./backups/core可是前提是后面的命令必须支持多参数。相有些命令,好比unzip,就不支持输入多个jar包,因此必须用-exec。
xargs,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。所以上面的find -exec命令能够改写成:
find . -name 'core' -type f -print | xargs rmWith this approach, xargs bundles together as many filename arguments as possible for submission to each invocation of rm that's needed, in compliance with the OS's maximum allowed size for an argument list. This means xargs is guaranteed not only to handle all the arguments, but also to use the smallest possible number of processes in doing so. For example, if each command can handle 100 arguments, and there are 110 filenames to process, there will be two invocations of the command, respectively handling 100 and 10 arguments.
其中操做系统容许的最大参数长度由以下命令获得:
forrest@ubuntu:~$ getconf ARG_MAX
2097152这意味着xargs保证不会由于参数过多而挂掉。因此目前看来惟一须要保证的就是后面的命令支持多参数。好比前面说过的unzip,就不支持多参数,若是你使用xargs xxx.jar
相比之下,也不难看出各自的缺点
一、exec 每处理一个文件或者目录,它都须要启动一次命令,效率很差;
二、exec 格式麻烦,必须用 {} 作文件的代位符,必须用 \; 做为命令的结束符,书写不便。
三、xargs 不能操做文件名有空格的文件;
综上,若是要使用的命令支持一次处理多个文件,而且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 不然,就要用 exec了。express