0

Why does this rsync command work when I give it literally but not when I create it from variables?

Here are the variables - first the options I'm passing to rysnc, as an array:

$ echo "${options[@]}"
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar"

$ echo ${options[6]}
-f

$ echo ${options[7]}
"- *.wma"

Then the source directory, from which rsync is to copy files:

$ echo "\"$dir/\""
"/media/test/Ahmad Jamal Trio/Live at the Pershing/"

And the target directory, to which rsync is to copy files:

$ echo "\"$target_dir\""
"/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"

Put it all together:

$ echo "${options[@]}" "\"$dir/\"" "\"$target_dir\""
-av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing//" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"

That all looks like it should. And indeed, it does work if you give the command literally, like this:

$ rsync -av --prune-empty-dirs -f "- *.flac" -f "- *.WMA" -f "- *.wma" -f "- *.ogg" -f "- *.mp4" -f "- *.m4a" -f "- *.webm" -f "- *.wav" -f "- *.ape" -f "- *.zip" -f "- *.rar" "/media/test/Ahmad Jamal Trio/Live at the Pershing/" "/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing/"
./
Ahmad Jamal Trio - Live at the Pershing - 01 - But Not for Me.mp3
Ahmad Jamal Trio - Live at the Pershing - 02 - Surrey With The Fringe On Top.mp3
Ahmad Jamal Trio - Live at the Pershing - 03 - Moonlight In Vermont.mp3
Ahmad Jamal Trio - Live at the Pershing - 04 - Music, Music, Music.mp3
Ahmad Jamal Trio - Live at the Pershing - 05 - No Greater Love.mp3
Ahmad Jamal Trio - Live at the Pershing - 06 - Poinciana.mp3
Ahmad Jamal Trio - Live at the Pershing - 07 - Wood'yn You.mp3
Ahmad Jamal Trio - Live at the Pershing - 08 - What's New.mp3
AlbumArtSmall.jpg
AlbumArtLarge.jpg
Folder.jpg

sent 43,194,376 bytes  received 285 bytes  28,796,440.67 bytes/sec
total size is 43,182,454  speedup is 1.00

But it fails when I call rsync using the vars as args:

$ rsync "${options[@]}" "\"$dir/\"" "\"$target_dir\""
Unknown filter rule: `"- *.flac"'
rsync error: syntax or usage error (code 1) at exclude.c(902) [client=3.1.2]
  • 1
    So ${options[7]} is literally "- *.wma", including the quotation marks? – DonHolgo Nov 5 '19 at 11:42
  • Yes literally. Please see new info above: array variable created to contain quotes desired on command line. – markling Nov 6 '19 at 0:57
3

Part of your rsync filters and the source and target directories are quoted with additional escaped quotes. Remove the escaped quotes and it should work:

options=(
  -av --prune-empty-dirs 
  -f "- *.flac" 
  -f "- *.WMA" 
  -f "- *.wma" 
  -f "- *.ogg" 
  -f "- *.mp4" 
  -f "- *.m4a" 
  -f "- *.webm" 
  -f "- *.wav" 
  -f "- *.ape" 
  -f "- *.zip" 
  -f "- *.rar"
)
dir="/media/test/Ahmad Jamal Trio/Live at the Pershing"
target_dir="/home/test/mp3/Ahmad Jamal Trio/Live at the Pershing"
rsync "${options[@]}" "$dir/" "$target_dir"

I removed the trailing slashes from the dir and target_dir variables, the / is already added to $dir in the rsync call.

|improve this answer|||||
  • 2
    @markling Don't put quotes or escapes in the array values, put them around the array reference when you use it. Quotes are shell syntax, not data, so don't mix them in your data and expect it to work. The fact that it looks ok when you echo it is highly misleading, since that shows things after the shell parsed it, including applying and removing quotes and escapes. If echo shows the quotes, that means they didn't get removed, and therefore didn't get applied either. So the when it looks right with echo, that means it's actually wrong. – Gordon Davisson Nov 6 '19 at 1:33

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.