Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    rsync dynamic command --exclude in bash scripts


    I have a problem in a bash Script using rsync with a dynamic --exclude option. Here is a very simple example to demonstrate the behavior:

    The directory contains several files and folders:

    With "testscript" I wanna save my files and subfolder "testdir1" (NOT "testdir2") into my targetdir. The Testscript looks as follows:

    # Variante 1:
    #rsync -aEzuv --no-g --no-o --stats --exclude="testdir2" ./sourcedir/ ./targetdir
    # Variante 2:
    teststring=" --exclude=\"testdir2\""
    rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir
    echo "rsync-String: rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir"
    # Variante 3:
    #echo "- testdir2" > excludefile
    #rsync -aEzuv --no-g --no-o --stats --exclude-from="excludefile" ./sourcedir/ ./targetdir
    #rm excludefile
    "Variante 1" works (as expected). In "Variante 2" I save the exclude-params in a string and put that into the rsync-command. Here "testdir2" isn't excluded. WHY??? The output of the Command-String is correct and if I copy it into command line the command is executed correctly...!

    I found out that the dynamical exclude can be acheved with "Variante 3" using exclude-from=FILE, but I don't understand why "Variante 2" doesn't work...

    Can somebody help me with this problem?!
    Last edited by nenico84; 11-17-2013 at 10:40 AM.

  2. #2
    hello and welcome, nenico!

    try one of the following:

    leave off the escaped quotes, b/c they're being interpreted as part of the actual directory name, e.g.:
    teststring=" --exclude=testdir2"
    or use bash's eval built-in, e.g.:
    teststring=" --exclude=\"testdir2\""
    eval rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir

  3. #3
    Hi atreyu,

    thanx very much for your advice!!! The first one doesn't work unfortunately and would also be complicated for my script because I would have to transform the foldernames additionaly in case of spaces in directory names. But the eval-command works perfectly!!! I will put it in my script immediately!

    Thanx again...

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts