我希望清除 30 天以上的用户文件。下面的 bash 脚本工作得很好。但是,我希望删除输出中显示的“没有这样的文件或目录”错误,因为我有自己的回声。有人可以帮忙吗?
代码:
if [[ $(find /h/$USER/*.txt -mtime +30) ]]
then
find /h/$USER/*.txt -mtime +30 -print -exec rm -f {} \;
else
echo "No txt files to del"
fi
Run Code Online (Sandbox Code Playgroud)
输出:
find: stat() error /h/username/*.txt: No such file or directory
No text files to del
Run Code Online (Sandbox Code Playgroud)
这就是我的做法,在我们想要每晚清理的一堆目录上运行。
find /h/$USER -maxdepth 1 -name "*.txt" -type f -mtime +30 -delete
Run Code Online (Sandbox Code Playgroud)
不能说这是最好还是最差的方法,但它已经运行了多年,没有任何问题,实际上是一个垃圾清理器的集合,所有语法都相同,没有问题,所以我想它对于生产来说已经足够好了。