linux BASH -删除超过3个月的文件?

f0ofjuux  于 5个月前  发布在  Linux
关注(0)|答案(2)|浏览(75)

如何删除超过3个月的文件?
90天后,我知道:

find /tmp/*.log -mtime +90 -type f -delete

字符串
但是我怎么知道3个月总是等于90天?确切的天数是多少?有没有更好的方法告诉-mtime遵循months

bxgwgixi

bxgwgixi1#

如果你想要3个月的确切天数,那么你可以用途:

days=$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))

字符串
并将其用作:

find /tmp/*.log -mtime +$days -type f -delete


或者直接在find中:

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))" -delete

m0rkklqb

m0rkklqb2#

为了以防万一,find支持其他时间差选项,包括:-amin-atime-cmin-ctime-mmin-mtime

#! /usr/bin/env bash

# For example, today is 2023-12-12.
# Let's find all files since the start of the month (i.e. 2023-12-01).

declare currentSecs="$( date -u -- '+%s'; )";
declare previousSecs="$( date -ud '2023-12-01' -- '+%s'; )";

find . -mmin "-$(( (currentSecs - previousSecs) / 60 ))";

个字符
来源:man findfind (GNU findutils) 4.8.0)。

相关问题