rsync-排除子目录以外的所有内容,但排除子目录

时间:2018-06-21 14:50:38

标签: bash rsync

我正在尝试将远程目录同步到本地,我想排除子目录以外的所有内容。但是该子目录中有一个我要排除的子目录。我的理解是rsync在排除之后应用包含,我试图在这里找出必要的模式。这些是我正在传递的包含/排除标志:

rsync -Pav -e "ssh -i /path/to/key.pem" \
  --include="subdir/***" \
  --exclude="subdir/subsubdir" \
  --exclude="*" \
  remote@address.com:/path/to/remote/dir/ \
  /path/to/local/dir/

编辑:当前,这导致rsync捕获子目录中的所有内容,包括我要排除的子目录。

谢谢!

1 个答案:

答案 0 :(得分:2)

我对rsync如何应用过滤器的假设被误导了。 @meuh发表的评论是正确的答案:

  

rsync按照给它们的顺序应用过滤器,因此请尝试在包含之前先排除subsub。

我还必须在排除对象后面加上斜杠。这是正确的rsync命令:

rsync -Pav -e "ssh -i /path/to/key.pem" \
  --exclude="subdir/subsubdir/" \
  --include="subdir/***" \
  --exclude="*" \
  remote@address.com:/path/to/remote/dir/ \
  /path/to/local/dir/

这是在本地运行命令的演示:

rsync -Pav \
  --exclude="subdir/subsubdir/"
  --include="subdir/***" \
  --exclude="*" \
  ./remote/ \
  ./local/

之前:

.
├── local
└── remote
    ├── subdir
    │   ├── subsubdir
    │   │   └── test.txt
    │   └── test.txt
    └── test.txt

之后:

.
├── local
│   └── subdir
│       └── test.txt
└── remote
    ├── subdir
    │   ├── subsubdir
    │   │   └── test.txt
    │   └── test.txt
    └── test.txt