2

I'm trying to exclude a directory tree as part of an rsync command embedded in a bash script approximately like this:

$OPTIONS="-rl --exclude 'Some Parent Directory/Another Directory'"

rsync $OPTIONS $SOURCEDIR a@b.com:/SomeTargetDir

My aim is to sync all of $SOURCEDIR into /SomeTargetDir on the target machine, with the exception of everything under Some Parent Directory/Another Directory. However, I keep seeing errors of this form:

rsync: link_stat "/Users/myusername/Parent\" failed: No such file or directory (2)

I assume this is related to escaping the exclude path, but I just can't seem to get it right: every combination of \\, \ and so on that I try doesn't seem right. How can I write the exclude rule correctly?

I don't want to use --exclude-from unless I absolutely have to.

I am using rsync version 3.0.9 on OS X 10.8, syncing to Ubuntu 12.04 over SSH.

2 Answers2

1

Exclude matches patterns too. So try it like this:

$OPTIONS="-rl --exclude '*/Another Directory'"
rsync $OPTIONS $SOURCEDIR a@b.com:/SomeTargetDir

See this tutorial for more details.

EDIT #1

Another suggestion would be to try escaping double quotes rather than use single quotes:

$OPTIONS="-rl --exclude \"Some Parent\ Directory/Another\ Directory\""

The OP tried this alternative but it still didn't work for him in his case. It does work for me on Linux using version "3.0.8 protocol version 30" of rsync.

slm
  • 8,010
1

Use --exclude-from. The exclusion file will have all patterns on a new line, and you can forget about quotes, escaping and space problems.

I have a similar script to yours, and this is what I did. Note that with this solution, I am generating the exclusion file in my script, therefore I still have to make sure quotes are right. If you can have a static exclusion file, that will be even simpler.

# create a temporary file
RSYNC_EXCLUDES=$( mktemp -t rsync-excludes )
# remove the temp file when the script exits
trap "rm -f $RSYNC_EXCLUDES" EXIT
# populate the exclusion file
echo 'Some Parent Directory/Another Directory' > "$RSYNC_EXCLUDES"
# have your options point to your exclusion file
$OPTIONS="-rl --exclude-from='$RSYNC_EXCLUDES'"
# profit
rsync $OPTIONS $SOURCEDIR a@b.com:/SomeTargetDir
djjeck
  • 111