14

How can I exclude files by default with rsync? Here is how my normal rsync syntax starts out:

rsync --exclude ".ht*" --exclude "error_log" --exclude ".DS*" --exclude "old" ...

I've seen a lot of mention of configuring the /etc/rsyncd.conf file, but maybe that's more for the daemon than the rsync command.

Is it possible to have some default excludes for rsync when called from the command line like in my default syntax above?

4 Answers 4

13

Add your excludes to a file, then use --exclude-from=/path/to/exclude_file

e.g.

# cat rsync.excludes
.ht*
error_log
.DS*
old
...

# rsync --exclude-from=rsync.excludes
2
  • 4
    looks like the syntax is --exclude-from and not --exclude-file but otherwise this seems to be the ticket. thanks!
    – cwd
    Commented Sep 29, 2011 at 15:12
  • the only advantage this gives over @scottatron later answer: this gets more verbose and so much less hidden... but, then again, who gets to this point of rsync needs without a customized shell?
    – cregox
    Commented Aug 30, 2022 at 20:26
6

No, rsync does not have a default configuration file to read upon invocation. It does not have a configuration file at all. The best you can do is what @frogstarr78 says: create a text file with patterns to exclude, and then point rsync to it with --exclude-from=filename.

But nothing is stopping you from overloading the rsync command name with the name of a shell function that automatically picks up your file of exclusion patterns:

rsync () {
    if [ -f "$HOME/.rsync.exclude" ]; then
        set -- --exclude-from="$HOME/.rsync.exclude" "$@"
    fi

    command rsync "$@"
}

This shell function prepends --exclude-from="$HOME/.rsync.exclude" to the rsync command line if the regular file .rsync.exclude exists in the user's home directory. It then calls rsync with the possibly modified list of arguments.

If you feel adventurous and know how to write the "new style" rsync filters (the ones presented in the section "FILTER RULES IN DEPTH" in the rsync(1) manual), then you can put both exclusion and inclusion filters (and show, hide, risk, and protect filters etc.) in the same file, and then use that with --filter='. filename' or --filter='merge filename'.

Our shell function, adapted for this (uses the file $HOME/.rsync.filters, if it exists):

rsync () {
    if [ -f "$HOME/.rsync.filters" ]; then
        set -- --filter="merge $HOME/.rsync.filters" "$@"
    fi

    command rsync "$@"
}

Use --filter='dir-merge,n .rsync.filters' to automatically read filter rules from files called .rsync.filters in the source directories that rsync reads (without letting the rules be valid for subdirectories; this is what n does), or --filter='dir-merge,n- .rsync.exclude' to read ordinary, "old style", exclusion filters from any .rsync.exclude file that rsync encounters on the source. See the "MERGE-FILE FILTER RULES" section of the rsync(1) manual for further information.

0
3

While rsync doesn't let you set default options, you can create a wrapper script and put it higher up in your $PATH than the rsync binary.

This is is my rsync wrapper which lives in ~/bin/rsync

#!/bin/sh

# Set path to the rsync binary
RSYNC=/usr/bin/rsync

# Look for these exclude files
IGNORE_FILES=(~/.rsyncignore ./.gitignore ./.rsyncignore)

EXCLUDE_FROM=""
for f in ${IGNORE_FILES[@]}; do
  if [[ -e $f ]]; then
    EXCLUDE_FROM="$EXCLUDE_FROM --exclude-from=$f "
  fi
done
$RSYNC $EXCLUDE_FROM "$@"

It'll look for ~/.rsyncignore, ./.gitignore, ./.rsyncignore files and, if any of them exist, use them as default --exclude-from arguments.

Just change the RSYNC and IGNORE_FILES to suit your envrionment and preferences.

1
  • much better general solution than current accepted answer, thanks for sharing!
    – cregox
    Commented Aug 30, 2022 at 20:24
-1

--exclude "/*" will exclude everything by default. Here is an example:

rsync -av --include "bin/" --exclude "/*" /source_dir/ /dest_dir/
1
  • This does not answer the question. The question is whether the utility has a way of configuring a specific set of default exclusion patterns.
    – Kusalananda
    Commented Jun 7 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .