大纲

一、locate命令

二、find命令

三、locate与find优缺点对比

四、find实例


一、locate命令

locate - find files by name        # 通过文件名查找文件

SYNOPSIS
       locate [OPTION]... PATTERN...
       
[root@soysauce scripts]# locate inittab
-bash: locate: command not found            # 系统上没有安装locate
[root@soysauce scripts]# yum install -y mlocate    # 安装mlocate包
[root@soysauce scripts]# locate inittab            # 查找inittab这个文件
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory # 文件数据库未建立
[root@soysauce scripts]# updatedb          # 手动生成文件数据库
[root@soysauce scripts]# locate inittab    # 可以看到此时文件数据库已生成,查找到包含inittab字符串的文件
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/terminfo/a/ansi+inittabs
/usr/share/vim/vim70/syntax/inittab.vim


二、find命令

find - search for files in a directory hierarchy    #  在目录层次中寻找文件

SYNOPSIS
       find [-H] [-L] [-P] [path...] [expression]
find简单用法:
           find 查找路径 查找标准 查找到以后的处理运作
           
查找路径:默认为当前目录,可自定义
查找标准:默认为指定路径下的所有文件,下面1-7都是查找标准
处理运作:默认为显示
            -print: 显示
    	    -ls:类似ls -l的形式显示每一个文件的详细
    	    -ok COMMAND {} \; 每一次操作都需要用户确认
    	    -exec COMMAND {} \;
    	    |xargs  COMMAND
    	    -delete            删除

1.依据文件类型查找

-type
        f:普通文件    
        d:目录文件
        c:字符设备文件
        b:块设备文件
        l:软连接文件
        p:命名管道文件
        s:套接字文件
        
