rsync - 排除除少数目录之外的所有目录

Tom*_*ger 10 rsync

似乎有很多关于此的 SF 和 SO 问题,但似乎没有一个符合我的要求。

source_dir/
  some_dir/
  another_dir/
  some_file.php
  some_other_file.txt
  include_this_dir/
  include_that_dir/
  yet_another_dir/
Run Code Online (Sandbox Code Playgroud)

所以我想 rsync 其中两个目录,同时排除其余的。排除这两个目录以外的所有目录很重要,因为可能有其他文件在以后添加到 source_dir 中,需要在未明确列出的情况下将其排除。

我试过:

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

我的 include.txt 有

  include_this_dir/
  include_that_dir/
Run Code Online (Sandbox Code Playgroud)

我也尝试添加

  source_dir/
Run Code Online (Sandbox Code Playgroud)

没有喜悦。什么都不包括在内。

小智 16

一个简单的过滤器应该可以解决问题。用一个适当的例子建立在先前的答案的基础上——明确包含父文件夹,加上所有 (**) 子文件夹和文件。然后排除所有其他内容。这是filter.txt

+ /include_this_dir/
+ /include_this_dir/**
+ /include_that_dir/
+ /include_that_dir/**
- /**
Run Code Online (Sandbox Code Playgroud)

使用命令行:

rsync -av --dry-run --filter="merge filter.txt" source_dir/ dest_dir/
Run Code Online (Sandbox Code Playgroud)

会导致:

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)
Run Code Online (Sandbox Code Playgroud)

  • 你可以用三个“*”进一步减少它,比如`+ /include_this_dir/***`,这意味着`+ /include_this_dir/ + /include_this_dir/**` (4认同)