linux命令之-find文件查找

linux命令之-find文件查找

1、文件查找:locate, find

  • locate: 非实时查找工具;依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新此数据库(updatedb);查找速度快;模糊查找;

  • find:实时查找;查找速度略慢;精确查找;
    image_1dlhn8hun11i6k191j4f5bvbmp9.png-687.8kB

2、find命令使用方法

1
find [OPTION]... [查找路径] [查找条件] [处理动作]

查找路径:默认为当前路径;
查找条件:指定的查找标准,可以根据文件名、大小、属主属组、类型等进行;默认为找出指定路 径下的所有文件;
处理动作:对符合条件的文件做什么操作;默认为输出至屏幕;

3、查找条件

  • 根据文件名进行查找:
    -name “文件名称”: 支持使用glob;
    *, ?, []
    -iname “文件名称”:不区分字符大小写,支持使用glob;
    -regex “PATTERN”:以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称;

  • 根据属主、属组查找:
    -user USERNAME: 查找属主为指定用户的文件;
    -group GROUPNAME:
    -uid UserID: 查找文件的属主指定uid的文件;
    -gid GroupID:
    -nouser: 查找没有属主的文件;
    -nogroup:查找没有属组的文件;

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost tmp]# find /tmp/ -user centos -ls
654082 0 -rw------- 1 centos root 0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -group centos -ls
654211 4 -rw-r--r-- 1 root centos 126 9月 1 12:46 /tmp/111
[root@localhost tmp]# find /tmp/ -user centos -a -group centos -ls
654210 20 -rw-r--r-- 1 centos centos 19697 4月 10 00:44 /tmp/functions
[root@localhost tmp]# find /tmp/ -uid 3008 -ls
654210 20 -rw-r--r-- 1 3008 3008 19697 4月 10 00:44 /tmp/functions
654082 0 -rw------- 1 3008 root 0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -nouser -ls
654210 20 -rw-r--r-- 1 3008 3008 19697 4月 10 00:44 /tmp/functions
654082 0 -rw------- 1 3008 root 0 8月 25 21:08 /tmp/yum.log
[root@localhost tmp]# find /tmp/ -nouser -a -nogroup -ls
654210 20 -rw-r--r-- 1 3008 3008 19697 4月 10 00:44 /tmp/functions

  • 根据文件类型进行查找:
    -type TYPE
    f: 普通文件
    d: 目录
    l: 符号链接
    b: 块设备
    c: 字符设备
    p: 命名管道
    s: 套接字

