find命令思维导图
Find命令是linux中最常用且重要的命令之一,用于检索文件所在的位置,可以根据多种参数组合进行检索:文件名称,文件权限,文件属组,文件类型,文件大小等。
虽然man find
手册有关于find的详细说明,可缺乏实例的说明文档显得干巴巴,对初学者很不友好。导致初学者对于find产生这样的印象:“我知道find很强大,但不知道用在什么场景,该怎么用”。
再强大的工具,只有会用,用得好,才能体现出其价值。
基于此,本文将用实例讲解find命令常用场景:
$ find /etc -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd
find会对指定路径进行递归查找
$ find . -iname test.txt
./TesT.txt
./Test.txt
./test.txt
$ find . -type d -name dir1
./dir1
$ find . -type f -name test.php
./test.php
$ find . -type f -name "*.php"
./test.php
./test1.php
./test2.php
*
表示通配符
$ find . -type f -perm 777 -print
$ find . -type f ! -perm 777
!
反选
即查找所有用户都拥有x
权限的文件
$ find . -type f -perm /a=x
$ ll
-rwxrwxrwx 1 root root 0 9月 17 22:01 test
$ find -type f -perm 777 -print -exec chmod 644 {} \;
./test
$ ll
-rw-r--r-- 1 root root 0 9月 17 22:01 test
$ find . -type f -name "test" -exec rm -f {} \;
$ find . -type f -name "*.txt" -exec rm -f {} \;
$ find /tmp -type f -empty
$ find /tmp -type d -empty
$ find . -type f -name ".*"
$ find /etc -user senlong -name passwd
$ find /etc -user root -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd
$ find /etc -group root -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd
$ find . -mtime 50
$ find . -mtime +50 -mtime -100
$ find / -size 50M
$ find / -size +50M -size -100M
$ find / -type f -name "*.log" -size +100M -exec rm {} \;