【Linux】命令 - find

Posted by 西维蜀黍 on 2020-10-08, Last Modified on 2022-12-10

find 命令

Linux find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

$ find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
$ find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。

参数

-type t - 按文件类型 filter

  • c - 文件类型是 c 的文件(character special)
  • d: 目录
  • c: 字型装置文件
  • b: 区块装置文件(block special)
  • p: FIFO
  • f: 一般文件(regular file)
  • l: 符号连结
  • s: socket

-name pattern - 按名字匹配

$ find root_path -name '*.ext'

Special shell pattern matching characters ([, ],*, and ?) may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``'’).

Exclude:

# Find files matching a given pattern, excluding specific paths:
$ find root_path -name '*.py' -not -path '*/site-packages/*'

-or - Or

# Find files matching multiple path/name patterns:
$ find root_path -path '**/path/**/*.ext -or -name '*pattern*'

-atime n[smhdw] - 在过去一段时间内(外)被访问过的文件

If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods. -> 如果 [smhdw] 都没有提供,则表示 n 天。

If units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started is exactly n units. Possible time units are as follows:

  • s second
  • m minute (60 seconds)
  • h hour (60 minutes)
  • d day (24 hours)
  • w week (7 days)

Any number of units may be combined in one -atime argument, for example, -atime -1h30m. Units are probably only useful when used in conjunction with the + or - modifier.

  • +: -atime +1h30m 表示一个半小时之前被访问过的文件
  • -: -atime -1h30m 表示一个半小时之内被访问过的文件

-ctime n[smhdw] - 在过去一段时间内(外)文件状态发生变化(被创建或被修改)

If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods. -> 如果 [smhdw] 都没有提供,则表示 n 天。

If no units are specified, this primary evaluates to true if the difference between the time of last change of file status information and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.

-mtime n[smhdw] - 在过去一段时间内(外)被修改过的文件

If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods. -> 如果 [smhdw] 都没有提供,则表示 n 天。

If units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started is exactly n units.

Please refer to the -atime primary description for information on supported time units.

-size n[ckMGTP] - 文件大小

True if the file’s size, rounded up, in 512-byte blocks is n.

If n is followed by a c, then the primary is true if the file’s size is n bytes (characters).

Similarly if n is followed by a scale indicator then the file’s size is compared to n scaled as:

  • k kilobytes (1024 bytes)
  • M megabytes (1024 kilobytes)
  • G gigabytes (1024 megabytes)
  • T terabytes (1024 gigabytes)
  • P petabytes (1024 terabytes)
# Find files matching a given size range:
$ find root_path -size +500k -size -10M

-perm [-|+]mode - 根据文件被分配权限

The mode may be either symbolic (see chmod(1)) or an octal number.

If the mode is symbolic, a starting value of zero is assumed and the mode sets or clears permissions without regard to the process’ file mode creation mask.

If the mode is octal, only bits 07777 (S_ISUID | S_ISGID | S_ISTXT | S_IRWXU | S_IRWXG |S_IRWXO) of the file’s mode bits participate in the comparison.

If the mode is preceded by a dash (-), this primary evaluates to true if at least all of the bits in the mode are set in the file’s mode bits.

If the mode is preceded by a plus (+), this primary evaluates to true if any of the bits in the mode are set in the file’s mode bits. Otherwise, this primary evaluates to true if the bits in the mode exactly match the file’s mode bits.

Note, the first character of a symbolic mode may not be a dash (-).

-print - 将匹配的文件输出到标准输出

This primary always evaluates to true. It prints the pathname of the current file to standard output.

If none of -exec, -ls, -print, -print0, or -ok is specified, the given expression shall be effectively replaced by ( given expression ) -print.

-exec utility [argument ...] ; - 对匹配的文件执行 shell 命令

True if the program named utility returns a zero value as its exit status.

Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (;). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator.

If the string {} appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs.

比如:

# 删除文件大小为零的文件
$ find ./ -size 0 -exec rm {} \; 

# Run a command for each file (use {} within the command to access the filename):
$ find root_path -name '*.ext' -exec wc -l {} \;

-ok utility [argument ...] ; - 带用户提示确认地对匹配的文件执行 shell 命令

The -ok primary is identical to the -exec primary with the exception that find requests user affirmation for the execution of the utility by printing a message to the terminal and reading a response. If the response is not affirmative (in the POSIX locale), the command is not executed and the value of the -ok expression is false.

Usage

将目前目录及其子目录下所有扩展名为 c 的文件列出来。

$ find . -name "*.c"

将指定目录及其子目录下所有扩展名为 c 的文件列出来。

$ find ./content/post -name "*.c"

Print out a list of all the files whose names do not end in .c.

$ find / ! -name "*.c" -print

将目前目录及其子目录中的所有一般文件列出

$ find . -type f

将目前目录及其子目录下所有最近 20 天更新过的文件列出

$ find . -ctime -20

查找/var/log目录中更改时间在7日之前的普通文件,并在删除之前询问它们:

$ find /var/log -type f -mtime +7 -ok rm {} \;

查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件:

$ find . -type f -perm 644 -exec ls -l {} \;

为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径:

$ find / -type f -size 0 -exec ls -l {} \;

删除文件大小为零的文件:

$ find ./ -size 0 -exec rm {} \; 

在/logs目录中查找更改时间在5日以前的文件并删除它们:

$ find /logs -type f -mtime +5 -exec rm { } \;

在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示

$ find . -name "*.conf" -mtime +5 -ok rm { } \; 

Reference