Q:

Condition when I do not have a file in my list

My script:
#!/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

my question is: What test do I find out if I have files to be archived or not ?
Thx

A:

If you want to skip the case in which there are no files to archive, this can help you:
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

The syntax if [ -z "string" ] checks if a string is empty or not. Negating it, if [ ! -z $(find...) ] we check if the result returns results or not.

