我的脚本:
#!/bin/bash
NOW=$(date +"%Y%m%d%t%H%M%S")
# Pour chaque dossiers "log" trouvé.
for folder in $(find . -name "*log*" -type d )
do :
# Pour chaque dossier log contenant des fichiers ".log" vieux de +30jours.
tar czf archive-log.$NOW.tar.gz $(find $folder -name "*.log" -mtime +30)
# On supprime les anciens fichiers de log
rm $(find $folder -name "*.log" -mtime +30)
done
我的问题是:如果我有要归档的文件,我应该通过什么测试来确定? 谢谢
最佳答案
如果你想跳过没有文件要归档的情况,这可以帮助你:
if [ ! -z $(find $folder -name "*.log" -mtime +30) ]
then
tar czf archive-log.$NOW.tar.gz $(find $folder -name "*.log" -mtime +30)
rm $(find $folder -name "*.log" -mtime +30)
else
echo "there are no files to archive"
fi
语法 if [ -z "string"]
检查字符串是否为空。否定它,if [ ! -z $(find...) ]
我们检查结果是否返回结果。
关于linux - 当我的列表中没有文件时的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854018/