条件我的列表中没有文件

时间:2013-09-17 15:32:49

标签: linux bash

我的剧本:

#!/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

我的问题是:如果我要归档文件,我会发现什么测试? THX

1 个答案:

答案 0 :(得分:1)

如果您想跳过没有要归档的文件的情况,这可以帮助您:

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...) ]我们检查结果是否返回结果。

相关问题