Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to exclude the directories "temp" and "Ubuntu One/" from backup. What is the correct escaping of the space????

RSYNCCONF="--delete --exclude=.* --exclude=temp/"

Thanks in advance!

  • Chris


excludes=( ".*" temp/ "Ubuntu One/" )' 'RSYNCCONF="${excludes[@]/#/--exclude=}" 



didn't work. I'd really appreciate any advice.

This is the script:

share|improve this question

If you're building up options in a string, you'll run into problems when you try to use it (rsync "$RSYNCCONF"). Better to use an array -- when you dereference the array, each element will be properly handled by the shell:

RSYNCCONF=( --delete '--exclude=.*' --exclude=temp/ '--exclude="Ubuntu One/"' )
rsync "${RSYNCCONF[@]}"
share|improve this answer
Well, I copied the whole script from… I don't know how to handle all the other options so I'll try Rajish's reply next time. Won't it work? – chris Jun 13 '11 at 17:12
You need to lose the outer quotes on the last --exclude there. – geirha Jun 13 '11 at 20:26
@chris I'm afraid that script is quite badly written, so it's not a good script to use as a template. – geirha Jun 13 '11 at 20:36
Thank you for all the answers. When I use an array like in glenn's or geirha's reply, what do I write in line 53 for example? $RSYNC -e "$S" -avR $FROMSSH:$SOURCE "${RSYNCCONF[@]}" $TARGET$TODAY $INC >> $LOG 2>&1 Simply replace $RSYNCCONF with the array? – chris Jun 13 '11 at 23:15
'excludes=( ".*" temp/ "Ubuntu One/" )' 'RSYNCCONF="${excludes[@]/#/--exclude=}"' and '$RSYNC -e "$S" -avR $FROMSSH:$SOURCE ${RSYNCCONF[@]} $TARGET$TODAY $INC >> $LOG 2>&1 ' didn't work. I'd really appreciate any advice. – chris Jun 14 '11 at 22:57

Put it into single quotes:

RSYNCCONF="--delete --exclude=.* --exclude=temp/ --exclude='Ubuntu One/' "

or put a backslash before the space:

RSYNCCONF="--delete --exclude=.* --exclude=temp/ --exclude=Ubuntu\\ One/ "
share|improve this answer
Thank you for replying quickly. – chris Jun 13 '11 at 15:27
You can't put syntactical quotes inside quotes. An array is the only sane way. – geirha Jun 13 '11 at 20:23

Here's one way of doing it.

excludes=( ".*" temp/ "Ubuntu One/" )
rsync --delete "${excludes[@]/#/--exclude=}"

Note that the above uses bash syntax, so it won't work with sh. Also, the quotes are vital, do not omit them.

See for an explanation of why putting more than one argument into a string variable is wrong.

EDIT: From the pastebin in your comments, this seems to be what you want.


backupname=$(date +%Y-%m-%d)-cd
excludes=( ".*" temp/ "Ubuntu One/" )

if [[ -d $backupdir ]]; then
    rsync -av ${linkdest:+"--link-dest=$linkdest"} --delete \
          "${excludes[@]/#/--exclude=}" "$source" "$backupdir/$backupname"
    echo "$0: $backupdir: Not a directory, make sure the filesystem is mounted" >&2
    exit 1
share|improve this answer
Dear geirha, thank you very much. This is exactly what I was looking for. I don't know anything about bash-scripting. Now I've a reliable backup! Thanks again. – chris Jun 16 '11 at 5:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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