rsync - exclude all directories except a few
Solution 1:
A simple filter should do the trick. To build on the prior answer with a proper example -- Explicitly include the parent(s), plus all (**) sub folders and files. Then exclude everything else. Here's filter.txt
:
+ /include_this_dir/
+ /include_this_dir/**
+ /include_that_dir/
+ /include_that_dir/**
- /**
With the command line:
rsync -av --dry-run --filter="merge filter.txt" source_dir/ dest_dir/
Would result in:
sending incremental file list
created directory dest_dir
./
include_that_dir/
include_that_dir/somefile.txt
include_that_dir/subdir/
include_this_dir/
sent 202 bytes received 65 bytes 534.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
Solution 2:
To cite from the rsync
manual section FILTER RULES
:
[..] 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.
Thus, it might be your best option to read your filter rules from a file, and sort them accordingly. Example:
CW+ source_dir/include_this_dir/
CW+ source_dir/include_that_dir/
CW- source_dir/
Your can specify the filter rules file with the --filter=
parameter.