14

There appear to be a lot of SF and SO questions out there about this, but none seem to match my requirements.

source_dir/
  some_dir/
  another_dir/
  some_file.php
  some_other_file.txt
  include_this_dir/
  include_that_dir/
  yet_another_dir/

So I want to rsync two of those dirs, while excluding the rest. It's important that we exclude all but those two dirs, because there might be other files that at some later date are added to the source_dir and would need to be excluded without being explicitly listed.

I tried:

rsync -av --dry-run --delete --exclude="source_dir/*" (or just "*") --include-from="include.txt" source_dir dest_dir

And my include.txt had

  include_this_dir/
  include_that_dir/

and also I tried adding

  source_dir/

No joy. Nothing gets included at all.

2
  • 1
    You might try to put the include option before the exclude option. Im not sure but rsync may stop checking after the first match. Apr 15, 2016 at 19:57
  • I think I tried that before, but it's worth another go
    – Tom Auger
    Apr 15, 2016 at 20:50

3 Answers 3

21
+50

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)
2
  • 7
    You can reduce it further with three "*", like + /include_this_dir/***, which means + /include_this_dir/ + /include_this_dir/**
    – Drasill
    Feb 1, 2017 at 10:09
  • This example works, but if you are tarteting files in folders several levels deep, you need to include all parent folders up to the target files. Like this: + /first_dir/ + /first_dir/second_dir/ + /first_dir/second_dir/** - /**
    – Shumoapp
    Feb 18 at 15:39
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.

0

New command string which creates --included dirs and copies it's content dir and/or files, but ONLY 2 DIRS DEEP. Why?

rsync -avid --progress ~/MyDocuments/'Inet Publishing'/ --recursive --include='LandisTwo (2019)' --include='LandisTwo (2020) {backup}' --include='*.*' --exclude='**/' e6420:/home/landis/Media/2TBackup/Backups/MyDocuments/"Inet\ Publishing"/ | tee -a ~/logs/Backup-InetPublishing_2020.txt
  • I've descovered that the Directories are Created, but NO FILES are copied to them (no recursion). files in parent are copied, but Not sub dirs - working on it.

this works for me, from workstation to server, backing up (rysnc) my personal site to another server in addition to the whole directory w/ all sites when rysnc'ing parent ../Inet\ Publishing (the 'n' in -avin is 'Dry-Run', remove.).

The --exclude */ is all other directories in 'Inet Publishing/' and seems to have to be After --include per man rsync process order ( if before --include, then Everything is --excluded and no further tokens are processed, at least that's how i read it and in practice dry runs when coming up with this. ).

This is actually one line out of one of my scripts i run with cron. *obviously, the destination host can be an IP, i have the hosts defined in /etc/hosts and have ssh key pairs,,, so i use the host name.

rsync -avin --progress ~/MyDocuments/'Inet Publishing'/ --include='LandisTwo (2019)' --include='LandisTwo (2020) {backup}' --exclude='*/' e6420:/home/landis/Media/2TBackup/Backups/MyDocuments/ | tee -a ~/logs/Backup-InetPublishing_2020.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.