Using 'rsync –exclude-from' to Exclude Files Containing Spaces

A few months ago I wrote a post about escaping filename or directory spaces for rsync. Well that wasn't the end of rsync giving me problems with spaces.

When I used the --exclude-from rsync option to specify a list of exclusions, I figured using single or double-quotes around files/directories that contain spaces would be enough to escape them. However, after swashing through hundreds and hundreds of lines from rsync's output, I discovered the excluded directories were still being synced!

When using --exclude-from, files and directories should not contain any single or double quotes, only a backslash:

/afs/*
/automount/*
/Users/raam/Documents/Virtual Machines/*

Note: A commenter pointed out that this no longer applies to the latest version of rsync. I tested this on Mac OS X 10.9 (Mavericks) and rsync v2.6.9 and confirmed that you no longer need to escape spaces in the exclude file.

Write a Comment

Comment

  1. Thanks. This is good information to know. I suspected that this was the case, but this will save me a decent amount of experimentation.

    Cheers!

  2. my rsync cmd is as:
    rsync -[options] —-exclude-from=exclude.file /mnt/xyz/ /home

    so in the exclude.file do I include the path as:
    – /mnt/xyz/abc/some.file or
    – /abc/some.file or
    – abc/some.file

    • Hi Dalal,

      The syntax for rsync is rsync [OPTION]... SRC [SRC]... DEST, so your example command will copy things FROM /mnt/xyz/ TO /home. I just want to make sure that’s what you’re trying to do!

      Regarding excluding: I believe the exclude pattern is in relation to the path you’re copying from. So, I think “abc/some.file” is the correct way to exclude in your example.

      To make sure, you can do a “dry run” of the command to see what will happen by using the “-n” option. This causes rsync to show you what it would do, but it doesn’t actually transfer any files.

      I hope this helps!

  3. I find that when rsync is used in a shell script instead of using “–exclude-from” you can embed the “–excludes” in an array inside of the script, like such:
    # Excludes list
    list=(
    –exclude ‘Crash*Reports/’
    –exclude *.iso
    )

    To exclude a directory that contain spaces the only way that I’ve found that works is to use an asterisk in replace of the space. Neither a slash-space ( ) nor a period (.) worked.

    I’m using RSYNC version 3.0.9 protocol version 30, the one that came with cygwin.

  4. I don’t know if this feature was changed after you wrote this post, but at least rsync 3.1.0 doesn’t work like this.

    One shoudn’t use any escaping, not even a backslash, in the exclude file. Spaces don’t have any special meaning there, so they cause no problem. A backslash before a space would be matched literally.

    • Thank you for pointing that out. I just tested this with rsync v2.6.9 and the exclude file works fine even when spaces are not escaped. That tells me the behavior must’ve changed, either with the Mac OS filesystem or with a newer version of rsync. I will add a note to the bottom of this post to indicate this issue no longer applies.