删除在"7"天之前创建的文件,无论修改时间如何



我必须删除超过7天的日志文件,即使它们在7天内被修改。但是我能找到的唯一解决方案是基于使用mtime选项的查找命令,如下所示:

find /path/to/files -mtime +7 -exec rm {} ;

这个问题的可能解决方案是什么?

如果您正在使用跟踪文件出生时间的文件系统和当前足够多的Linux内核、glibc和GNU内核,则可以执行以下操作

weekago=$(date -d "last week" +%s)
for file in /path/to/files/*; do
birthdate=$(stat -c %W "$file")
if [[ $birthdate -gt 0 && $birthdate -lt $weekago ]]; then
printf "%sn" "$file"
# Uncomment when you're sure you're getting just the files you want
# rm -- "$file"
fi
done

最新更新