find命令小结

时间:2022-04-24
本文章向大家介绍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