find 命令

作用:实时查找工具,通过遍历指定路径下的文件系统完成文件查找

工作特点:

  1. - 查找速度慢
  2. - 精确查找
  3. - 实时查找

使用

[root@localhost ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

find [选项][路径][查找条件+处理动作]

     - 路径:指定具体目录路径,默认是当前文件夹
     - 查找条件:指定查找的标准(文件名/大小/文件类型/权限等等),默认是找出所有文件
     - 处理动作:对符合条件的文件做什么操作,默认是输出屏幕

查找条件

根据文件名查找:

-name “filename” 根据文件名称查找,支持统配等
-iname “filename” 根据文件名查找,忽略大小写
[root@tenor ~]# find -name home
./home
[root@tenor ~]# find -iname home
./home
./HOme

根据属主和属组查找

-user username 查找属主为指定用户的文件
-group groupname 查找属组为指定属组的文件
-uid UID 查找属主为指定UID的文件
-gid GID 查找属组为指定GID的文件
-nouser 查找没有属主的文件
-nogroup 查找没有属组的文件

根据文件类型查找

-type TYPE 查找为指定类型的文件

根据组合条件

-a
-o
-not
例子:
[root@tenor ~]# find /home -type f -a -name 1.txt
/home/lars/1.txt
[root@tenor ~]# find /home -type f -o -name 1.txt
/home/lars/.bashrc
/home/lars/.bash_profile
/home/lars/.bash_history
/home/lars/.cache/abrt/lastnotification
/home/lars/.bash_logout
/home/lars/1.txt
[root@tenor ~]# find /home -type f -not  -name 1.txt
/home/lars/.bashrc
/home/lars/.bash_profile
/home/lars/.bash_history
/home/lars/.cache/abrt/lastnotification
/home/lars/.bash_logout



练习1:找出tmp目录下,属主不是root, 且文件不是fstab的文件
[root@tenor ~]# find /tmp /-not -user root -a -not -name fstab/

根据文件大小进行查找

-size +N [bkMG] 大于 N [bkMG]
-size - N [bkMG] 小于 N [bkMG]

根据时间戳

-atime [+/-] N 访问时间 N天内/前
-mtime [+/-] N 修改文件内容时间 N天内/前
-ctime [+/-] N 元数据改变时间 N天内/前
  • eg:删除/var/log/java/下三天前产生的日志

find /var/log/java/ -atime +3 -delete

分钟

-amin [+/-] N 访问时间 N分钟内/前
-mmin [+/-] N 修改文件内容时间 N分钟内/前
-cmin [+/-] N 元数据改变时间 N分钟内/前

根据权限查找

        - 根据权限查找
           - -perm [+|-] MODE
              - MODE:精确权限匹配
              - /MODE:任何一类()对象的权限中能够匹配一位即可
              - -MODE:每一类的对象的权限必须同时拥有指定的权限才能够匹配
        - 处理动作:
              - -print:默认动作,输出到屏幕中
              - -ls:列出符合条件的文件
              - -delete:删除查找到文件
              - -fls /path/to/somefile : 对符合条件的文件保存到指定文件中
              - -ok command{}\:对查找后文件进行一次命令操作,每次操作都需要确认
              - -exec command{}\:对查找到的文件进行一次command操作
                 - command {}\:{}代表引用查找到的文件名称自己
                 - <br />
1. txt
2. txt
find /tmp/ \( -not -user root -a -not -name fstab \) -exec mv {} {}.bak \;
mv 1.txt 1.txt.bak
mv 2.txt 2.txt.bak
     - ps:find查找内容,进行命令操作的时候,是找到所有匹配文件然后一次传递给后面的命令进行操作,另一种方式  find ... I xargs command
[root@localhost tmp]# find /tmp/ \( -not -user root -a -not -name fstab \) I xargs rm -rf
     - <br />
     - eg:
1.查找/var目录下属主为root, 且属组为mai1的所有文件或目录
[root@localhost ~]# find /var/ -group mail -user root 
2.查找/usr目录下不属于root, bin的所有文件或目录
[root@localhost ~]# find /usr/ -not -user root -o -not -user  bin
[root@localhost ~]# find /usr/ -not \( -user root -a -user bin \)
3.查找/etc目录下最近一周 .内容曾被访问过的文件或目录
[root@localhost ~]# find /etc/ -atime -7
4.查找当前系统上没有属主或属组,且最近周内曾被访问过的文件或目录
[root@localhost ~]# find / -atime -7 -nouser -o -nogroup
5、查找/etc目录下大于1M且类型为普通文件的所有文件或目录  
[root@localhost ~]# find /etc -type f -size +1M
6、查找/etc目录下所有用户都没有写权限的文件     
find /etc ‐not ‐perm /222 
7、查找/etc目录下至少一类用户没有执行权限的文件   
find /etc ‐not ‐perm ‐111 
8、查找/etc/init.d目录下,所有用户都执行权限,且其它用户写权限的文件      
find /etc/init.d ‐perm ‐113
参数 作用
-name 匹配名称
-perm 匹配权限(mode为完全匹配,-mode为包含即可)
-user
匹配所有者
-group 匹配所有组
-mtime

-n +n | 匹配修改内容的时间(-n指n天以内,+n指n天以前) | | -atime -n +n | 匹配访问文件的时间(-n指n天以内,+n指n天以前) | | -ctime -n +n | 匹配修改文件权限的时间(-n指n天以内,+n指n天以前) | | -nouser | 匹配无所有者的文件 | | -nogroup | 匹配无所有组的文件 | | -newer f1 !f2 | 匹配比文件f1新但比f2旧的文件 | | —type b/d/c/p/l/f | 匹配文件类型(后面的字母参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件) | | -size | 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件) | | -prune | 忽略某个目录 | | -exec …… {}\; | 后面可跟用于进一步处理搜索结果的命令 |

find练习题:
1.查找/var目录下属主为root,且属组为mail的所有文件或目录
[root@localhost ~]# find  /var/ -user root -a -group mail
/var/spool/mail
2.查找/usr目录下不属于root,bin或centos所有文件或目录
[root@localhost ~]# find  /usr/ -not -user root -o -user bin -o  -user centos 
3.查找/etc目录下最周一内容曾被访问过的文件或目录
find /etc/ -atime +7
4.查找当前系统上没有属主或属组,且最近一周内曾被访问过的文件或目录
find / -atime -7   -nouser -o -nogroup 
5.查找/etc目录下大于1M且类型为普通文件的所有文件或目录
find /etc -type f -size +1M
6.查找/etc目录下所有用户都没有写权限的文件
[root@localhost ~]# find /etc -not -perm /222-
7.查找/etc目录下至少一类用户没有执行权限的文件
find /etc -not -perm -111
8.查找/etc/init.d目录下,所有用户都执行权限,且其它用户写权限的文件
 find /etc/init.d  -perm -113