Rsync – Sync Only Subdirectories and Content

directoryfilesrsync

I have a directory that contains a number of files and subdirectories. See below:

baseDir
  bad1
  bad2
  subDir1
    file1
  subDir2
    file1

I would like to rsync the contents of baseDir without rsyncing bad1 and bad2.

resulting in

targetDir
  SubDir1
    file1
  SubDir2
    file1 

I don't care if it takes two commands but how do I do it?

Best Answer

You can either include the desired directories and their parent and exclude everything else, or exclude the bad directories. Note that order matters: rsync decides what to do with a file from the first matching pattern (so for example anything after --exclude=* is effectively ignored). Note also that excluding a directory prevents it not only from being copied but also from being traversed, so this effectively excludes everything below it. See this rsync filter primer for more information.

rsync --include='/' --include='/subDir***' --exclude='*' baseDir/ targetDir/
rsync --exclude='/bad*' baseDir/ targetDir/
Related Question