rsync only subdirectories and content

10,649

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/
Share:
10,649

Related videos on Youtube

Adam Hartwell
Author by

Adam Hartwell

Updated on September 18, 2022

Comments

  • Adam Hartwell
    Adam Hartwell over 1 year

    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?

    • Admin
      Admin almost 11 years
      The solution depends on whether all files are known and static or new files may appear but shall be excluded, too.
    • Admin
      Admin almost 11 years
      The files are known now. But at some point in the future more may be added.