文章目录
在使用linux时,经常需要进行文件查找。其中查找的命令主要有find和grep。两个命令是有区的。
(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访问时间,修改时间等。
(2)grep是根据文件的内容进行查找,会对文件的每一行按照给定的模式(patter)进行匹配查找。(上一篇正则表达式就是在介绍grep命令)
文件属性查找工具有这两种 :1、locate :非实时查找(基于数据库查找)2、find: 实时查找。
1.locate查找工具
基于数据库的搜索/var/lib/mlocate/mlocate/db
更新数据文件方法 :updatadb
索引构建过程需要遍历整个根文件系统,极消耗资源 ,生产中不建议全盘搜索,太消耗资源
工作特点:
-
查找速度快
-
模糊查找
-
非实时性查找
-
搜索的文件是全路径,比仅仅是文件名,只要包括关键字,都会搜索
-
可能只搜索用户具备读取的执行权限目录
有用选项:
- -i:不区分大小写的搜索
- -n N:只列举钱N个匹配到的项目
- -r 使用正则表达式
示例:
1、 搜索名称或路径中带有“conf”的文件
[root@ecs-c2c9 ~]# locate conf #
2、 使用Regex来搜索以“.conf”结尾的文件
[root@ecs-c2c9 ~]# locate -r ‘.conf$’
3、搜索前3项,从以下执行结果可以看到只要包括passwd都会显示
[root@centos6 ~]#locate -n 3 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
4、 搜索前2项,搜索以.conf结尾的文件
[root@ecs-c2c9 ~]# locate -r '.conf$' -n 2
/CloudResetPwdUpdateAgent/conf
/CloudResetPwdUpdateAgent/conf/wrapper.conf
2.find查找工具
实时查找、查找速度略慢、精确查找、搜索范围必须是用户具备读写跟执行权限
find [选项][查找路径][查找条件][处理动作]
工作特点:
- 实时查找
- 查找啊速度慢
- 精确查找
- 可能只搜索用户具备读取和执行权限的目录
语法:
find [选项] [查找路径] [查找条件] [处理动作]
- 查找路径:指定具体目标路径,默认为当前目录
- 查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件
- 处理动作:对符合条件的文件作操作,默认输出至屏幕
查找条件
搜索条件(根据搜索层级)
-maxdepth level 最大搜索目录深度,从指定目录为第一级
-mindepth level 最小搜索目录深度
示例:
1、 指定搜索/etc目录下为5级目录的目录和文件
Find /etc –maxdepth 5 –mindepth 5
2、 搜索/etc目录1层到2层的所有文件
[root@centos6 ~]#find /etc -mindepth 1 -maxdepth 2 | head -2
/etc/cron.weekly
/etc/rc3.d
根据文件名和inode号查找
-name “文件名称”:支持正则匹配,使用 * ? [] [^]
-iname “文件名称” 不区分大小写
-inum n:按inode号查找
-samefile name 相同inode号的文件
-links n:连接数为n的文件
-regex “范本样式”:指定字符串作为寻找文件或目录的范本样式
-regex -regextype posix-egrep -regex “匹配内容”
find /etc/ -regextype posix-egrep -regex “.*/pa[sa]{2}wd”
其中 posix-egroup 属于扩展正则表达式
posix-basic 属于正则表达式
posix-extended 属于扩展正则表达式
示例:
1、 -name "文件名称":支持正则匹配
[root@centos6 ~]#find /etc -name passwd #在/etc目录下搜索名字为passwd的文件
/etc/passwd
/etc/pam.d/passwd
2、 -iname "文件名称" 不区分大小写
[root@centos6 ~]#find /etc -iname PASSWD #不区分大小写的搜索
/etc/passwd
/etc/pam.d/passwd
3、 -inum n:按inode号查找
[root@centos6 ~]#find /etc -inum 2232956 #根据inode号查找文件
/etc/passwd
4、 -samefile name 相同inode号的文件
[root@centos6 ~]#find / -samefile passwd1 查找该文件在系统中有几个硬链接即相同inode号
/etc/passwd
find: `/proc/5248/task/5248/fd/5': No such file or directory
find: `/proc/5248/task/5248/fdinfo/5': No such file or directory
find: `/proc/5248/fd/5': No such file or directory
find: `/proc/5248/fdinfo/5': No such file or directory
/root/passwd1
5、 -links n:连接数为n的文件
[root@centos6 ~]#find / -links 2 | xargs ls -l | head -1 #这个文件的硬链接数为2
-rwxr-xr-x. 2 root root 53400 Nov 11 2010 /bin/ed
6、 -regex "范本样式":指定字符串作为寻找文件或目录的范本样式
[root@centos6 app]#find -regex ".*\.sh" #基于正则表达式查找
./sysinfo.sh
./create.sh
[root@centos6 app]#find -iregex ".*\.SH" #-iregex忽略大小写
./sysinfo.sh
./create.sh
根据属主,属组查找
-user 用户名:查找属组为指定用户(UID)的文件
-group 组名:查找属组为指定组(GID)的文件
-uid UID:查找属主为指定的UID号的文件
-gid GID:查找属组为指定的GID号的文件
-nouser:查找没有属主的文件
-nogroup:查找没有属组的文件
示例:
1、 -user 用户名:查找属组为指定用户(UID)的文件
[root@centos6 app]#find / -user test #搜索属于test用户的所有文件
/home/mage/.bash_history
/home/test
/home/test/.bashrc
2、 -group 组名:查找属组为指定组(GID)的文件
[root@centos6 app]#find / -group test #搜索所有test组的文件
/home/mage/.bash_history
/home/test
/home/test/.bashrc
3、 -uid UID:查找属主为指定的UID号的文件
[root@centos6 app]#find / -uid 500
/home/mage/.bash_history
/home/test
/home/test/.bashrc
4、 -gid GID:查找属组为指定的GID号的文件
[root@centos6 app]#find / -gid 500
/home/mage/.bash_history
/home/test
/home/test/.bashrc
5、 -nouser:查找没有属主的文件
[root@centos6 app]#find /tmp -nouser #因为刚才删除test用户,所以test建立的所有文件将变成无属主属组的文件
/tmp/1.txt
6、 -nogroup:查找没有属组的文件
[root@centos6 app]#find /tmp -nogroup
/tmp/1.txt
根据文件类型查找
-type 类型
- f:普通文件
- d:目录文件
- l:符号连接文件
- s:套接字文件
- b:块设备文件
- c:字符设备文件
- p:管道文件
示例:
搜索app目录下所有的普通文件,别的类型文件搜索方式相似
[root@centos6 app]#find /app/ -type f
/app/sysinfo.sh
/app/access.log
/app/create.sh
组合条件:
组合条件:
与:-a
或:-o
非:-not,!
德·摩根定律:
(非A)或(非B)= 非(A且B)
(非A)或(非B)= 非(A或B)
示例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
示例:
1、找出/tmp目录下,属主不是root,且文件名不以f开头的文件
find /tmp \( -not -user root -a -not -name 'f*' \) -ls
find /tmp -not \( -user root -o -name 'f*' \) –ls
查找并显示文件的方法
查找到某个文件是我们的目的,我们更想知道查找到的文件的详细信息和属性,如果我们采取现查找文件,在使用ls命令来查看文件信息是相当繁琐的,现在我们也可以把这两个命令结合起来使用
示例:
1、搜索名为snow.png的文件
find -name snow.png
2、不分大小写地搜索名为snow.png、Snow.png、 SNOW.PNG等等的文件
find -iname snow.png
3、搜索根目录下以.txt结尾的文件
find / -name “.txt”
4、搜索/var目录下文件名带log的文件
find /var –name “log”
5、搜索被用户joe 以及组群joe所拥有的文件
find -user joe -group joe
6、搜索所属人是joe,但是所属组不是joe的文件
find -user joe -not -group joe
7、搜索所属人是joe,或者是jane的文件。
find -user joe -o -user jane
8、搜索不属于是joe且不属于jane的文件。
find -not ( -user joe -o -user jane )
9、搜索根目录所属人是joe或所属组为500的文件。
find / -user joe -o -uid 500
排除目录
示例:
1、查找/etc/下,除/etc/sane.d目录的其它所有.conf后缀的文件
find /etc -path ‘/etc/sane.d’ -a -prune -o -name “*.conf”
2、查找/etc/下,除/etc/sane.d和/etc/fonts两个目录的其它所有.conf后缀的文件
find /etc \(–path ‘/etc/sane.d’ –o –path ’/etc/fonts’ \) -a -prune –o -name “*.conf”
根据文件大小来查找
-size [-+]大小
-size # :(#-1,#] #按指定大小为7举例:-size 7M
大于6小于等于7
-size -# :[0,#-1] #按指定大小为7举例:-size -7M
大于等于0小于等于7
-size +#:(#,oo) #按指定大小为7举例:-size +7M
大于7小于等于正无穷
[root@centos6 app]#ll #搜索大小
total 25568
-rw-r--r-- 1 root root 18830560 Mar 3 11:55 access.log
-rwxr--r--. 1 root root 206 Mar 2 17:35 create.sh
-rwxr--r--. 1 root root 589 Mar 2 16:30 sysinfo.sh
-rw-r--r-- 1 root root 7340032 Mar 3 17:29 test.txt
[root@centos6 app]#find /app -size 7M #大于6小于等于7
/app/test.txt
[root@centos6 app]#find /app -size -7M #大于7小于等于正无穷
/app
/app/sysinfo.sh
/app/create.sh
[root@centos6 app]#find /app -size +7M #大于7小于等于正无穷
/app/access.log
根据时间戳查找
以 ”天“ 为单位
-atime(atime(访问时间),ctime(修改时间),mtime(修改元数据时间))
-atime #:[#,#+1): #按照指定天数为7,大于等于7,小于等于8
-atime +#:[#+1,oo]: #按照指定天数为7,大于等于8,小于等于正无穷
-atime -#:[0,#) #按照指定天数为7,大于等于0,小于7
以 “分钟” 为单位 访问时间,修改时间,修改元数据时间
-amin -mmin -cmin #跟以 ”天“ 为单位一样
示例:
find /etc/ -mtime -1 //查看etc目录下一分钟以内的操作
find / -amin -10 # 查找在系统中最后10分钟访问的文件
find / -atime -2 # 查找在系统中最后48小时访问的文件
find / -empty # 查找在系统中为空的文件或者文件夹
find / -group cat # 查找在系统中属于 groupcat的文件
find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
find / -mtime -1 #查找在系统中最后24小时里修改过的文件
find / -nouser #查找在系统中属于作废用户的文件
find / -user fred #查找在系统中属于FRED这个用户的文件
下面的列表就是对find命令所可以指定文件的特征进行查找的部分条件。在这里并没有列举所有的查找条件,参考有关Linux有关书籍可以知道所有find命令的查找函数。
-amin n | 查找系统中最后N分钟访问的文件 |
---|---|
-atime n | 查找系统中最后n*24小时访问的文件 |
-mmin n | 查找系统中最后N分修改过的文件 |
-mtime n | 查找系统中最后n*24小时修改过的文件 |
-cmin n | 查找系统中最后N分钟被改变状态的文件 |
-ctime n | 查找系统中最后n*24小时被改变状态的文件 |
-empty | 查找系统中空白的文件,或空白的文件目录,或目录中没有子目录的文件夹 |
-false | 查找系统中总是错误的文件 |
-fstype type | 查找系统中存在于指定文件系统的文件,例如:ext2 . |
-gid n | 查找系统中文件数字组 ID 为 n的文件 |
-group gname | 查找系统中文件属于gnam文件组,并且指定组和ID的文件 |
Find命令的控制选项说明:
Find命令也提供给用户一些特有的选项来控制查找操作。下表就是我们总结出的最基本,最常用的find命令的控制选项及其用法。
选项 | 用途描述 |
---|---|
-depth | 使用深度级别的查找过程方式,在某层指定目录中优先查找文件内容 |
-follow | 遵循通配符链接方式查找; 另外,也可忽略通配符链接方式查询 |
-help | 显示命令摘要 |
-maxdepth levels | 在某个层次的目录中按照递减方法查找 |
-mount | 不在文件系统目录中查找, 用法类似 -xdev. |
-noleaf | 禁止在非UNUX文件系统,MS-DOS系统,CD-ROM文件系统中进行最优化查找 |
-version | 打印版本数字 |
下面的表格就是一些常用的查找文件并显示文件信息的参数和使用方法
选项 | 用途描述 |
---|---|
-exec command; | 查找并执行命令 |
-fprint file | 打印文件完整文件名 |
-fprint0 file | 打印文件完整文件名包括空的文件 |
-fprintf file format | 打印文件格式 |
-ok command; | 给用户命令执行操作,根据用户的Y 确认输入执行 |
-printf format | 打印文件格式 |
-ls | 打印同种文件格式的文件. |
根据权限查找
-perm [/|-]MODE
MODE: 精确权限匹配
/MODE: 任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从centos7开始淘
-MODE: 每一类对象都必须同时拥有指定权限,与关系 0 表示不关注
find -perm755 会匹配权限模式恰好是755的文件
只要当任意人有写权限时,find-perm+222就会匹配
只有当每个人都有写权限时,find-perm -222才会匹配
只有当其它人(other)有写权限时,find-perm-002才会匹配
-perm 权限
[root@centos6 app]#find -perm 755 #只有是755权限才会匹配
.
./1.txt
[root@centos6 app]#find -perm -222 #所属人,所属组,其他人,三者都要有写权限才会匹配
./3.txt
[root@centos6 app]#find -perm +222 #三个文件都会显示,因为三个文件中都有写权限
./1.txt
./3.txt
./2.txt
处理动作
-print:默认的处理动作,显示值屏幕
-ls:类似于超找命令中的ls -l命令
-delete:删除查找到的文件
-fls file:查找到的所有文件的长格式信息保存至指定文件中
-ok 命令 {} ; 对查找到的每个文件执行该命令,都会交互式要求用户确认。 #常用方法
-exec 命令 {} ; 对查找到的每个文件执行该命令。不会交互式确认。
[root@centos6 app]#find -perm 222 -exec ls -l {} \; #-ok与这个相似,不过是交互式
--w--w--w- 1 root root 0 Mar 3 17:32 ./3.txt
{}:用于引用查找到文件名称自身身
find命令在传递查找到的文件只后面指定命令是,查找到所有符合条件的文件一次性传递给后面命令,有些命令不可以直接使用。
示例:
1、备份配置文件,添加.orig这个扩展名
find -name “*.conf” -exec cp {} {}.orig \;
2、提示删除存在时间超过3天以上的joe的临时文件
find /tmp -ctime+3 -userjoe -ok rm {} \;
3、在你的主目录中寻找可被其它用户写入的文件
find ~ -perm-002 -execchmodo-w {} \;
find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \;
find /home –type d -ls
示例:
1、查找/var目录下属主为root,且属组为mail的所有文件
[root@centos6 spool]# find /var -user root -and -group mail -ls
394408 4 drwxrwxr-x 2 root mail 4096 Jan 27 09:06 /var/spool/mail
396328 4 -rw------- 1 root mail 3263 Jan 26 09:29 /var/spool/mail/root
2、查找/var目录下不属于root、lp、gdm的所有文件
[root@centos6 /]# find /var -not -user root -not -user lp -not -user gdm -ls
394492 4 drwxr-xr-x 2 abrt abrt 4096 Mar 23 2017 /var/spool/abrt
396391 0 -rw-rw---- 1 linkang mail 0 Jan 26 09:43 /var/spool/mail/linkang
396380 0 -rw-rw---- 1 nologin mail 0 Jan 20 20:09 /var/spool/mail/nologin
396379 0 -rw-rw---- 1 sh mail 0 Jan 20 13:53 /var/spool/mail/sh
396355 0 -rw-rw---- 1 wei mail 0 Jan 17 08:28 /var/spool/mail/wei
396338 0 -rw-rw---- 1 500 mail 0 Jan 13 17:07 /var/spool/mail/liubei
3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
[root@centos6 /]# find /var -mtime -7 -not ( -user root -o -user postfix )
/var/spool/mail/linkang
/var/spool/mail/qwer
/var/spool/mail/linakng
/var/spool/mail/yio
/var/spool/mail/werty
/var/spool/mail/asdf
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
[root@centos6 ~]# find / -type f -nouser -nogroup -atime -7
/home/lin/.bash_history
/home/lin/789
/home/lin/456
5、查找/etc目录下大于1M且类型为普通文件的所有文件
[root@centos6 ~]# find /etc -type f -size +1M
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24
6、查找/etc目录下所有用户都没有写权限的文件
方法1:
find /etc/ -not ( -perm -002 -o -perm -020 -o -perm -200 ) | xargs ls -l
方法2:
[root@centos6 ~]# find /etc -not -perm +222 -ls -exec ls -l {} \;
1190012 4 -r--r--r-- 1 root root 80 Feb 22 2016 /etc/lvm/profile/thin-performance.profile
1190009 4 -r--r--r-- 1 root root 2391 Mar 23 2017 /etc/lvm/profile/command_profile_template.profile
7、查找/etc目录下至少有一类用户没有执行权限的文件
find /etc/ -not ( -perm -001 -a -perm -010 -a -perm -100 ) | xargs ls -l
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
find /etc/init.d/ -perm -113 |xargs ls -l
find /etc/init.d/ -perm -113 -exec ls -l {} \;
find /etc/init.d/ -perm -113 -ls
1、备份配置文件,添加.orig这个扩展名
find -name “.conf” -exec cp {} {}.orig \;
2、提示删除存在时间超过3天以上的joe的临时文件
find /tmp -ctime +3 -user joe -ok rm {} \;
3、在你的主目录中寻找可被其它用户写入的文件
find ~ -perm -002 -exec chmod o-w {} \;
find /data –type f -perm 644 -name “.sh” –exec chmod 755 {} \;
find /home –type d -ls
3、xargs传递参数
由于很多命令不支持管道|来传递参数,而日常工作中有这个必要,所以就有了xargs命令
xargs用于产生某个命令的参数,xargs 可以读入 stdin(标准输入) 的数据,并且以空格符或回车符将 stdin 的数据分隔成为arguments(参数)
注意:文件名或者是其他意义的名词内含有空格符的情况
有些命令不能接受过多参数,命令执行可能会失败,xargs可以解
语法:
find /etc –perm +777 | xargs 命令(command)
参数替换:xargs
由于很多命令不支持管道 | 来传递参数,所有在管道后面加上xargs命令就可以完成相接。
示例:
ls f* |xargs rm
find /sbin -perm +700 |ls -l #这个命令是错误的
find /sbin -perm +7000 | xargs ls –l #因为ls不支持管道,所以需要用xargs相接才可以