[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:00 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:00 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:00 c -> /etc/inittab 
[root@soysauce test]# find . -type d    # 查找当前目录下是目录的文件
.
./a


2.依据文件权限查找

(1).-perm MODE:精确匹配

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -perm 644    # 查找当前目录下权限为644的文件
./b

(2).-perm -MODE:文件权限能完全包含此MODE时才符合条件 

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -perm -644  # 查找当前目录下属主可读可写,属组和其他用户可读的文件
.
./a
./b
./c

(3).-perm /MODE:9位当中任意一位匹配即满足条件

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -perm /441   # 查找当前目录下属主或属组有读权限或其他人有执行权限的文件
.
./a
./b            # b的第三位虽然没有可执行权限,但是属主有读权限,所以也满足
./c


3.依据文件属主查找

(1).-user USERNAME

[root@soysauce test]# ll    
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -user user1    # 查找属主是user1的文件
./a

(2).-uid UID

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 root 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# id user2
uid=501(user2) gid=501(user2) groups=501(user2)
[root@soysauce test]# find . -uid 501    # 查找属主uid是501的文件
./b

(3).-nouser

[root@soysauce test]# userdel -r user1    # 删除user1用户
[root@soysauce test]# id user1         # 此时可以看到user1用户已经不存在
id: user1: No such user
[root@soysauce test]# ll
total 4
drwxr-xr-x 2   500 root 4096 Nov 21 15:22 a   # 此时a的属主位已经变成了原先属主的uid
-rw-r--r-- 1 user2 root    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root   12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -nouser        # 查找当前目录下没有属主的文件
./a


4.依据文件属组查找

(1).-group GROUPNAME

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 user1 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 user2    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root    12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -group user1    # 查找当前目录下属组为user1的文件
./a

(2).-gid GID

[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 user1 4096 Nov 21 15:22 a
-rw-r--r-- 1 user2 user2    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root    12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# id user2
uid=501(user2) gid=501(user2) groups=501(user2)
[root@soysauce test]# find . -gid 501    # 查找属组uid是501的文件    
./b


(3).-nogroup

[root@soysauce test]# userdel -r user2      # 删除user2用户
[root@soysauce test]# id user2            # 此时可以看到user2用户已经不存在
id: user1: No such user
[root@soysauce test]# ll
total 4
drwxr-xr-x 2 user1 user1 4096 Nov 21 15:22 a
-rw-r--r-- 1   501   501    0 Nov 21 15:22 b
lrwxrwxrwx 1 root  root    12 Nov 21 15:22 c -> /etc/inittab
[root@soysauce test]# find . -nogroup        # 查找当前目录下没有属组的文件     
./b


5.依据文件大小查找

(1).-size #k|M|G 

[root@soysauce test]# ls -lh
total 29M
drwxr-xr-x 2 user1 user1 4.0K Nov 21 15:22 a
-rw-r--r-- 1 user2 user2 9.7M Nov 21 16:06 b
lrwxrwxrwx 1 root  root    12 Nov 21 15:22 c -> /etc/inittab
-rw-r--r-- 1 root  root   19M Nov 21 16:08 d
[root@soysauce test]# find . -size 10M    # 查找当前目录下文件大小在9M-10M之间的文件
./b

(2).-size +#k|M|G 

[root@soysauce test]# ls -lh
total 29M
drwxr-xr-x 2 user1 user1 4.0K Nov 21 15:22 a
-rw-r--r-- 1 user2 user2 9.7M Nov 21 16:06 b
lrwxrwxrwx 1 root  root    12 Nov 21 15:22 c -> /etc/inittab
-rw-r--r-- 1 root  root   19M Nov 21 16:08 d
[root@soysauce test]# find . -size +10M        # 查找当前目录下文件大小大于10M的文件
./d

(3).-size -#k|M|G 

[root@soysauce test]# ls -lh
total 46M
drwxr-xr-x 2 user1 user1 4.0K Nov 21 15:22 a
-rw-r--r-- 1 root  root  8.0M Nov 21 16:13 b
-rw-r--r-- 1 user2 user2 9.7M Nov 21 16:06 c
-rw-r--r-- 1 root  root   19M Nov 21 16:08 d
-rw-r--r-- 1 root  root  9.0M Nov 21 16:16 f
[root@soysauce test]# find . -size -10M        # 查找当前目录下大小在0-9M之间的文件,包括9M
.
./a
./b
./f


6.依据文件时间戳查找

(1).-atime [+|-]#

[root@soysauce test]# find /etc/ -atime 10   # 查找/etc目录下距现在刚好10天访问过的文件
[root@soysauce test]# find /etc/ -atime +10  # 查找/etc目录下距现在至少10天没访问过的文件
[root@soysauce test]# find /etc/ -atime -10  # 查找/etc目录下距现在10天内访问过的文件

(2).-mtime [+|-]#

[root@soysauce test]# find /etc/ -mtime 10   # 查找/etc目录下距现在刚好10天修改过的文件
[root@soysauce test]# find /etc/ -mtime +10  # 查找/etc目录下距现在至少10天没修改过的文件
[root@soysauce test]# find /etc/ -mtime -10  # 查找/etc目录下距现在10天内修改过的文件

(3).-ctime [+|-]#

[root@soysauce test]# find /etc/ -ctime 10   # 查找/etc目录下距现在刚好10天改变过的文件
[root@soysauce test]# find /etc/ -ctime +10  # 查找/etc目录下距现在至少10天没改变过的文件
[root@soysauce test]# find /etc/ -ctime -10  # 查找/etc目录下距现在10天内改变过的文件

同样的以分钟为单位进行查找还有一组amin、mmin、cmin


7.依据文件名查找

(1).-name

[root@soysauce test]# find /etc -name "passwd"
/etc/pam.d/passwd
/etc/passwd

(2).-iname

[root@soysauce test]# ls -lh
total 8.1M
drwxr-xr-x 2 user1 user1 4.0K Nov 21 15:22 a
-rw-r--r-- 1 root  root  8.0M Nov 21 16:13 b
-rw-r--r-- 1 root  root     0 Nov 21 16:21 B
[root@soysauce test]# find . -iname "b"       # 查找当前目录下文件名是b的文件,忽略大小写
./B
./b


三、locate与find优缺点对比

1.locate命令

  • 优点:速度快

  • 缺点:非实时,模糊匹配,查找是根据全系统文件数据库进行的


2.find命令

  • 缺点:遍历指定目录中的所有文件完成查找,速度慢(相对于locate)

  • 优点:实时,精确查找,支持众多查找标准



四、find实例

1、查找/var目录下属主为root并且属组为mail的所有文件

[root@soysauce ~]# find /var -user root -group mail

2、查找/usr目录下不属于root,bin,或student的文件

[root@soysauce ~]#  find /usr -not \( -user root -o -user bin -o -user student \)

3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件

[root@soysauce ~]#  find /etc -mtime -7 -not \ ( -user root -o -user student \)

4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root

[root@soysauce ~]#  find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;

5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中

[root@soysauce ~]#  find /etc -size +1M >> /tmp/etc.largefiles

6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息

[root@soysauce ~]#  find /etc -not -perm /222 -ls