概要

find [-H] [-L] [-P] [path...] [expression]
Linux find命令

名称

查找 - 在目录层次结构中搜索文件

常用选项

-iname pattern
       Like -name, but the match is case insensitive.
-name pattern
      Base  of  file  name  (the  path  with  the  leading 
      directories removed) matches shell pattern pattern.
-mmin n
      File's data was last modified n minutes ago.
-size n[cwbkMG]
      File uses n units of space.  The following suffixes
      can be used:
-exec command ;
      Execute command; true if 0 status is  returned. 
      All  following arguments to find are taken to be
      arguments to the command until an argument
      consisting of `;' is encountered.
-type c
      File is of type c:
      b      block (buffered) special
      c      character (unbuffered) special
      d      directory

      p      named pipe (FIFO)
      f      regular file
      l      symbolic link; this is never true if the -L
             option or the -follow  option is in effect, 
             unless the symbolic link is broken. If you
             want to search for symbolic links when -L 
             is in effect, use -xtype.
      s      socket
      D      door (Solaris)

示例

find命令的执行可能会变得非常复杂。我们可以写一本关于find命令可以使用的选项和参数数量的书。首先,我们可以尝试搜索文件。我忘了resolv.conf文件隐藏在哪里了。我们可以尝试在/目录中找到它。在本例中,我将把标准错误重定向到/dev/null,请随意运行find命令而不使用它。语法如下所示,find(实际命令)where(/)和what(-name resolv.conf):

$ find / -name resolv.conf

假设/tmp目录中有两个文件:file和File。Unix系统区分大小写,因此file和File是两个不同的文件。touch命令可以帮助我们创建这些文件:

$ touch /tmp/file /tmp/File

默认情况下,查找命令仅搜索完全匹配:

$ find /tmp -name file

要查找命令不区分大小写,我们可以使用-iname选项。

$ find /tmp -iname file

要查找在过去20分钟内创建的文件我们可以使用-mmin选项:

$ find /tmp/ -mmin -20

在根据访问时间搜索文件时,下表可能会有所帮助:

Options描述
-atime -3最后一个上次访问的文件不到3天前
-atime +5所有上次访问超过5天前访问的文件
-atime 4最后4天前访问的所有文件

让我们查找具有特定大小的文件。在本例中,我们在/var/log中搜索大小为8K的文件:

$ find /var/log/ -size 8k

我们还可以指示find命令对找到的每个文件执行某些命令。允许将/tmp目录中的文件和文件的权限更改为chmod 777。

$ find /tmp -iname file -exec chmod 777 {} \;

“find”还可以搜索某些类型的文件。例如,如果要查找符号链接,可以使用-type选项运行find命令:

$ find / -maxdepth 1 -type l
日期:2020-07-07 20:54:47 来源:oir作者:oir