find命令小结

背景:由于机器上log日志比较多,所以想写个脚本定时清理日志

find  /apps/logs/log_receiver -mtime +7 -name "*[log|err]" -exec rm -f {} ;

 

使用find命令来做这个事情

find [-H] [-L] [-P] [path...] [expression]

find 目录路径

-mtime 天数,+7表示7天前

-name 查看文件名字 可以使用通配符

-exec 执行shell脚本 {} ; 这为固定模式;

处理过程中发现一个奇怪的问题:

find /apps/logs/log_receiver/ -mtime +2 -name "*.err" -o  -name "*.log" -exec rm -f {} ;

上面的命令只能删除log日志,不能清除err

-o == or,或

-o最好跟()结合,有优先级处理

应该为:

find /apps/logs/log_receiver/ -mtime +2 ( -name "*.err" -o  -name "*.log" ) -exec rm -f {} ;

如果没有-exec默认为-print打印出来而已

find /apps/logs/log_receiver/ -mtime +2 -name "*.err" -o  -name "*.log" -exec rm -f {} ;

等同于

find /apps/logs/log_receiver/ -mtime +2 -name "*.err"-print -o  -name "*.log" -exec rm -f {} ;

 

其他可以参考man find

http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html

原文地址:https://www.cnblogs.com/jwentest/p/8186544.html