仅 rsync 子目录和内容

仅 rsync 子目录和内容

我有一个包含许多文件和子目录的目录。见下文:

baseDir
  bad1
  bad2
  subDir1
    file1
  subDir2
    file1

我想 rsync baseDir 的内容而不 rsync bad1 和 bad2。

导致

targetDir
  SubDir1
    file1
  SubDir2
    file1 

我不在乎是否需要两个命令,但我该怎么做呢?

答案1

您可以包含所需的目录及其父目录并排除其他所有目录,也可以排除不良目录。请注意,顺序很重要:rsync 决定如何处理第一个匹配模式中的文件(因此,例如之后的任何内容都--exclude=*将被有效忽略)。另请注意,排除目录不仅会阻止其被复制,还会阻止其被遍历,因此这实际上会排除其下面的所有内容。看这个 rsync 过滤器底漆了解更多信息。

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

答案2

您可以使用--exclude-from选项来rsync定义您不想包含的同步内容

例如:

 #cat exclude.txt 
  baseDir/bad1
  baseDir/bad2

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

答案3

rsync会将通配符作为--exclude选项的参数,因此您可以执行以下操作:

$ pwd
$ /some/dir

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

例如:

$ 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

相关内容