Ubuntu 16.04

I would like to backup all .txt, .cfg, and .ini files, while keeping their folder structure to a zip file in the backups folder.

I know there is an easier way to do this entire operation but this is what I have.


allcfg=$(find suan -name '*.cfg')
alltxt=$(find suan -name '*.txt')
allini=$(find suan -name '*.ini')
timeStamp="$(date +%Y--%b-%d--%k:%M--%P)"

#-- set some arrays ...
backupfiles=( "$allcfg" "$alltxt" "$allini" )

cd "$wdir"
zip -r "$backupfilename" "$backupfiles"

So I can see a nice output of the files it files but when it gets to the zipping part it errors out.

zip error: Nothing to do! (try: zip -r /home/files/backup/Backup-2018--Jul-04--21:37--pm.zip . -i suan/cfg/360controller.cfg

But that doesn't work either.

  • 2
    When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find – Rakesh Sharma Jul 5 '18 at 2:05

This is how the -r recursive option on 7z works, it's a recursive pattern search.

7z a -r "$backupfilename" \*.ini \*.cfg \*.txt

7z creates zip files just fine.

You could also do the multi-arg option on find if you somehow don't have 7zip,

find suan \( -name \*.cfg -o -name \*.txt -o -name \*.ini \) \
    -exec zip -r "$backupfilename" {} +
  • @thill Excellent! – needtoknow Jul 6 '18 at 3:40

Using find and zip:

find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' \
    -exec zip -r "$backupfilename" {} +

You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.

This would find all relevant files and archive them in your backup file.

Alternatively with bash, if there's not thousands of files:

set -s globstar nullglob dotglob
cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt

The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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