4

Is there any way to exclude multiples files and directory shortly with rsync?

Below is my code:

rsync -avzh . blabla@blabla:~/ --exclude=__pycache__ --exclude=checkpoints --exclude=logs --exclude=.git --exclude=plot

I have to explicitly declare for each file (dir). I feel it too long.

3 Answers 3

7

The documentation for rsync (see man rsync) has the --exclude-from parameter that will allow you to specify a list of exclusions in a file.

With regard to your set of example exclusions, the directories should be followed with / to show they are directories rather than unspecified files or directories, and those that are only in your home directory itself should be prefixed with / so that they don't match anywhere else.

In an exclusions file they could be listed like this

# Directories found anywhere
__pycache__/
checkpoints/
logs/
plot/

# Directories found only in HOME
/.git/
4

If you're using bash, you could use a brace expansion which expands to multiple --exclude=xxx options:

rsync -avzh --exclude={__pycache__,checkpoints,logs,.git,plot} . blabla@blabla:~/ 
3

Unless you can formulate a pattern that would match all possible directories/files that you'd like to exclude, the only other way I can think of is to read the patterns from a file with --exclude-from=filename.

You could also store the patterns in an array (in a shell that supports this, such as zsh, bash, ksh, or yash), which may be useful if you're writing a script:

exclude=( __pycache__ checkpoints logs .git plot )

for pattern in "${exclude[@]}"; do
    exclude=( "${exclude[@]:1}" --exclude="$pattern" )
done

rsync --archive --verbose --compress --human-readable \
    "${exclude[@]}" \
    . blabla@blabla:

Or use the positional parameters (less typing, and portable to all POSIX shells):

set -- __pycache__ checkpoints logs .git plot

for pattern do
    set -- "$@" --exclude="$pattern"
    shift
done

rsync --archive --verbose --compress --human-readable \
    "$@" \
    . blabla@blabla:
2
  • Perhaps a caveat about having a very long list (IE args limits)?
    – bxm
    Nov 15, 2019 at 9:37
  • @bxm If you have many thousands of patterns for things to exclude, you should probably use --exclude-from to read them from file. That would be, I think, a quite unusual situation though, but the code in the answer could easily be modified to write the patterns to a file that is used with --exclude-from.
    – Kusalananda
    Nov 15, 2019 at 9:40

You must log in to answer this question.

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