Linux文件查找

来源:互联网 发布:centos libudev.h 编辑:程序博客网 时间:2024/04/27 14:50

在linux中,文件查找主要有两个命令:locate命令和find命令

一、locate命令

        locate命令查找文件是基于全系统文件数据库进行的,文件数据库是非实时的,且其匹配类型为模糊匹配,locate命令相对于find唯一的优点是查找速度非常快

1、默认数据库文件位置

/var/lib/mlocate/mlocate.db

2、 常用参数

[root@centos ~]# locate --helpUsage: locate [OPTION]... [PATTERN]...Search for entries in a mlocate database.  -b, --basename         match only the base name of path names  -c, --count            only print number of found entries  -i, --ignore-case      ignore case distinctions when matching patterns  -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries  -r, --regexp REGEXP    search for basic regexp REGEXP instead of patterns      --regex            patterns are extended regexps  -s, --stdio            ignored, for backward compatibility  -V, --version          print version information  -w, --wholename        match whole path name (default)
3、 更新数据库
[root@centos ~]# updatedb

二、搜索find命令

1、find命令格式

       find 查找路径 查找标准 查找到以后的处理运作
              查找路径:默认为当前目录
              查找标准:默认为指定路径下的所有文件
              处理运作:默认为显示


2、 匹配标准

☞ 文件名匹配

-name 'FILENAME':对文件名作精确匹配

也可以使用*,?,[]等通配符进行文件名匹配

[root@centos test]# find /etc/ -name passwd  /etc/pam.d/passwd  /etc/passwd  [root@centos test]# find /etc/ -name passwd*  /etc/pam.d/passwd  /etc/passwd-  /etc/passwd  [root@centos test]# find /etc/ -name *passwd  /etc/security/opasswd  /etc/pam.d/passwd  /etc/passwd  [root@centos test]# find /etc/ -name *passwd*  /etc/security/opasswd  /etc/pam.d/passwd  /etc/passwd-  /etc/passwd 

☞ 文件名匹配(不区分大小写)

-iname 'FILENAME': 文件名匹配时不区分大小写

☞ 正则匹配

-regex PATTERN:基于正则表达式进行文件名匹配

☞ 用户名或用户组名匹配

-user USERNAME: 根据属主查找

-group GROUPNAME: 根据属组查找

[root@centos ~]# find ./ -user storm  # 查找当前目录下所属用户为storm的所有文件

☞ 用户id或用户组id匹配

-uid UID: 根据UID查找

-gid GID: 根据GID查找

当用户被删除,所属该用户的所有文件的属主用户会变为该用户删除前的uid,此时根据-user是查找不到结果的,但是根据-uid依然可以找到结果

☞ 匹配没有用户或组所属的文件

-nouser:查找没有属主的文件

-nogroup: 查找没有属组的文件

[root@centos test]# find ./ -nouser -ls1066027    0 -rw-r--r--   1 2527     2527            0 5月 10 19:20 ./no-user-file1066028    4 drwxr-xr-x   2 2527     2527         4096 5月 10 19:20 ./no-user-dir

☞ 匹配文件类型

-type  文件类型

   f: 普通文件

   d:目录

   c:字符设备

   b:块设备

   l:链接文件

   p:管道设备

  s:套接字设备

☞ 匹配文件大小

-size [+|-] [ k | M | G ]

[root@centos ~]# find /etc -size 10k -ls   # 精确查找大小为10k的文件(9k-10k之间都能匹配)[root@centos ~]# find /etc -size +10k -ls  # 大于10k[root@centos ~]# find /etc -size -10k -ls  # 小于10k

☞ 根据文件时间属性查询(单位:天)

-mtime  [+|-]天数:  修改时间

-ctime  [+|-]天数:   改变时间 

-atime  [+|-]天数:  访问时间

[root@centos ~]# find ./ -mtime 5  #表示到此刻刚好5天访问的文件[root@centos ~]# find ./ -mtime +5  #表示5天前访问的过的文件[root@centos ~]# find ./ -mtime -5  #表示5天前到此刻访问过的文件

☞ 根据文件时间属性查询(单位:分钟)

-mmin  [+|-]分钟数

-cmin [+|-]分钟数

-amin [+|-]分钟数

3、运作

☞ 显示结果

-print: 显示 (默认)

☞ 显示详细信息

-ls:类似ls -l的形式显示每一个文件的详细

☞ 交由命令处理

-ok COMMAND {} \;  

-exec COMMAND {} \;

{}为占位符(也代表查找的文件名,可做字符串拼接等操作),结尾一定要以\;结束,否则视为语法错误 

区别:-ok每一次操作都需要用户确认,-exec不需要确认,

通常情况下,我们会把find查询到的结果通过管道传送给xargs命令进行处理

4、 组合多条件查询

-a  and 与
 -o  or  或
-not    非

多条件查询默认为与条件 -a

[root@centos ~]# find ./ -type f -a -user root -a -not -size 1k [root@centos ~]#find ./ -not \(-user user1 -o user user2\)

0 0