find 命令用来在指定目录下查找文件,任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件,并且将查找到的子目录和文件全部进行显示。
查找的基本语法为:find dir [options] expression
,在 dir
目录查找符合 expression
的文件,默认会递归查找。
一、基础用法
1.1 根据名字和类型查找
-name filename
:直接查找该文件名的文件。-type filetype
:通过文件类型查找文件。文件类型包括:f:普通文件 b:块设备文件 c:字符设备文件 d:目录 l:链接文档 s:套接字文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 查找当前目录下以.tar.gz 结尾的名字 ma@ubuntu:/data/software$ find . -name "*.tar.gz" ./pip-9.0.1.tar.gz ./php-5.6.33.tar.gz ./node-v9.4.0-linux-x64.tar.gz ./LuaJIT-2.0.5.tar.gz ./mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz ./go1.10.1.linux-amd64.tar.gz ./gcc-centos6.tar.gz ./cmake-3.11.0-rc3.tar.gz # 查找当前目录下的所有文件夹,不递归查询 [ma@ma software]$ find . -maxdepth 1 -type d ./gcc-7.2.0 ./go1.4 ./setuptools-36.5.0 ./lua-5.3.4 ./openssl-1.0.2l ./build ./zlib-1.2.11 ./vim ./gdb-8.1 |
1.2 设置查找深度
通过-maxdepth
选项控制查找的深度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 默认会递归查询,子菜单中符合条件的也会被列出来 ma@ubuntu:/data/software$ find . -name "*.zip" ./Python-2.7.14/Lib/test/zipdir.zip ./setuptools-36.6.0.zip ./php-5.6.33/ext/phar/tests/zip/files/badalias4.phar.zip ./php-5.6.33/ext/phar/tests/zip/files/frontcontroller3.phar.zip ./php-5.6.33/ext/phar/tests/zip/files/encrypted.zip ./php-5.6.33/ext/phar/tests/zip/files/frontcontroller2.phar.zip ./php-5.6.33/ext/phar/tests/zip/files/compress_unsup2.zip ./php-5.6.33/ext/phar/tests/zip/files/zlib_alias.phar.zip ./php-5.6.33/ext/zip/examples/test.zip # 加上 maxdepth 之后控制查询的深度 ma@ubuntu:/data/software$ find . -maxdepth 1 -name "*.zip" ./setuptools-36.6.0.zip |
1.3 时间属性控制
-atime +n/-n
:访问或执行的时间大于或小于 n 天的文件。-ctime +n/-n
:写入,更改 inode 属性 (如更改所有者、权限或者链接) 的时间大于或小于 n 天的文件。-mtime +n/-n
:表示写入时间大于或小于 n 天的文件,该参数用的最多。-mmin +n/-n
:表示 mtime 在 n 分钟内的文件。
二、正则表达式查询
使用正则表达式查询需要的选项有两个:-regextype
和-regex
。
-regextype
指定正则匹配的类型,可用的选项有很多,常见的有:『『awk』, 『egrep』, 『ed』, 『emacs』, 『grep』, 『sed』
等,不同匹配模式匹配的结果不一样。默认是 emacs
,但是默认和常用的正则不太一致,达不到想要的效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 默认匹配模式打不到匹配的效果 ma@ubuntu:/data/software$ find . -maxdepth 1 -regex "(.*?gz|.*?zip)" # 使用 awk 的正则匹配 ma@ubuntu:/data/software$ find . -maxdepth 1 -regextype "awk" -regex "(.*?gz|.*?zip)" ./pip-9.0.1.tar.gz ./php-5.6.33.tar.gz ./lua-5.3.4.tar.gz ./cmake-3.11.1.tar.gz ./Python-2.7.14.tgz ./gcc-7.2.0.tar.gz ./glibc-2.15.tar.gz ./libiconv-1.15.tar.gz ./autoconf-2.68.tar.gz ./nginx.tar.gz ./node-v9.4.0-linux-x64.tar.gz ./LuaJIT-2.0.5.tar.gz ./setuptools-36.6.0.zip ./mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz ./go1.10.1.linux-amd64.tar.gz ./gcc-centos6.tar.gz ./cmake-3.11.0-rc3.tar.gz |
三、用法示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 查找当前目录下,不区分大小写是 example 的文件 find . -iname example # 查找当前目录下名为 tmp 的目录: find . -type d -name tmp # 查找当前目录下文件权限是 777 的所有文件 find . -type f -perm 0777 # 查找当前目录下文件权限不是 777 的文件 find . -type f ! -perm 777 # 查找/etc/目录下的所有只读文件 find /etc/ -type f ! -perm /a+w # 查找主目录下的所有可执行文件 find ~ -type f -perm /a+x # 查找/tmp/目录下所有的.log 文件并将其删除 find /tmp/ -type f -name "*.log" -exec rm -f {} \; # 查找所有的空文件和目录 find /tmp/ -empty # 查找所有者为 guest 组为 root 的文件 find . -user guest -group root -type f # 查找所有 30 天到 60 天有过修改的文件 find . -type f -mtime +30 -mtime -60 # 查找所有大小位于 50-100M 之间的文件 find . -type f -size +50MB -size -100Mb # 查找所有 30 天未修改过的超过 50M 的文件 find . -type f -mtime +30 -size +50Mb -exec rm -rf {} \; |
评论