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

虽千万人,吾往矣!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值