一、命令简介

  Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。find 命令有非常大的灵活性,可以向其指定丰富的搜索条件(如文件权限、属主、属组、文件类型、日期和大小等)来定位系统中的文件和目录。此外,find 还支持对搜索到的结果进行多种类型的命令操作。

二、使用示例

1、查找指定文件

使用-name参数查找指定文件
[root@test1 apache-tomcat-9.0.44]# find . -name startup.sh
./bin/startup.sh

2、查找指定后缀文件

使用正则表达式通配符查找匹配的文件
[root@test1 apache-tomcat-9.0.44]# find . -name “*.sh”
./bin/catalina.sh
./bin/ciphers.sh
./bin/configtest.sh
./bin/daemon.sh
./bin/digest.sh
./bin/makebase.sh
./bin/setclasspath.sh
./bin/shutdown.sh
./bin/startup.sh
./bin/tool-wrapper.sh
./bin/version.sh

3、查找指定类型的文件

使用-type支持指定类型的文件
[root@test1 apache-tomcat-9.0.44]# find ./webapps/ -type d
./webapps/
./webapps/qiuhun
./webapps/qiuhun/renxi

4、查找最近修改过的文件

查找最近2天内有更新的文件
[root@test1 apache-tomcat-9.0.44]# find ./logs -mtime -2
./logs
./logs/catalina.out
./logs/catalina.2021-03-31.log
./logs/localhost.2021-03-31.log
./logs/manager.2021-03-31.log
./logs/host-manager.2021-03-31.log
./logs/localhost_access_log.2021-03-31.txt

5、查找指定目录深度下的文件

查找/var/log目录下最小深度为2的log文件
[root@test1 apache-tomcat-9.0.44]# find /var/log/ -mindepth 2 -name *.log
/var/log/audit/audit.log
/var/log/gdm/:0-greeter.log
/var/log/gdm/:0.log
/var/log/tuned/tuned.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
/var/log/anaconda/ifcfg.log
/var/log/anaconda/ks-script-oDrhPT.log
/var/log/anaconda/ks-script-ohL_89.log
/var/log/anaconda/journal.log
/var/log/vmware/rc.local.log

6、根据inode删除文件

首先通过ls -li查找inode
[root@test1 apache-tomcat-9.0.44]# ll -i
然后通过find -inum inode号 -delete指定文件,此方法对于删除上传到服务器后文件名乱码的文件非常有用
[root@test1 apache-tomcat-9.0.44]# find -inum 2663645 -delete
在这里插入图片描述

7、清除查找到的超过指定时间的日志文件

清除查找到的文件,删除前确认
[root@test1 apache-tomcat-9.0.44]# find ./logs/ -mtime +5 -ok rm {} ;
< rm … ./logs/catalina.2021-03-26.log > ? y
< rm … ./logs/localhost.2021-03-26.log > ? y
< rm … ./logs/manager.2021-03-26.log > ? y
< rm … ./logs/host-manager.2021-03-26.log > ? y
< rm … ./logs/localhost_access_log.2021-03-26.txt > ? y

8、查找当前目录下具有指定权限的文件并获取完整路径

使用perm参数查找指定权限的文件
[root@test1 apache-tomcat-9.0.44]# find /home/wuhs/apache-tomcat-9.0.44/bin -type f -perm 750 -exec ls -l {} \;
-rwxr-x— 1 wuhs wuhs 25294 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/catalina.sh
-rwxr-x— 1 wuhs wuhs 1997 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/ciphers.sh
-rwxr-x— 1 wuhs wuhs 1922 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/configtest.sh
-rwxr-x— 1 wuhs wuhs 9100 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/daemon.sh
-rwxr-x— 1 wuhs wuhs 1965 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/digest.sh
-rwxr-x— 1 wuhs wuhs 3382 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/makebase.sh
-rwxr-x— 1 wuhs wuhs 3708 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/setclasspath.sh
-rwxr-x— 1 wuhs wuhs 1902 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/shutdown.sh
-rwxr-x— 1 wuhs wuhs 1904 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/startup.sh
-rwxr-x— 1 wuhs wuhs 5540 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/tool-wrapper.sh
-rwxr-x— 1 wuhs wuhs 1908 Mar 5 05:52 /home/wuhs/apache-tomcat-9.0.44/bin/version.sh

9、查找指定用户的文件

查找webapps目录下属主为wuhs用户的文件
[root@test1 apache-tomcat-9.0.44]# find ./webapps/ -user wuhs -type f -print
./webapps/qiuhun/说明.txt
./webapps/qiuhun/qiuhun.html
./webapps/qiuhun/renxi.mp3
./webapps/qiuhun/qiuhun.html.bak
./webapps/qiuhun/WebForm1.aspx.cs
./webapps/qiuhun/WebForm1.aspx
./webapps/qiuhun/WebForm1.aspx.designer.cs
./webapps/qiuhun/renxi/jscex-async.min.js
./webapps/qiuhun/renxi/jscex-jit.js
./webapps/qiuhun/renxi/jscex-builderbase.min.js
./webapps/qiuhun/renxi/jscex.min.js
./webapps/qiuhun/renxi/default.css
./webapps/qiuhun/renxi/love.js
./webapps/qiuhun/renxi/functions.js
./webapps/qiuhun/renxi/jscex-parser.js
./webapps/qiuhun/renxi/jscex-async-powerpack.min.js
./webapps/qiuhun/renxi/jquery.min.js
./webapps/qiuhun/index.html

10、查找指定大小的文件

查找超过1M的文件
[root@test1 apache-tomcat-9.0.44]# find . -size +1M -type f
./lib/catalina.jar
./lib/ecj-4.18.jar
./webapps/qiuhun/renxi.mp3

三、使用语法及参数说明

1、使用语法

usage:find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]
用法:find [路径] [表达式选项] [行动]

2、常用表达式选项参数说明

选项参数说明
-mount, -xdev只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin n在过去 n 分钟内被读取过
-anewer file比文件 file 更晚被读取过的文件
-atime n在过去n天内被读取过的文件
-cmin n在过去 n 分钟内被修改过
-cnewer file比文件 file 更新的文件
-ctime n在过去n天内被修改过的文件
-empty空的文件-gid n or -group name
-ipath p, -path p路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name文件名称符合 name 的文件。iname 会忽略大小写
-size n文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。
-type b/d/c/p/l/f查是块设备、目录、字符设备、管道、符号链接、普通文件
-perm按执行权限来查找
-user username按文件属主来查找
-group groupname按组来查找
-depth指定查找目录深度
-follow如果遇到符号链接文件,就跟踪链接所指的文件
-prune忽略某个目录
-maxdepth指定查找目录最大深度
-mindepth指定查找目录最小深度
-version查看版本
-help获取帮助

3、常用行动参数说明

参数参数说明
-delete删除查找到的文件
-exec command对查找到的文件执行command命令
-ok command执行命令前需要进行确认
-printf格式化输出

4、多条件组合参数

参数参数说明
-o是或者的意思
-a是而且的意思
-not是相反的意思
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