5分钟学会find命令

find的使用格式如下:

  $ find <指定目录> <指定条件> <指定动作>

  - <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。

  - <指定条件>: 所要搜索的文件的特征。

  - <指定动作>: 对搜索结果进行特定的处理。

如果什么参数也不加,find默认搜索当前目录及其子目录,并且不过滤任何结果(也就是返回所有文件),将它们全都显示在屏幕上。

1.当前目录下查找文件

[[email protected] ~]# find . -name test.txt 
./findtest/test.txt

2.指定目录下查找

[[email protected] ~]# find /root/ -name test.txt
/root/findtest/test.txt

3.忽略大小写查找

[[email protected] ~]# find /root -iname test.txt
/root/findtest/test.txt
/root/findtest/TEST.txt

4.查找目录

[[email protected] ~]# find / -type d -name test
/usr/lib64/python2.7/unittest/test
/usr/lib64/python2.7/test
/usr/src/kernels/3.10.0-229.14.1.el7.x86_64/include/config/test
/usr/src/kernels/3.10.0-229.14.1.el7.x86_64/lib/raid6/test

5.按名称查找php文件

[[email protected] zabbix]# find . -type f -name events.php 
./events.php

6.在目录中查找所有的php文件

[[email protected] zabbix]# find . -type f -name "*.php"
./graphs.php
./tr_logform.php
./authentication.php
./popup_httpstep.php
./image.php
..........

7.查找文件权限是777的

[[email protected] ~]# find . -type f -perm 0777 -print
./findtest/test.txt

8.查找文件权限不是777的

[[email protected] ~]# find . -type f ! -perm 0777 -print

9.查找644权限的SGID文件

[[email protected] ~]# find / -perm 2644

10.查找权限为551的粘着位文件

[[email protected] ~]# find / -perm 1551

11.查找所有SUID文件

[email protected] ~]# find / -perm /u=s

12.查找所有SGID文件

[[email protected] ~]# find / -perm /g+s

13.查找所有只读文件

[[email protected] ~]# find / -perm /u=r

14.查找所有可执行文件

[[email protected] ~]# find / -perm /a=x

15.查找所有777文件,并改为644

反斜杠用来告诉find何时命令结束

[[email protected] ~]# find / -type f -perm 0777 -print -exec chmod 644 {} \;

16.查找所有777的目录,并改为755

[[email protected] ~]# find / -type d -perm 777 -print -exec chmod 755 {} \;

17.查找并删除某个文件

[[email protected] ~]# find . -type f -name "test.txt" -exec rm -f {} \;

18.查找并删除多个文件

[[email protected] ~]# find . -type f -name "*.txt" -exec rm -f {} \;

19.查找所有的空文件

[[email protected] ~]# find /tmp -type f -empty

20.查找所有空目录

[[email protected] ~]# find /tmp -type d -empty

21.查找所有隐藏文件

[[email protected] ~]# find /tmp -type f -name ".*"

22.根据用户查找某个文件

[[email protected] ~]# find / -user root -name test.txt

23.根据用户查找所有的文件

在/home下属于某个用户的所有文件

复制代码
[[email protected] ~]# find /home -user zabbix
/home/zabbix
/home/zabbix/.bash_history
/home/zabbix/.config
/home/zabbix/.config/abrt
/home/zabbix/mysql-community-release-el7-5.noarch.rpm
/home/zabbix/.lesshst
/home/zabbix/.cache
/home/zabbix/.cache/abrt
/home/zabbix/.cache/abrt/lastnotification
/home/zabbix/.bash_logout
/home/zabbix/.viminfo
/home/zabbix/.mysql_history
/home/zabbix/.bashrc
/home/zabbix/.bash_profile
复制代码

24./home目录下查找某个组的所有文件

[[email protected] ~]# find /home -group developer

25./home目录下忽略大小写查找用户zabbix的所有文件

[[email protected] ~]# find /home -user zabbix -iname "*.txt"

26.查找50天之内修改过的文件

[[email protected] ~]# find / -mtime 50

27.查找50天之内被存取过的文件

[[email protected] ~]# find / -atime 50

28.查找50-100天之内修改过的文件

[[email protected] ~]# find / -atime +50 -mtime -100

29.查找1个小时之内有变化的文件

[[email protected] ~]# find / -cmin -60

30.查找1个小时之内修改过的文件

[[email protected] ~]# find / -mmin -60

31.查找1个小时之内被存取过的文件

[[email protected] ~]# find / -amin -60

32.查找所有的50M大小的文件

[[email protected] ~]# find / -size 50M

33.查找50-100M之间的文件

[[email protected] ~]# find / -size +50M -size -100M

34.查找并删除100M大小的文件

[[email protected] ~]# find / -size +100M -exec rm -rf {} \;

35.查找并删除指定类型,指定大小的文件

[[email protected] ~]# find / -type f -name *.mp3 -size +10M -exec rm {} \;

猜你喜欢

转载自www.linuxidc.com/Linux/2016-11/137051.htm