大网数据广告活动
服务器运维问答

rsync - exclude all directories except a few

· 52CE 我爱测

There appear to be a lot of SF and SO questions out there about this, but none seem to match my requirements. source_dir/ some_dir/ another_dir/ some_file.php some_other_file.txt include_this_dir/ include_that_dir/ yet_another_di…

问题原文

原帖链接 · Tom Auger · CC BY-SA 3.0

There appear to be a lot of SF and SO questions out there about this, but none seem to match my requirements.

source_dir/
  some_dir/
  another_dir/
  some_file.php
  some_other_file.txt
  include_this_dir/
  include_that_dir/
  yet_another_dir/

So I want to rsync two of those dirs, while excluding the rest. It's important that we exclude all but those two dirs, because there might be other files that at some later date are added to the source_dir and would need to be excluded without being explicitly listed.

I tried:

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

And my include.txt had

  include_this_dir/
  include_that_dir/

and also I tried adding

  source_dir/

No joy. Nothing gets included at all.

已采纳解答

原帖链接 · Darryl E. Clarke · CC BY-SA 3.0

A simple filter should do the trick. To build on the prior answer with a proper example -- Explicitly include the parent(s), plus all (**) sub folders and files. Then exclude everything else. Here's filter.txt:

+ /include_this_dir/
+ /include_this_dir/**
+ /include_that_dir/
+ /include_that_dir/**
- /**

With the command line:

rsync -av --dry-run --filter="merge filter.txt" source_dir/ dest_dir/

Would result in:

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)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。