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 filename # 查找名为filename的文件
- -perm # 按执行权限来查找
- -user username # 按文件属主来查找
- -group groupname # 按组来查找
- -mtime -n/+n # 按文件更改时间来查找文件,-n指n天以内,+n指n天以前,可以是小数,如 -0.25 表示 6h 以内
- -atime -n/+n # 按文件访问时间来查GIN: 0px">
- -ctime -n/+n # 按文件创建时间来查找文件,-n指n天以内,+n指n天以前
- -nogroup # 查无有效属组的文件,即文件的属组在/etc/groups中不存在
- -nouser # 查无有效属主的文件,即文件的属主在/etc/passwd中不存
- -newer f1/!f2 # 找文件,-n指n天以内,+n指n天以前
- -size n[c] # 查长度为n块[或n字节]的文件
- -depth # 使查找在进入子目录前先行查找完本目录
- -fstype # 查更改时间比f1新但比f2旧的文件
- -type b/d/c/p/l/f # 查是块设备、目录、字符设备、管道、符号链接、普通文件
- -mount # 查文件时不跨越文件系统mount点
- -follow # 如果遇到符号链接文件,就跟踪链接所指的文件
- -cpio # 查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
- -prune # 忽略某个目录
示例
find /data/bkee/logs/ -name "*log*" -mtime +30 -delete