1

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?

  • The solution depends on whether all files are known and static or new files may appear but shall be excluded, too. – Hauke Laging Jun 28 '13 at 3:23
  • The files are known now. But at some point in the future more may be added. – Andrew Hoos Jun 29 '13 at 6:50
1

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/
0

You can use --exclude-from option of rsync to define whatever you do not want to include to sync

For Example:

 #cat exclude.txt 
  baseDir/bad1
  baseDir/bad2

 #rsync --exclude-from=exclude.txt -avr source_dir_path destination_dir_path
0

rsync will take wildcards as arguments to the --exclude options so you can do something like this:

$ pwd
$ /some/dir

$ rsync -avz baseDir/ --exclude 'bad[1,2]' targetDir/.

For example:

$ rsync -avz baseDir/ --exclude 'bad[1,2]' targetDir/.
sending incremental file list
./
subDir1/
subDir1/file1
subDir2/
subDir2/file1

sent 185 bytes  received 61 bytes  492.00 bytes/sec
total size is 0  speedup is 0.00

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.