find命令:

search for files in a directory hierarchy

搜索指定目录中的文件

语法:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

常用选项:

-type指定需要查找的文件或目录。d  f

-name 指定需要查找文件或目录名称,-name 只支持一个文件的查找,如下实例2

-mtime n    +n表示在n天之前,-n表示在n天之内,n表示在第n天修改的文件

-size +/-n 大于或小于大小的文件或目录

-empty 查找空文件或目录

-perm 查找是属于多少权限的文件或目录

-iregex 可加正则

-exec +command   如:find / -type f -exec ls -l {} \;

        {}表示由find查找到的内容

        -exec和\;都是关键字,中间的是find的额外命令。就是ls -l {}

        ;在Linux中是又特殊意义的,所以使用\来转义。

实例:

[root@www1 ~]# find -size 0
./1
./b
./ceshi/a
./python/test/__init__.py
./.elinks/socket0
./.elinks/bookmarks
[root@www1 ~]# find /  -name "myfile.py"
/root/python/myfile.py
[root@www1 ~]# find -size 0 -exec rm -r {} \;
[root@www1 ~]# find -size 0
[root@www1 ~]#
[root@www1 ~]# find -type f -exec ls {} \;
[root@www1 ~]# find -type f |xargs ls
./1.log   ./httpd-2.4.33.tar.gz   ./python/__pycache__/third.cpython-36.pyc     ./test666.txt   
[root@www1 ~]# find -type f -mtime 3 |xargs ls
[root@www ~]#
[root@www ~]# find / -name '*.txt' -type f -exec ls -l {} \;        
-rw-r--r--. 1 root root 17315 Feb 24  2013 /usr/share/X11/rgb.txt

实例2:

[root@www ~]# ls *.txt
a.txt  test.txt  test1.txt  test_umask.txt#多个文件
[root@www ~]# find . -name a.txt test.txt #多个文件,报错了。
find: paths must precede expression: test.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@www ~]# find . -name 'a.txt test.txt'    #加上单引号,就会当作一个文件了。查不到
[root@www ~]#
[root@www ~]# find . -name *.txt   #这样也报错,为什么?因为当前目录下有多个.txt。通用匹配到test.txt a.txt就会和上面的实例一样了。
find: paths must precede expression: test1.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@www ~]#
[root@www ~]# find . -name '*.txt'    #正常了
./yang/test.txt
./test1.txt
./a.txt
./test_umask.txt
./test.txt
[root@www ~]#
[root@www ~]# touch a.text
[root@www ~]# find . -name *.text   #就一个.text文件,建议多文件查找时要加入单引号。
./a.text
[root@www ~]#