linux find命令用法
1、搜索某一时间段建立的文件:
find . -type f -newermt "2023-11-15 11:55:00" ! -newermt "2023-11-15 12:00:00"
-newerXY argument 表示搜索argument之后文件。X和Y可以下如下组合:
a The access time of the file reference
B The birth time of the file reference
c The inode status change time of reference
m The modification time of the file reference
t reference is interpreted directly as a time
搜索argument之前的文件可以使用! -newerXY
2、搜索到的文件直接对其进行某种操作:
find /etc_data/pass/send -maxdepth 1 -type d ! -newermt $(date -d 'now -4 days' '+%Y%m%d') -exec echo {}\/ \;
最后的 \;是必须存在的,表示命令执行完毕,而且\;前面必须有一个空格用于区分命令结尾符号。
-exec代表搜索到的文件要执行某一操作,其中{}代表搜索到的文件
3、排除某个搜索路径
find /etc_data -path *vlpr/backup -prune -o -type f -exec grep -i G00012300100111011020260622104129400 {} \;
这里是排除了*vlpr/backup这个路径,-path需要写完整路径或者使用通配符代替前面的路径。或者:
find /etc_data -type d -name backup -prune -o -type f -exec grep -i G00012300100111011020260622104129400 {} \;
注意:
expr1 -o expr2
在使用-o时,expr1为真expr2不执行;expr1为假expr2执行,所以排除语句中的顺序很重要。