find mtime参数+号,-号,不带符号的用法

注意删除要精确匹配类型或者文件名,不能删除要查找的目录路径

find . -mtime +0 -type f -name "oms*" | xargs rm -f 删除24小时以前 oms格式的文件  

按文件更改时间来查找文件,-n指n天以内,+n指n天以前参考下图

注意这里的n,如果n为带有+号的值,意思为查找n天前所有的文件

如果n=+1且当前是24号9点,那么查找22号9点以前的数据,不包括22号9点到23号9点这段时间,

如果n=-1,则为查找一天内的文件,比如当前时间24号9点,那么查找23点9点到24点9点时间段的数据

如果n=-2,则代表查找两天内的所有数据,比如当前时间24号9点,那么查找22点9点到24点9点时间段的数据

如果不带有符号,那么则查找指定前n天中这一天的数据,比如(n=1)且当前时间24号9点,那么查找22点9点到23点9点这个时间段的数据

1.删除2h之前的文件和目录:

find /data0/docker/overlay2/*/merged/cache/www/workspace/*/output -cmin +120 -name "*"

注意: * 不能匹配 斜杠 / 

2.压缩 *.log 文件

10 01-19 * * * root find /data1/xxx/logs/ttt -type f -cmin +540 -name "*.log" -exec gzip {} ; > /dev/null 2>&1

3.同时删除多个后缀的文件

20 * * * * root nice -n 19 find /data1/xxx/logs ( -name "*.log" -o -name "*.log.gz" ) -mmin +360 -exec rm -rf {} ; >/dev/null 2>&1

4.删除3天之前的文件

find /workspace/logs/ -mtime +2 -name "dubbo.log*" -delete

5.gzip压缩(默认会删除源文件)

find /data0/data1/www/applogs/live.weibo.com/resource -not -name "*.gz" -cmin +120  -type f -exec gzip -q -f --best {} ; > /dev/null 2>&1

扩展补充:

压缩:gzip src ---执行后生成src.gz,但是会把src这个原文件删除
解决:
gzip -c src > src.gz

解压缩:gzip -d 1.gz  执行后生成1,但是会把1.gz这个原压缩文件删除
解决:
gzip -dc 1.gz > 1

  

其他

37 4 * * * test -d /data1/www/cache && /usr/bin/find /data1/www/cache/ -atime +3 -type f -exec rm -f {} ; 2>&1
40 3 * * * test -d /var/spool/mail && find /var/spool/mail -mtime +1 -type f | xargs rm -f > /dev/null 2>&1
0 17 * * * test -d /data1/www/applogs/ && find /data1/www/applogs/ -mtime +5 -daystart -type f -delete > /dev/null 2>&1
0 17 * * * test -d /data1/www/logs/ && find /data1/www/logs/ -mtime +5 -daystart -type f -delete > /dev/null 2>&1
0 6 * * * test -d /data1/www/applogs/ && find /data1/www/applogs/ -not -name "*.gz" -mtime +0 -daystart -type f -exec gzip -q -f --best {} ; > /dev/null 2>&1
00 3 * * * test -d /var/log/sa && find /var/log/sa -mtime +1 -type f | xargs rm -f > /dev/null 2>&1
00 3 * * * test -d /var/log/sudo_bak && find /var/log/sudo_bak -mtime +1 -type f | xargs rm -f > /dev/null 2>&1
0 4 * * * find /var/lib/docker/containers/ -size +1G -exec cp /dev/null {} ; &
0 2 * * * root test -d /data1/daemon_log/krcom && find /data1/daemon_log/krcom/*/logs/ -mtime +7 -type f -exec rm -rf {} ; 2>&1
0 2 * * * root test -d /data1/daemon_log/krcom && find /data1/daemon_log/krcom/strike_message/data/ -mtime +7 -type f -exec rm -rf {} ; 2>&1
0 * * * * test -d /data0/data1/daemon_log/krcom/strike_message && find /data0/data1/daemon_log/krcom/strike_message -type f -mtime +2 -delete &>/dev/null
0 * * * * test -d /data0/daemon_log/krcom/media/logs && find /data0/daemon_log/krcom/media/logs -type f -mtime +2 -delete &>/dev/null

原文地址:https://www.cnblogs.com/robinunix/p/12188643.html