例子:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost tmp]# find /tmp/ -type l -ls
654205 0 lrwxrwxrwx 1 root root 22 8月 25 21:24 /tmp/grub.conf -> ../boot/grub/grub.conf
[root@localhost /]# find /tmp/ -type d -ls
915714 4 drwxr-xr-x 9 root root 4096 9月 3 18:09 /tmp/
915721 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/89123jk4
915715 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/jkashd
915719 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/sdfhk
915718 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/1892373
915717 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/kjashd
915720 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/90124h3kjshdr
915716 4 drwxr-xr-x 2 root root 4096 9月 3 18:09 /tmp/kjashdkj

  • 组合查找条件:
    与条件:-a
    或条件:-o
    非条件:-not, !
    !A -o !B = !(A -a B)
    !A -a !B = !(A -o B)

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost tmp]# find /tmp/ -type f -a -nouser -ls
915724 4 -rw------- 1 3012 hadoop 827 8月 30 15:46 /tmp/grub.conf
[root@localhost tmp]# find /tmp/ \( -nouser -o -user lisir \) -ls
915724 4 -rw------- 1 3012 hadoop 827 8月 30 15:46 /tmp/grub.conf
915723 4 -rw-r--r-- 1 lisir lisir01 957 8月 25 13:35 /tmp/fstab
915722 20 -rw-r--r-- 1 lisir 3012 19697 4月 10 00:44 /tmp/functions
[root@localhost tmp]# find /tmp/ \( -nouser -a -group hadoop \) -ls
915724 4 -rw------- 1 3012 hadoop 827 8月 30 15:46 /tmp/grub.conf
[root@localhost tmp]# find /etc ! -name "*.conf" -a -not -type f -ls
392449 12 drwxr-xr-x 117 root root 12288 9月 3 18:14 /etc
393096 4 drwxr-xr-x 2 root root 4096 8月 25 21:12 /etc/purple
393295 0 lrwxrwxrwx 1 root root 15 8月 25 21:13 /etc/rc.sysinit -> rc.d/rc.sysinit
394019 4 drwxr-xr-x 2 root root 4096 8月 25 21:16 /etc/pcmcia
393055 4 drwxr-xr-x 2 root root 4096 8月 25 21:12 /etc/obex-data-server
393034 4 drwxr-xr-x 3 root root 4096 8月 25 21:11 /etc/ghostscript
393040 4 drwxr-xr-x 2 root root 4096 8月 25 21:11 /etc/ghostscript/8.70
392886 4 drwxr-xr-x 2 root root 4096 8月 25 21:11 /etc/wpa_supplicant
392787 4 drwxr-xr-x 2 root root 4096 8月 25 21:11 /etc/ssl

  • 根据文件大小来查找:
    -size [+|-]#UNIT:
    常用单位有:k, M, G
    1
    2
    3
    4
    5
    6
    7
    8
    #UNIT: #-1 < x <= #
    2k:1.8k, 1.9k, 1.1k

    -#UNIT:x <= #-1
    -2k: 1k, 0.9k

    +#UNIT: x > #
    +2k: 2.1k, 3.1k

例子:

1
2
3
4
5
6
7
8
9
[root@localhost /]# find /etc/ -size 3k -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.9K 8月 21 2010 /etc/pinforc
-rwxr-xr-x. 1 root root 2.6K 4月 10 00:44 /etc/sysconfig/network-scripts/ifup-tunnel
-rwxr-xr-x. 1 root root 2.4K 4月 10 00:44 /etc/sysconfig/network-scripts/ifup-post
-rw-r--r--. 1 root root 2.2K 7月 24 16:22 /etc/sysconfig/nfs
-rw-r--r--. 1 root root 2.6K 7月 24 19:32 /etc/sysconfig/cpuspeed
-rw-r--r--. 1 root root 2.6K 5月 20 22:18 /etc/sysconfig/raid-check
-rw-------. 1 root root 2.3K 8月 25 21:25 /etc/lvm/backup/VolGroup
-r--r--r--. 1 root root 2.2K 7月 24 12:04 /etc/lvm/profile/command_profile_template.profile

  • 根据时间戳
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #以“天”为单位
    -atime [+|-]#
    :[#,#+1)
    +#: [#+1,oo]
    -#: [0,#)

    #:
    -mtime
    -ctime
    +3大于三天 -3三天以内 3大于等三4天以内
1
2
3
4
#以“分钟”为单位
-amin
-mmin
-cmin

注:a表示最近访问文件的时间、m表示最近修改文件内容的时间、c表示最近文件有所改变的状态 ,如文件修改,属性\属主 改变 ,节点 ,链接变化等

例子:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost /]# touch -m -t 201509031828 /tmp/lisir
[root@localhost /]# stat /tmp/lisir
File: "/tmp/lisir"
Size: 0 Blocks: 0 IO Block: 4096 普通空文件
Device: fd00h/64768d Inode: 915725 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-09-03 18:28:23.640000731 +0800
Modify: 2015-09-03 18:28:00.000000000 +0800
Change: 2015-09-03 18:28:23.640000731 +0800
[root@localhost /]# find /tmp/ -mtime -3 -ls
915725 0 -rw-r--r-- 1 root root 0 9月 3 18:28 /tmp/lisir

  • 根据权限:
    -perm [/|-]MODE
    MODE: 精确权限匹配
    /MODE+MODE:任何一类对象(u,g,o)的任何一位权限符合条件即可;隐含或条件;
    /444+444:表示只要符合u,g,o任何一个权限有读取即可
    -MODE:为每一类对象指定的每一位权限都必须同时存在方为符合条件;隐含与条件;
    -444:表示必须符合u,g,o每一位权限都有读即可

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost soft]# find /soft/ -perm /222 -ls
2354690 4 drwxr-xr-x 2 root root 4096 9月 3 18:38 /soft/
2354691 0 -rwx------ 1 root root 0 9月 3 18:38 /soft/a
2354692 0 -rwxrwx--- 1 root root 0 9月 3 18:38 /soft/b
2354693 0 -rwxrwxrwx 1 root root 0 9月 3 18:38 /soft/c
2354694 0 -rw-r--r-- 1 root root 0 9月 3 18:38 /soft/d
2354695 0 -rw-r--r-- 1 root root 0 9月 3 18:38 /soft/e
[root@localhost soft]# find /soft/ -perm -222 -ls
2354693 0 -rwxrwxrwx 1 root root 0 9月 3 18:38 /soft/c
[root@localhost soft]# find /soft/ ! -perm -222 -ls
2354690 4 drwxr-xr-x 2 root root 4096 9月 3 18:38 /soft/
2354691 0 -rwx------ 1 root root 0 9月 3 18:38 /soft/a
2354692 0 -rwxrwx--- 1 root root 0 9月 3 18:38 /soft/b
2354694 0 -rw-r--r-- 1 root root 0 9月 3 18:38 /soft/d
2354695 0 -rw-r--r-- 1 root root 0 9月 3 18:38 /soft/e

