Using Rsync Include and Exclude Options to Include Directory and File by Pattern

Using Rsync include and exclude options to include directory and file by pattern

The problem is that --exclude="*" says to exclude (for example) the 1260000000/ directory, so rsync never examines the contents of that directory, so never notices that the directory contains files that would have been matched by your --include.

I think the closest thing to what you want is this:

rsync -nrv --include="*/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/

(which will include all directories, and all files matching file_11*.jpg, but no other files), or maybe this:

rsync -nrv --include="/[0-9][0-9][0-9]0000000/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/

(same concept, but much pickier about the directories it will include).

How to use include and exclude precedence with rsync?

I'm unsure if there's a more native way to cross post an answer, but I asked on Stack Exchange and got this excellent answer that solved what I was trying to do perfectly, with context.

rsync include specific files and exclude directory

You need to put --exclude='build' before --include '*/'. Both of these rules could apply to the "build" directory, and whichever is given first takes precedence, so to get the --exclude rule to override the --include rule, you need to list it first.

From the rsync man page, in the FILTER RULES section (with my emphasis):

As the list of files/directories to transfer is built, rsync checks
each name to be transferred against the list of include/exclude
patterns in turn, and the first matching pattern is acted on: if it is
an exclude pattern, then that file is skipped; if it is an include
pattern then that filename is not skipped; if no matching pattern is
found, then the filename is not skipped.

rsync include only directory pattern

This should work:

--include='*/'
--include='*cache*/**'
--exclude='*'
--prune-empty-dirs

That says:

  • Include all folders (this is necessary to search inside them).
  • Include all files with "cache" in the name of a parent directory.
  • Exclude everything else.
  • Prune away any folders that were copied but turned out to contain no caches. Unfortunately, this also removes any empty folders within cache directories, but hopefully that's not important to you.

rsync --include option does not exclude other files

You need to add a --exclude all and it has to come after the --include

rsync -zvr --include="*.sh" --exclude="*" $from/*  root@$host:/home/tmp/

rsync: --include-from vs. --exclude-from what is the actual difference?

rsync doesn't work like that. Any file with a filename pattern that does not match any of the include or exclude patterns are considered to be included. In other words, think of the include pattern as a way of overriding exclude pattern.

From the docs (emphasis mine):

Rsync builds an ordered list of include/exclude options as specified on the command line. Rsync checks each file and directory name against each exclude/include pattern in turn. The first matching pattern is acted on. If it is an exclude pattern, then that file is skipped. If it is an include pattern then that filename is not skipped. If no matching include/exclude pattern is found then the filename is not skipped.

So, if you want to include only specific files, you first need to include those specific files, then exclude all other files:

--include="*/" --include="*.cfg" --exclude="*"

Couple of things to note here:

  1. The include patterns have to come before the excludes, because the first pattern that matches is the one that gets considered. If the file name matches the exclude pattern first, it gets excluded.

  2. You need to either include all subdirectories individually, like --include="/opt" --include="/opt/dir1" etc. for all subdirectories, or use --include="*/" to include all directories (not files). I went with the second option for brevity.

It is quirky and not very intuitive. So read the docs carefully (the "EXCLUDE PATTERNS" section in the link) and use the --dry-run or -n option to make sure it is going to do what you think it should do.

Rsync include, exclude and filters

I finally found where my issue was. I had the source dir in double quotes thus interpreting the * as a literal... Thanks anyway.

Rsync include a specific file under a certain structure

Is there a reason you're using --include? If it's a single file then rsync /one/two/file.txt /target/ would work.

Have a look at this answer:
How to RSYNC a single file?

If you're trying to send more than one file then you can use this, based on a pattern:
Using Rsync include and exclude options to include directory and file by pattern



Related Topics



Leave a reply



Submit