find命令是一种通过条件匹配在指定目录下查找对应文件或者目录的工具。匹配的条件可以是文件名称、类型、大小、权限属性、时间戳等。find命令还可以配合相关命令对匹配到的文件作出后续处理。
find在工作时会遍历指定的目录,通过匹配指定的条件搜寻我们需要的文件或者目录。因此,find命令具有以下特点: * 查找速度略慢 * 精却查找 * 实时查找 * 可能只搜索用户具备读取和执行权限的目录(这点同locate)
find命令语法格式为: find [path...] [expression] [path...]为需要查找文件所指定的路径。如果不指定,则默认为当前目录及其子目录。 [expression]为匹配条件/表达式。如果未设置匹配条件,则默认查找指定目录及子目录下所有文件
指定搜索目录的层级需要用到选项-maxdepth和-mindepth,分别指定搜索目录的最大深度和最小深度。两个选项可以配合使用,指定搜索目录的深度范围。如果-maxdepth/-mindepth二者的参数相同,则find命令会搜索某个特定深度的文件
-maxdepth level 指定最大搜索目录深度level,指定的目录为第1级 -mindepth level 指定最小搜索目录深度level。配合-maxdepth可搜索指定深度的文件。用法示例 * find / -maxdepth 2 -name "*.conf" 该命令中”/”为第一级目录,查找文件的最大目录深度为2。 * find / -maxdepth 3 -mindepth 3 -name "*.conf" 查找文件的最大目录深度为3。
代码实现:
[root@Centos7T test1]#ls -AR ./ # 查看当前目录及子目录下所有文件 # ./: f1 f2 test2 ./test2: f3 f4 test3 ./test2/test3: f5 f6 [root@Centos7T test1]#find -maxdepth 2 # 查找最大目录深度为2的所有文件 # . ./test2 ./test2/test3 ./test2/f3 ./test2/f4 ./f1 ./f2 [root@Centos7T test1]#find -mindepth 2 -name "f*" # 查找最大目录深度为2的所有文件 # ./test2/test3/f5 ./test2/test3/f6 ./test2/f3 ./test2/f4 [root@Centos7T test1]#find -maxdepth 2 -mindepth 2 ./test2/test3 ./test2/f3 ./test2/f4在上面的例子中,我们使用了-name选项查找f开头的文件,现在我们开始讲解如何根据文件名查找指定文件。另外,由于每个文件都有一个inode编号与其对应,除了硬链接文件外,同一分区下,每个文件的inode编号都是唯一的,也因此我么可以利用inode编号及文件的链接数来查找符合条件的文件。相关选项用法如下:
-name "FILE_NAME" 文件名查找,支持使用glob:*、?、[]、[^] -iname "FILE_NAME" 文件名查找,不区分大小写 -inum n 按inode号查找 -samefile name 查找与指定文件有相同inode号的文件,一般用于查找硬连接文件 -links n 查找连接数为N的文件 -regex "PATTERN" 以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称代码实现:
[root@Centos7T test1]#find ./ -name "f*" # 查找制定目录下所有文件名以f开头的文件 # ./test2/test3/f5 ./test2/test3/f6 ./test2/test3/f1-link ./test2/f3 ./test2/f4 ./f1 ./f2 [root@Centos7T test1]#find ./ -samefile "f1" # 查找与当前目录下文件f1有相同inode编号的文件 # ./test2/test3/f1-link ./f1 [root@Centos7T test1]#ll -i ./test2/test3/f1-link ./f1 # 验证查找结果的inode编号 # 74 -rw-r--r--. 2 root root 0 Aug 6 13:45 ./f1 74 -rw-r--r--. 2 root root 0 Aug 6 13:45 ./test2/test3/f1-link [root@Centos7T test1]#find ./ -links 2 ./test2/test3 ./test2/test3/f1-link ./f1 [root@Centos7T test1]#ll -ad ./test2/test3 ./test2/test3/f1-link ./f1 -rw-r--r--. 2 root root 0 Aug 6 13:45 ./f1 drwxr-xr-x. 2 root root 41 Aug 6 14:36 ./test2/test3 -rw-r--r--. 2 root root 0 Aug 6 13:45 ./test2/test3/f1-link [root@Centos7T test1]#find ./ -regex ".*f[0-9]$" ./test2/test3/f5 ./test2/test3/f6 ./test2/f3 ./test2/f4 ./f1 ./f2根据文件的属主以及属组信息,我们可以查找符合指定属主、属组的文件。相关选项用法如下:
-user username 查找属主为指定用户(UID)的文件 -group groupname 查找属组为指定组(GID)的文件 -uid UseerID 查找属主为指定的UID号的文件 -gid GroupID 查找属组为指定的GID号的文件 -nouser 查找没有属主的文件 -nogroup 查找没有属组的文件代码实现:
[root@Centos7T test1]#ll total 0 -rw-r--r--. 1 root root 0 Aug 6 13:45 f1 -rw-r--r--. 1 geoffrey root 0 Aug 6 13:45 f2 -rw-r--r--. 1 root geoffrey 0 Aug 6 15:04 f3 -rw-r--r--. 1 geoffrey geoffrey 0 Aug 6 15:04 f4 [root@Centos7T test1]#find ./ -user geoffrey ./f2 ./f4 [root@Centos7T test1]#find ./ -group geoffrey ./f3 ./f4 [root@Centos7T test1]#find ./ -gid 0 ./ ./f1 ./f2linux系统中的文件大致分为以下种:普通文件、目录文件、二进制程序文件、符号链接文件、套接字文件、块设备文件、字符设备文件。我们可以指定搜索文件的文件类型以达到查找文件的目的。命令用法为find [path] -type TYPE。所搜文件的具体类型有TYPE的值确定。TYPE的值及具体含义如下:
f 普通文件 d 目录文件 l 符号链接文件 s 套接字文件 b 块设备文件 c 字符设备文件 p 管道文件代码实现:
[root@Centos7T ~]#find /dev -type b /dev/sda5 /dev/sda4 /dev/sda3 ······ root@Centos7T ~]#find / -maxdepth 1 -type d / /app /boot /dev ······ [root@Centos7T ~]#find /etc -maxdepth 1 -type l /etc/favicon.png /etc/redhat-release /etc/system-release /etc/mtab ······命令用法为find [path] -size [+|-]N 其中N为文件大小,单位为c/k/M/G. [+|-]N的具体含义如下:
50k: 搜索49k~50k大小的文件 N-1~N +50k: 搜索大于50k的文件 N~······ -50k: 搜索小于49k的文件 0~N-1代码实现:
[root@Centos7T ~]#find /app -size 2M /app/man_pages_zh-CN_v1.6.2.1.tar.gz [root@Centos7T ~]#find /app -size +10k /app/man_pages_zh-CN_v1.6.2.1.tar.gz /app/source/httpd-2.2.34.tar.gz /app/source/httpd-2.4.27.tar.bz2 /app/source/nginx-1.12.1.tar.gz /app/source/linux-4.12.4.tar.xz [root@Centos7T ~]#find /app -size -1k /app/wang/1024 /app/wang/18_f2 /app/wang/2048 /app/wang/4096 /app/wang/9172 /app/wang/f1 /app/wang/test110 /app/wang/test文件的时间戳分为访问时间(access time)/修改时间(modify time)/创建时间(create time),分别简写为atime/mtime/ctime。我们可以根据这三个时间戳查找对应文件。命令用法如下:
-atime [+|-] N N:[N,N+1) +N :[N+1,······] -N :[0,N) N:[a,b]以当前时刻点往前推(N+1)*24小时为最远时刻点a,以当前时刻点往前推N*24小时为最近时刻点b。时间段长度h=24h -N:[a,b]以当前时刻往前推N*24小时为最远时刻点a,最近时刻点b不是当前时间,而是未!来!任意时间(-1能搜出未来很多年的文件),时间段长度N*24h<H +N:[-oo,b]最远时刻点为负无穷,以当前时刻点往前推(N+1)*24小时为最近时刻点b -mtime [+|-] N -ctime [+|-] N代码实现:
[root@Centos7T app]#find /app -atime +1 /app/wang/1024 /app/wang/18_f2 /app/wang/2048 /app/wang/4096 /app/wang/9172 /app/wang/f1 [root@Centos7T app]#find /app -atime -1 /app/test1 /app/test1/f1 /app/test1/f2 /app/test1/f3 /app/test1/f4 [root@Centos7T app]#find /app -atime 1 /app/systeminfo.sh /app/arg.sh /app/wang/08-scp.sh /app/wang/1024.sh /app/wang/11backup.sh /app/wang/11scp.sh /app/wang/15scp.sh /app/wang/17scp.sh我们可以根据文件三类访问对象的三种三种权限查找对应文件,命令用法如下:
-perm [/|-] MODE MODE:精确匹配三类对象(u,g,o)的权限 /MODE:三类对象只要有一类对象中的三个权限位匹配一位即可,或关系(/444,三个只要有一 个“有”读权限(注意不是“是”读权限,)) -MODE:三类对象分别有对应权限,是否还有其他权限不在意,与关系(-444,三个都要“有”读权限,可以匹配到4,6,7;是否还有写或者执行权限不关心) Notes:/或者-后面需要匹配的权限出现0(例如404),,则表明不关心该对象权限(例如440,所属组有无权限都不关心)。如果单独的权限表示精确匹配,则表明该对象无任何权限代码实现:
[root@Centos7T test1]#ll total 0 --wx--x--x. 1 root root 0 Aug 6 13:45 f1 -rw-r--r-x. 1 root root 0 Aug 6 13:45 f2 -r-xr--r--. 1 root root 0 Aug 6 15:04 f3 --w--wx-w-. 1 root root 0 Aug 6 15:04 f4 [root@Centos7T test1]#find ./ -name "f*" -perm 311 ./f1 [root@Centos7T test1]#find ./ -name "f*" -perm 232 ./f4 [root@Centos7T test1]#find ./ -name "f*" -perm /122 ./f1 ./f3 ./f4 [root@Centos7T test1]#find ./ -name "f*" -perm /442 ./f2 ./f3 ./f4 [root@Centos7T test1]#find ./ -name "f*" -perm -644 ./f2 [root@Centos7T test1]#find ./ -name "f*" -perm -244 ./f2 [root@Centos7T test1]#find ./ -name "f*" -perm -444 ./f2 ./f3有时候我们查找文件可能还会对这些文件进行后续的操作,例如删除,修改权限等。可以使用以下命令:
-print:默认的处理动作,显示至屏幕 -ls :类似于对查找到的文件执行“ls -l”命令 -delete:删除查找到的文件 *慎用!* -fls file :查找到的所有文件的长格式信息保存至指定文件中,也可用重定向的方式 -ok COMMAND {} \; :对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认 -exec COMMAND {} \; :对查找到的每个文件执行由COMMAND指定的命令,没有-ok中的交互式确认。 其中{}用于引用查找到的文件名称自身,\;是配合-ok和-exec选项的。代码实现:
[root@Centos7T app]#find ./test1/ -type f -perm -001 ./test1/f1 ./test1/f2 [root@Centos7T app]#find ./test1/ -type f -perm -001 -exec chmod o-x {} \; [root@Centos7T app]#ll ./test1/* --wx--x---. 1 root root 0 Aug 6 13:45 ./test1/f1 -rw-r--r--. 1 root root 0 Aug 6 13:45 ./test1/f2 -r-xr--r--. 1 root root 0 Aug 6 15:04 ./test1/f3 --w--wx-w-. 1 root root 0 Aug 6 15:04 ./test1/f4关于find命令的用法就介绍到这。