4、处理动作

-print: 默认处理动作;
-ls:类似于对查找到的每个文件做”ls -l”的操作;
-delete: 删除查找到的文件;
-fls /path/to/somefile:查找到的文件的详细路径信息保存至指定文件中;
-ok COMMAND {} \; 对每个文件执行指定的命令之前需要用户事先确认;
-exec COMMAND {} \; 无需用户确认;

例子:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost soft]# find . -perm -222 -exec mv {} {}.a \;
[root@localhost soft]# ll
总用量 0
-rwx------. 1 root root 0 9月 3 18:43 a
-rwxrwx---. 1 root root 0 9月 3 18:43 b
-rwxrwxrwx. 1 root root 0 9月 3 18:43 c.a
[root@localhost soft]# find . -perm -222 -exec chmod 777 {} \;
[root@localhost soft]# ll
总用量 0
-rwx------. 1 root root 0 9月 3 18:43 a
-rwxrwx---. 1 root root 0 9月 3 18:43 b
-rwxrwxrwx. 1 root root 0 9月 3 18:43 c.a

5、练习题

  • 1、查找/var目录属主为root,且属组为mail的所有文件;
    答:find /var -user root -group mail -ls

  • 2、查找/usr目录下不属于root、bin或hadoop的所有文件;
    答:find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls

  • 3、查找/etc目录下最近一周内其内容修改过,且属主不为root或hadoop的所有文件;
    答:find /etc -ctime -7 -a -not -user root -not -user hadoop -ls

  • 4、查找当前系统上没有属主或属组,且最近一周内曾被访问过的所有文件;
    答:find / ( -nouser -o -nogroup ) -a -atime -7 -ls

  • 5、查找/etc目录下大于20k且类型为普通谁的的所有文件;
    答:find /etc -type f -a -size +20k -exec ls -lh {} \;

  • 6、查找/etc目录下所有用户都没有写权限的文件;
    答:find /etc -not -perm /222 -ls

  • 7、查找/etc目录下至少有一类用户没有执行权限的文件;
    答:find /etc -not -perm -111 -ls

  • 8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户拥有写权限的文件
    答:find /etc/init.d/ -perm /111 -a -perm /002 -ls

-------------本文结束感谢您的阅读-------------
LiGuanCheng wechat
如有问题,请与我微信交流或通过右下角“daovoice”与我联系~。
请我喝一杯咖啡~