find命令用来在指定目录下查找文件。任何位于参数expression之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
find针对的是文件信息进行查找, 而非文件内容. 文件信息包括ls -l列出来的种种信息, 因此也可以作为ls的一种filter使用, 十分强大.
语法:
find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
前面几个是查找功能选项, 然后是查找路径, 最后才是要查找的表达式.
功能选项
其实用处不太大..可能会用到一下-L和-E.
- -P: 查找文件返回的信息使用的是符号链接本身, 而非其链接对象. 默认选项.
- -L: 查找文件返回的信息使用的是链接对象而非符号链接本身, 除非链接对象不存在.
- -H: 类似于-L, 但针对符号链接specified on the command line的, 不明…
- -E: 使用扩展正则表达式而非一般正则. 适用于 -regex 和 -iregex 选项时.
- -d: 深度优先从深到浅 (即优先子文件夹而非文件夹本身). 默认从浅到深(不是广度优先搜索)
- -s: 使得搜索遵循字母顺序. 貌似本来就是字母顺序嘛..
- -f: 指明查找路径…可以不用这选项吧..
- -x: 阻止进入设备号和开始时不同的文件夹.可能适用于符号链接文件夹.
- -X: 使得搜索文件名能更好被xargs利用, 其中含有
'"\
的文件会报错并忽略.
表达式选项
表达式选项是用来判断递归文件夹文件时, 将文件和表达式进行比较, 符合的返回True, 否则返回False. 返回True的会被进一步被操作. 支持一些判断式操作符, 例如\( \), !, -not, -true, -false, -and (-a), -or (-o)
. 默认多条表达式是and的关系. !
使用要前后空格形成独立参数, 并且加反义符\!
.
动作型选项
-print
:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串;默认行为.-fprint
<列表文件>:此参数的效果和指定“-print”参数类似,但会把结果保存成指定的列表文件;列表文件>-ls
:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出(相当于ls -l效果);-delete
: 删除文件/文件夹-exec
<执行指令> {} ;:假设find指令的回传值为True,就执行该指令, `{}`处将会传入文件名.注意不能多条指令, 命令结尾`\;`;执行指令>-exec
<执行指令> {} +:和上面的不同是会对多个文件名进行执行一次命令, 这里结尾是`+`或`\+`.执行指令>-execdir
<执行指令> {}: 执行指令时将在文件所在的文件夹进行执行.执行指令>- -ok<执行指令>:此参数的效果和指定“-exec”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”才执行命令,否则放弃执行命令;执行指令>
- -okdir<执行指令>:此参数的效果和指定“-execdir”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”才执行命令,否则放弃执行命令;执行指令>
- -print0:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为全部的名称皆在同一行;
- -printf<输出格式>:假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式可以自行指定;输出格式>
- -fprint0<列表文件>:此参数的效果和指定“-print0”参数类似,但会把结果保存成指定的列表文件;列表文件>
- -fprintf<列表文件><输出格式>:此参数的效果和指定“-printf”参数类似,但会把结果保存成指定的列表文件;输出格式>列表文件>
- -fls<列表文件>:此参数的效果和指定“-ls”参数类似,但会把结果保存为指定的列表文件;列表文件>
filter型选项
用于过滤文件, 只有返回True才进一步执行. 当filter型选项后面跟的是数值N时(例如-newer), +N
表示more than N, -N
表示less than N, 直接N
是精确匹配.
文件名/范本
-name
<范本样式>:指定字符串作为寻找文件或目录的范本样式;范本样式>- -iname<范本样式>:此参数的效果和指定“-name”参数类似,但忽略字符大小写的差别;范本样式>
- -lname<范本样式>:指定字符串作为寻找符号连接内容的范本样式;范本样式>
- -ilname<范本样式>:此参数的效果和指定“-lname”参数类似,但忽略字符大小写的差别;范本样式>
-prune
:不寻找字符串作为寻找文件或目录的范本样式;-regex
<范本样式>:指定字符串作为寻找文件或目录的范本样式;范本样式>- -iregex<范本样式>:此参数的效果和指定“-regexe”参数类似,但忽略字符大小写的差别;范本样式>
搜索范围
-path
<范本样式>:指定字符串作为寻找目录的范本样式;范本样式>- -ipath<范本样式>:此参数的效果和指定“-path”参数类似,但忽略字符大小写的差别;范本样式>
-maxdepth
<目录层级>:设置最大目录层级;目录层级>-mindepth
<目录层级>:设置最小目录层级;目录层级>- -depth:从指定目录下最深层的子目录开始查找; (类似于find功能选项-d)
- -xdev:将范围局限在先行的文件系统中;(被find功能选项-x代替)
- -mount:此参数的效果和指定“-xdev”相同;
文件特点
-size
<文件大小>:查找符合指定的文件大小的文件;文件大小>- -empty:寻找文件大小为0 Byte的文件,或目录下没有任何子目录或文件的空目录;
-type
<文件类型>:只寻找符合指定的文件类型的文件;文件类型>- -xtype<文件类型>:此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查。文件类型>
- -fstype<文件系统类型>:只寻找该文件系统类型下的文件或目录;文件系统类型>
- -follow:排除符号连接;
- -inum:查找符合指定的inode编号的文件或目录;
- -links<连接数目>:查找符合指定的硬连接数目的文件或目录;连接数目>
- -noleaf:不去考虑目录至少需拥有两个硬连接存在;
权限
- -gid<群组识别码>:查找符合指定之群组识别码的文件或目录;群组识别码>
-group
<群组名称>:查找符合指定之群组名称的文件或目录;群组名称>- -nogroup:找出不属于本地主机群组识别码的文件或目录;
- -nouser:找出不属于本地主机用户识别码的文件或目录;
- -uid<用户识别码>:查找符合指定的用户识别码的文件或目录;用户识别码>
-user
<拥有者名称>:查找符和指定的拥有者名称的文件或目录;拥有者名称>-perm
<权限数值>:查找符合指定的权限数值的文件或目录;权限数值>
时间
a是访问access, c是创建create, m是修改modify.
- -amin<分钟>:查找在指定时间曾被存取过的文件或目录,单位以分钟计算;分钟>
- -anewer<参考文件或目录>:查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录;参考文件或目录>
- -atime<24小时数>:查找在指定时间曾被存取过的文件或目录,单位以24小时计算;
- -cmin<分钟>:查找在指定时间之时被更改过的文件或目录;分钟>
- -cnewer<参考文件或目录>查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;参考文件或目录>
- -ctime<24小时数>:查找在指定时间之时被更改的文件或目录,单位以24小时计算;
- -mtime<24小时数>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
- -mmin<分钟>:查找在指定时间曾被更改过的文件或目录,单位以分钟计算;分钟>
- -daystart:从本日开始计算时间;
-newer
<参考文件或目录>:查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录;参考文件或目录>- -used<日数>:查找文件或目录被更改之后在指定时间曾被存取过的文件或目录,单位以日计算;日数>
其余选项..
- -true:将find指令的回传值皆设为True;
- -false:将find指令的回传值皆设为False;
- -help或——help:在线帮助;
- -version或——version:显示版本信息;
示例:
### 根据文件或者正则表达式进行匹配
# 列出当前目录及子目录下所有文件和文件夹
find .
# 在/home目录下查找以.txt结尾的文件名, *.txt记得带引号:
find /home -name "*.txt"
# 同上,但忽略大小写
find /home -iname "*.txt"
# 当前目录及子目录下查找所有以.txt和.pdf结尾的文件
find . \( -name "*.txt" -o -name "*.pdf" \) 或 find . -name "*.txt" -o -name "*.pdf"
# 匹配文件路径或者文件
find /usr/ -path "*local*"
# 基于正则表达式匹配文件路径
find . -regex ".*\(\.txt\|\.pdf\)$"
# 同上,但忽略大小写
find . -iregex ".*\(\.txt\|\.pdf\)$"
# 否定参数 找出/home下不是以.txt结尾的文件
find /home ! -name "*.txt"
# 根据文件类型进行搜索
# 类型参数列表: f 普通文件 l 符号连接 d 目录 c 字符设备 b 块设备 s 套接字 p Fifo
find . -type 类型参数
# 查找文件类型为文件型, 再执行echo文件名.
find / -type f -exec echo {} \;
# 基于目录深度搜索
# 向下最大深度限制为3 (指定文件夹同层为1, 子文件夹为2, 子子为3)
find . -maxdepth 3 -type f
# 搜索出深度距离当前目录至少2个子目录的所有文件
find . -mindepth 2 -type f
# 根据文件时间戳进行搜索
# UNIX/Linux文件系统每个文件都有三种时间戳:
# - 访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
# - 修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。
# - 变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。
find . -type f 时间戳
# 搜索最近七天内被访问过的所有文件
find . -type f -atime -7
# 搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
# 搜索超过七天内被访问过的所有文件
find . -type f -atime +7
# 搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
# 找出比file.log修改时间更长的所有文件
find . -type f -newer file.log
# 根据文件大小进行匹配
# 文件大小单元:
# b —— 块(512字节)
# c —— 字节
# w —— 字(2字节)
# k —— 千字节
# M —— 兆字节
# G —— 吉字节
find . -type f -size 文件大小单元
# 搜索大于10KB的文件
find . -type f -size +10k
# 搜索小于10KB的文件
find . -type f -size -10k
# 搜索等于10KB的文件
find . -type f -size 10k
# 要列出所有长度为零的文件
find . -empty
# 根据文件权限/所有权进行匹配
# 当前目录下搜索出权限为777的文件
find . -type f -perm 777
# 找出当前目录下权限不是644的php文件
find . -type f -name "*.php" ! -perm 644
# 找出当前目录用户tom拥有的所有文件
find . -type f -user tom
# 找出当前目录用户组sunk拥有的所有文件
find . -type f -group sunk
# 查找根目录下, 既不新于ttt用户且用户不是wnj的. 注意括号要转义, 括号内实际是-and逻辑
find / \! \( -newer ttt -user wnj \)
# 删除匹配文件
# 删除当前目录下所有.txt文件
find . -type f -name "*.txt" -delete
# 借助-exec选项与其他命令结合使用
# 找出当前目录下所有root的文件,并把所有权更改为用户tom
# {} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。
find .-type f -user root -exec chown tom {} \;
# 找出自己家目录下所有的.txt文件并删除
# -ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
find $HOME/. -name "*.txt" -ok rm {} \;
# 查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> all.txt
# 将30天前的.log文件移动到old目录中
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
# 找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
# 因为单行命令中-exec参数中无法使用多个命令,可以将多条命令写在脚本中再执行脚本, 如 -exec ./text.sh {} \;
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
# 搜索但跳出指定的目录
# 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
find . -path "./sk" -prune -o -name "*.txt" -print
更多用法实例可以参考妈咪,我找到了! – 15个实用的Linux find命令示例和爹地,我找到了!, 15个极好的Linux find命令示例, Linux中find常见用法示例
Mac上的find帮助:
FIND(1) BSD General Commands Manual FIND(1)
NAME
find -- walk a file hierarchy
SYNOPSIS
find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
DESCRIPTION
The find utility recursively descends the directory tree for each path
listed, evaluating an expression (composed of the ``primaries'' and
``operands'' listed below) in terms of each file in the tree.
The options are as follows:
-E Interpret regular expressions followed by -regex and -iregex pri-
maries as extended (modern) regular expressions rather than basic
regular expressions (BRE's). The re_format(7) manual page fully
describes both formats.
-H Cause the file information and file type (see stat(2)) returned
for each symbolic link specified on the command line to be those
of the file referenced by the link, not the link itself. If the
referenced file does not exist, the file information and type
will be for the link itself. File information of all symbolic
links not on the command line is that of the link itself.
-L Cause the file information and file type (see stat(2)) returned
for each symbolic link to be those of the file referenced by the
link, not the link itself. If the referenced file does not
exist, the file information and type will be for the link itself.
This option is equivalent to the deprecated -follow primary.
-P Cause the file information and file type (see stat(2)) returned
for each symbolic link to be those of the link itself. This is
the default.
-X Permit find to be safely used in conjunction with xargs(1). If a
file name contains any of the delimiting characters used by
xargs(1), a diagnostic message is displayed on standard error,
and the file is skipped. The delimiting characters include sin-
gle (`` ' '') and double (`` " '') quotes, backslash (``\''),
space, tab and newline characters.
However, you may wish to consider the -print0 primary in conjunc-
tion with ``xargs -0'' as an effective alternative.
-d Cause find to perform a depth-first traversal, i.e., directories
are visited in post-order and all entries in a directory will be
acted on before the directory itself. By default, find visits
directories in pre-order, i.e., before their contents. Note, the
default is not a breadth-first traversal.
This option is equivalent to the -depth primary of IEEE Std
1003.1-2001 (``POSIX.1''). The -d option can be useful when find
is used with cpio(1) to process files that are contained in
directories with unusual permissions. It ensures that you have
write permission while you are placing files in a directory, then
sets the directory's permissions as the last thing.
-f Specify a file hierarchy for find to traverse. File hierarchies
may also be specified as the operands immediately following the
options.
-s Cause find to traverse the file hierarchies in lexicographical
order, i.e., alphabetical order within each directory. Note:
`find -s' and `find | sort' may give different results.
-x Prevent find from descending into directories that have a device
number different than that of the file from which the descent
began.
This option is equivalent to the deprecated -xdev primary.
PRIMARIES
All primaries which take a numeric argument allow the number to be pre-
ceded by a plus sign (``+'') or a minus sign (``-''). A preceding plus
sign means ``more than n'', a preceding minus sign means ``less than n''
and neither means ``exactly n''.
-Bmin n
True if the difference between the time of a file's inode cre-
ation and the time find was started, rounded up to the next full
minute, is n minutes.
-Bnewer file
Same as -newerBm.
-Btime n[smhdw]
If no units are specified, this primary evaluates to true if the
difference between the time of a file's inode creation and the
time find was started, rounded up to the next full 24-hour
period, is n 24-hour periods.
If units are specified, this primary evaluates to true if the
difference between the time of a file's inode creation and the
time find was started is exactly n units. Please refer to the
-atime primary description for information on supported time
units.
-acl May be used in conjunction with other primaries to locate files
with extended ACLs. See acl(3) for more information.
-amin n
True if the difference between the file last access time and the
time find was started, rounded up to the next full minute, is n
minutes.
-anewer file
Same as -neweram.
-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.
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 fol-
lows:
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.
-cmin n
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 minute, is n minutes.
-cnewer file
Same as -newercm.
-ctime n[smhdw]
If no units are specified, this primary evaluates to true if the
difference between the time of last change of file status infor-
mation and the time find was started, rounded up to the next full
24-hour period, is n 24-hour periods.
If units are specified, this primary evaluates to true if the
difference between the time of last change of file status infor-
mation and the time find was started is exactly n units. Please
refer to the -atime primary description for information on sup-
ported time units.
-d Same as depth. GNU find implements this as a primary in mistaken
emulation of FreeBSD find(1).
-delete
Delete found files and/or directories. Always returns true.
This executes from the current working directory as find recurses
down the tree. It will not attempt to delete a filename with a
``/'' character in its pathname relative to ``.'' for security
reasons. Depth-first traversal processing is implied by this
option. Following symlinks is incompatible with this option.
-depth Always true; same as the -d option.
-depth n
True if the depth of the file relative to the starting point of
the traversal is n.
-empty True if the current file or directory is empty.
-exec utility [argument ...] ;
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 argu-
ments 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.
-exec utility [argument ...] {} +
Same as -exec, except that ``{}'' is replaced with as many path-
names as possible for each invocation of utility. This behaviour
is similar to that of xargs(1).
-execdir utility [argument ...] ;
The -execdir primary is identical to the -exec primary with the
exception that utility will be executed from the directory that
holds the current file. The filename substituted for the string
``{}'' is not qualified.
-execdir utility [argument ...] {} +
Same as -execdir, except that ``{}'' is replaced with as many
pathnames as possible for each invocation of utility. This be-
haviour is similar to that of xargs(1).
-flags [-|+]flags,notflags
The flags are specified using symbolic names (see chflags(1)).
Those with the "no" prefix (except "nodump") are said to be
notflags. Flags in flags are checked to be set, and flags in
notflags are checked to be not set. Note that this is different
from -perm, which only allows the user to specify mode bits that
are set.
If flags are preceded by a dash (``-''), this primary evaluates
to true if at least all of the bits in flags and none of the bits
in notflags are set in the file's flags bits. If flags are pre-
ceded by a plus (``+''), this primary evaluates to true if any of
the bits in flags is set in the file's flags bits, or any of the
bits in notflags is not set in the file's flags bits. Otherwise,
this primary evaluates to true if the bits in flags exactly match
the file's flags bits, and none of the flags bits match those of
notflags.
-fstype type
True if the file is contained in a file system of type type. The
lsvfs(1) command can be used to find out the types of file sys-
tems that are available on the system. In addition, there are
two pseudo-types, ``local'' and ``rdonly''. The former matches
any file system physically mounted on the system where the find
is being executed and the latter matches any file system which is
mounted read-only.
-gid gname
The same thing as -group gname for compatibility with GNU find.
GNU find imposes a restriction that gname is numeric, while
find(1) does not.
-group gname
True if the file belongs to the group gname. If gname is numeric
and there is no such group name, then gname is treated as a group
ID.
-ignore_readdir_race
This option is for GNU find compatibility and is ignored.
-ilname pattern
Like -lname, but the match is case insensitive. This is a GNU
find extension.
-iname pattern
Like -name, but the match is case insensitive.
-inum n
True if the file has inode number n.
-ipath pattern
Like -path, but the match is case insensitive.
-iregex pattern
Like -regex, but the match is case insensitive.
-iwholename pattern
The same thing as -ipath, for GNU find compatibility.
-links n
True if the file has n links.
-lname pattern
Like -name, but the contents of the symbolic link are matched
instead of the file name. This is a GNU find extension.
-ls This primary always evaluates to true. The following information
for the current file is written to standard output: its inode
number, size in 512-byte blocks, file permissions, number of hard
links, owner, group, size in bytes, last modification time, and
pathname. If the file is a block or character special file, the
device number will be displayed instead of the size in bytes. If
the file is a symbolic link, the pathname of the linked-to file
will be displayed preceded by ``->''. The format is identical to
that produced by ``ls -dgils''.
-maxdepth n
Always true; descend at most n directory levels below the command
line arguments. If any -maxdepth primary is specified, it
applies to the entire expression even if it would not normally be
evaluated. ``-maxdepth 0'' limits the whole search to the com-
mand line arguments.
-mindepth n
Always true; do not apply any tests or actions at levels less
than n. If any -mindepth primary is specified, it applies to the
entire expression even if it would not normally be evaluated.
``-mindepth 1'' processes all but the command line arguments.
-mmin n
True if the difference between the file last modification time
and the time find was started, rounded up to the next full
minute, is n minutes.
-mnewer file
Same as -newer.
-mount The same thing as -xdev, for GNU find compatibility.
-mtime n[smhdw]
If no units are specified, this primary evaluates to true if the
difference between the file last modification time and the time
find was started, rounded up to the next full 24-hour period, is
n 24-hour periods.
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.
-name pattern
True if the last component of the pathname being examined matches
pattern. 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 (``\'').
-newer file
True if the current file has a more recent last modification time
than file.
-newerXY file
True if the current file has a more recent last access time
(X=a), inode creation time (X=B), change time (X=c), or modifica-
tion time (X=m) than the last access time (Y=a), inode creation
time (Y=B), change time (Y=c), or modification time (Y=m) of
file. In addition, if Y=t, then file is instead interpreted as a
direct date specification of the form understood by cvs(1). Note
that -newermm is equivalent to -newer.
-nogroup
True if the file belongs to an unknown group.
-noignore_readdir_race
This option is for GNU find compatibility and is ignored.
-noleaf
This option is for GNU find compatibility. In GNU find it dis-
ables an optimization not relevant to find(1), so it is ignored.
-nouser
True if the file belongs to an unknown user.
-ok utility [argument ...] ;
The -ok primary is identical to the -exec primary with the excep-
tion 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 (`y' in the
``POSIX'' locale), the command is not executed and the value of
the -ok expression is false.
-okdir utility [argument ...] ;
The -okdir primary is identical to the -execdir primary with the
same exception as described for the -ok primary.
-path pattern
True if the pathname being examined matches pattern. 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 (``\'').
Slashes (``/'') are treated as normal characters and do not have
to be matched explicitly.
-perm [-|+]mode
The mode may be either symbolic (see chmod(1)) or an octal num-
ber. 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.
-print0
This primary always evaluates to true. It prints the pathname of
the current file to standard output, followed by an ASCII NUL
character (character code 0).
-prune This primary always evaluates to true. It causes find to not
descend into the current file. Note, the -prune primary has no
effect if the -d option was specified.
-regex pattern
True if the whole path of the file matches pattern using regular
expression. To match a file named ``./foo/xyzzy'', you can use
the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'', but not
``xyzzy'' or ``/foo/''.
-samefile name
True if the file is a hard link to name. If the command option
-L is specified, it is also true if the file is a symbolic link
and points to name.
-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)
-type t
True if the file is of the specified type. Possible file types
are as follows:
b block special
c character special
d directory
f regular file
l symbolic link
p FIFO
s socket
-uid uname
The same thing as -user uname for compatibility with GNU find.
GNU find imposes a restriction that uname is numeric, while
find(1) does not.
-user uname
True if the file belongs to the user uname. If uname is numeric
and there is no such user name, then uname is treated as a user
ID.
-wholename pattern
The same thing as -path, for GNU find compatibility.
-xattr True if the file has any extended attributes.
-xattrname name
True if the file has an extended attribute with the specified
name.
OPERATORS
The primaries may be combined using the following operators. The opera-
tors are listed in order of decreasing precedence.
( expression )
This evaluates to true if the parenthesized expression evaluates
to true.
! expression
-not expression
This is the unary NOT operator. It evaluates to true if the
expression is false.
-false Always false.
-true Always true.
expression -and expression
expression expression
The -and operator is the logical AND operator. As it is implied
by the juxtaposition of two expressions it does not have to be
specified. The expression evaluates to true if both expressions
are true. The second expression is not evaluated if the first
expression is false.
expression -or expression
The -or operator is the logical OR operator. The expression
evaluates to true if either the first or the second expression is
true. The second expression is not evaluated if the first
expression is true.
All operands and primaries must be separate arguments to find. Primaries
which themselves take arguments expect each argument to be a separate
argument to find.
ENVIRONMENT
The LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES and LC_TIME environ-
ment variables affect the execution of the find utility as described in
environ(7).
EXAMPLES
The following examples are shown as given to the shell:
find / \! -name "*.c" -print
Print out a list of all the files whose names do not end in .c.
find / -newer ttt -user wnj -print
Print out a list of all the files owned by user ``wnj'' that are
newer than the file ttt.
find / \! \( -newer ttt -user wnj \) -print
Print out a list of all the files which are not both newer than
ttt and owned by ``wnj''.
find / \( -newer ttt -or -user wnj \) -print
Print out a list of all the files that are either owned by
``wnj'' or that are newer than ttt.
find / -newerct '1 minute ago' -print
Print out a list of all the files whose inode change time is more
recent than the current time minus one minute.
find / -type f -exec echo {} \;
Use the echo(1) command to print out a list of all the files.
find -L /usr/ports/packages -type l -exec rm -- {} +
Delete all broken symbolic links in /usr/ports/packages.
find /usr/src -name CVS -prune -o -depth +6 -print
Find files and directories that are at least seven levels deep in
the working directory /usr/src.
find /usr/src -name CVS -prune -o -mindepth 7 -print
Is not equivalent to the previous example, since -prune is not
evaluated below level seven.
COMPATIBILITY
The -follow primary is deprecated; the -L option should be used instead.
See the STANDARDS section below for details.
SEE ALSO
chflags(1), chmod(1), cvs(1), locate(1), lsvfs(1), whereis(1), which(1),
xargs(1), stat(2), acl(3), fts(3), getgrent(3), getpwent(3), strmode(3),
re_format(7), symlink(7)
STANDARDS
The find utility syntax is a superset of the syntax specified by the IEEE
Std 1003.1-2001 (``POSIX.1'') standard.
All the single character options except -H and -L as well as -amin,
-anewer, -cmin, -cnewer, -delete, -empty, -fstype, -iname, -inum,
-iregex, -ls, -maxdepth, -mindepth, -mmin, -path, -print0, -regex and all
of the -B* birthtime related primaries are extensions to IEEE Std
1003.1-2001 (``POSIX.1'').
Historically, the -d, -L and -x options were implemented using the pri-
maries -depth, -follow, and -xdev. These primaries always evaluated to
true. As they were really global variables that took effect before the
traversal began, some legal expressions could have unexpected results.
An example is the expression -print -o -depth. As -print always evalu-
ates to true, the standard order of evaluation implies that -depth would
never be evaluated. This is not the case.
The operator -or was implemented as -o, and the operator -and was imple-
mented as -a.
Historic implementations of the -exec and -ok primaries did not replace
the string ``{}'' in the utility name or the utility arguments if it had
preceding or following non-whitespace characters. This version replaces
it no matter where in the utility name or arguments it appears.
The -E option was inspired by the equivalent grep(1) and sed(1) options.
HISTORY
A find command appeared in Version 1 AT&T UNIX.
BUGS
The special characters used by find are also special characters to many
shell programs. In particular, the characters ``*'', ``['', ``]'',
``?'', ``('', ``)'', ``!'', ``\'' and ``;'' may have to be escaped from
the shell.
As there is no delimiter separating options and file names or file names
and the expression, it is difficult to specify files named -xdev or !.
These problems are handled by the -f option and the getopt(3) ``--'' con-
struct.
The -delete primary does not interact well with other options that cause
the file system tree traversal options to be changed.
The -mindepth and -maxdepth primaries are actually global options (as
documented above). They should probably be replaced by options which
look like options.
BSD September 28, 2011 BSD