As a follow up to a previous question, linked here, I have revised the code and developed what I believe to be a better solution.

In summary, the script should backup all files in a particular directory, and assign them to a particular zip file based on their modification dates. This should only occur for files modified yesterday and older, and skip files with the ".zip" extension.

Please let me know if their are any improvements that you would make? From testing the code, it has been working with no issues:


yestdayend=$(date --date="yesterday" +"%Y-%m-%d 23:59:59")

filelist=$(find $path -maxdepth 1 ! \( -name "*.zip" \) -type f ! -newermt "$yestdayend")

for file in $filelist
        moddate=$(stat -c %y $file | cut -d " " -f 1)
        if zip -rv $path/"logbackup-"$moddate.zip $file; then
           rm $file

1 Answer 1


Saving the output of find in a variable and looping over it is not a good idea in general: filenames with white-space (space, tab, newline) will be split, so the loop will not work on them.

It would be a little bit better to use a while loop instead:

find ... | while read file
        # ...

But this is still not great, as it won't protect you from newlines. But for your use case, it might be good enough. For a more robust solution, see this other post.

The \( ... \) is pointless here:

find $path -maxdepth 1 ! \( -name "*.zip" \) -type f ! -newermt "$yestdayend"

This is the same, but shorter and simpler:

find $path -maxdepth 1 ! -name "*.zip" -type f ! -newermt "$yestdayend"

It's a bit odd to quote "logbackup-" here:

if zip -rv $path/"logbackup-"$moddate.zip $file; then

Literal strings like that don't need quoting in Bash. On the other hand, it would be good to quote $path:

if zip -rv "$path"/logbackup-$moddate.zip $file; then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.