在 BSD 中,-j 用于防止设置日期,-f 参数用于设置输入日期的格式。 :
首先,您需要在距离 1970 年 1 月 1 日的天数中找到今天的日期:
today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
现在,您可以使用它来找出 7 天前的时间:
((cutoff = $today - 604800))
数字 604800 是 7 天的秒数。
现在,对于目录中的每个文件,您需要找到字符串的日期部分。我不知道更好的方法。 (也许有人知道一些 Bash 魔法)。
find . -type f | while read fileName
do
fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/')
yadda, yadda, yadda #Figure this out later
done
一旦我们有了文件日期,我们就可以使用 date 命令来确定该日期是否以秒为单位小于(因此比截止日期更早)
today=$(date -j -f "%Y-%m-%d" 1969-12-31 +%s)
((cutoff = $today - 604800))
find . -type f | while read fileName #Or however you get all the file names
do
fileDate=$(echo $foo | sed 's/.*-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/')
fileDateInSeconds=$(date -j -f "%Y-%m-%d" $fileDate +%s)
if [ $fileDateInSeconds -lt $cutoff ]
then
rm $fileName
fi
done
在 Linux 中,您使用 -d 参数来定义日期,该日期必须为 YYYY-MM-DD 格式:
today=$(date +"%Y-%m-%d)
现在,您可以计算秒数:
todayInSeconds=(date -d $today +%s)
其他一切都应与上述大致相同。