linux find
2019-01-11 16:22:44    690    0    0
gua_l

find用法

find [path...] [expression]

default path is the current directory; default expression is -print

--如果没有带路径,[path...] 为空,则默认是当前路径,如果 [expression] 也是空的,则默认是-print 

即只执行find 为 find ./ -print

一般常用的查找是:

 

类型匹配查找 -type ,f 为普通文件,d 为目录,l 为链接

find ./ -type d


 

名称匹配查找 -name ,匹配内容需要加引号

find ./ -type  f  -name "*.c"


 

时间匹配查找-mtime(modify time ,更新时间,+N ,N天以前修改过的,-N ,N天以内修改过的)

find ./ -type f -mtime +7 -name "*log"


 

新旧匹配 -newer filename ,查找比finename 这个文件更新的文件

find -type f -name "*log" -newer 201809150510.log

可以利用这个命令以更精细的时间戳进行查找

touch -t 201810100400.00 timetamp.txt

find -type f -name "*log" -newer  timetamp.txt


取反时,在选项前加! ,比如 ! -name "*log" 为查找不以log 结尾的文件


 

对查找结果加以处理:-exec cmd {} \ ;

{} \;  {}和\ 中间有空格,\ 后需要加分号,不加分号的话,\在命令行中表示命令未完进行换行。

将7天以前的log删除:

find ./ -type f -mtime +7 -name "*log" -exec rm {} \;


 

不递归查找:

find 默认是递归查找,如果只需要查找最外一层目录,将查找深度设置为1

-maxdepth LEVELS

-maxdepth 1


 

附find 的详细用法:

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 -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]

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>.

Pre: GitLab GEO 异地主副服务器同步--文档翻译

Next: mount/nfs 共享目录

690
Sign in to leave a comment.
No Leanote account? Sign up now.
0 comments
Table of content