2

I would like to sync two directories with excluding a few files. I tried below code but it fails in exclude.

FILES=abc.zip,hh.sh,Workarea/test/hi.jpg
rsync -av --exclude={$FILES} /home/test/ /home/test2

To verify it, i have executed as

# sh -x test.sh
+ FILES=abc.zip,hh.sh,Workarea/test/hi.jpg
+ rsync -av '--exclude={abc.zip,hh.sh,Workarea/test/hi.jpg}' /home/test/ /home/test2
2
  • Does the list of files need to be in a variable?
    – roaima
    Jan 30 '19 at 14:47
  • yes.. i will use it as runtime input..
    – Siva
    Jan 30 '19 at 14:50
6

You can't use the bash expansion {a,b,c} as anything other than a literal (you can't put a,b,c or even {a,b,c} in a variable and have it expanded directly. You could use eval but that opens up lots of unexpected complications, and I really do not recommend it:

FILES=abc.zip,hh.sh,Workarea/test/hi.jpg
eval rsync -av --exclude={$FILES} /home/test/ /home/test2    # Don't do this!

Anything is fine

Here's a better alternative suggestion for you, which uses the bash array feature to hold the set of exclusions. We then build up the command line for rsync by iterating across it.

files=(abc.zip hh.sh Workarea/test/hi.jpg "Some file with a space in its name")

excludes=()
for f in "${files[@]}"
do
    excludes+=(--exclude "$f")
done
rsync -av "${excludes[@]}" /home/test/ /home/test2

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.