Linux find 命令可以用来查找文件和目录,并对它们进行后续操作。它支持按文件、文件夹、名称、创建日期、修改日期、所有者和权限进行等镜像搜索。通过使用 -exec
参数可以对找到的文件或文件夹执行其他UNIX命令。
Help
# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype
normal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
-context CONTEXT
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.
参数说明
按文件名查找
-name pattern
按文件名查找,支持正则-iname pattern
按文件名查找,支持正则,不区分大小写-regex pattern
按正则查找
按属组、属主、权限查找
-user username
按文件属主来查找-group groupname
按组来查找-uid uid
按UID号查找-gid gid
按GID查找-nouser
查无有效属主的文件,即文件的属主在/etc/passwd中不存-nogroup
查无有效属组的文件,即文件的属组在/etc/groups中不存在-perm MODE
按执行权限来查找
按文件类型查找
-type type
其中,type 如下:b
块设备文件f
普通文件d
目录文件c
字符设备文件l
连接文件s
套接字文件p
管道文件
按时间查找
-atime -n/+n
按文件访问时间来查-n
指 n 天以内(下同),-0.25
表示 6h
以内+n
指 n 天以前(下同)
-mtime -n/+n
按文件更改时间查找文件-ctime -n/+n
按文件创建时间查找文件-newer f1/!f2
以 file 文件为条件,判断比它新的文件f1
表示比 f1 新的!f2
表示比 f1 旧的
-amin [+|-]n
以访问时间(分钟)查找-mmin [+|-]n
以数据修改时间(分钟)查找-cmin [+|-]TIME
以元数据修改时间(分钟)查找
按文件大小查找
-size [+|-]SIZE
按文件大小查询,大小包含 K
、M
、G
的单位,示例-size 5M
查找大小为 5M 的文件,允许有小偏差-size -5M
查询比5M小的文件-size +5M
查询比5M大的文件
其他
-maxdepth n
按文件夹深度,最多搜索n层-mindepth n
按文件夹深度,最少搜索n层-depth
使查找在进入子目录前先行查找完本目录-fstype
查更改时间比f1新但比f2旧的文件-mount
查文件时不跨越文件系统mount点-follow
如果遇到符号链接文件,就跟踪链接所指的文件-cpio
查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到-prune
忽略某个目录-empty
查找内容为空的文件
组合条件
-a
与,find . -type f -a -size +5M
-o
或,find . -type f -o -nouser
-not
or !
非find . -not -type f
查找非普通文件find . ! -type f
同上
处理动作
-print
默认为打印,输出入屏幕-ls
以ls格式输出-delete
删除查找到的文件-fls /path-to-somefile
将查询到的文件,以 ls 详细信息格式保存到 somefile 文件中-ok COMMAND {} \;
将查找到的文件传递给 COMMAND
命令,并且每步都提示用户确认操作-exec COMMAND {} \;
将查找到的文件传递给 COMMAND
命令,直接修改完成,无确认- 可以采用
xargs -i
代替符为 {}
,示例 find . -type f | xargs -i cp {} /tmp
find -exec unlink
示例
# 查找30天前修改的文件,并删除
find /data/bkee/logs/ -name "*log*" -mtime +30 -delete
# 查找7天没有访问的文件
find /etc -atime +7 -ls
# 查找1天没有修改的文件
find /etc -mtime -1 -ls
# 查找 /tmp 目录下文件名不包含 abc 的文件
find /tmp -not -name *abc*
查找大于10M的文件
find / -type f -size +10000000c -exec du -sh {} \;
dos2unix
find -type f | xargs dos2unix -o
chmod
find ./ -type f -name "*.sh" | xargs -i chmod +x {}
扩展
mlocate
是一个基于命令行的文件查找工具,比 find
命令更快、更精确,并且支持正则表达式搜索