Rsync:排除目录内容,但包含目录
Rsync: Exclude directory contents, but include directory
我知道可以用 --exclude
排除目录,如下所示:
rsync -avz --exclude=dir/to/skip /my/source/path /my/backup/path
这将省略目录 dir/to/skip
但是我想复制目录本身而不是内容 |是否有一个带有 rsync 的单行代码来实现这一点?
本质上,包括 dir/to/skip
但 排除 dir/to/skip/*
NOTE: I did search for this question. I found a lot of similar posts but not exactly this. Apologies if there is a dupe already.
尝试:
rsync -avz --include=src/dir/to/skip --exclude=src/dir/to/skip/* src_dir dest_dir
--include=src/dir/to/skip
包含目录。 --exclude=src/dir/to/skip/*
排除目录下的所有内容。
--exclude
选项采用 PATTERN
,这意味着您应该能够做到这一点:
rsync -avz --exclude='dir/to/skip/*' /my/source/path /my/backup/path
请注意,引用 PATTERN
是为了防止 shell 对其进行 glob 扩展。
由于 dir/to/skip
与模式 dir/to/skip/*
不匹配,它将被包括在内。
这里有一个例子来证明它是有效的:
> mkdir -p a/{1,2,3}
> find a -type d -exec touch {}/file \;
> tree --charset ascii a
a
|-- 1
| `-- file
|-- 2
| `-- file
|-- 3
| `-- file
`-- file
3 directories, 4 files
> rsync -r --exclude='/2/*' a/ b/
> tree --charset ascii b
b
|-- 1
| `-- file
|-- 2
|-- 3
| `-- file
`-- file
3 directories, 3 files
需要注意的是,上面PATTERN
中的前导/
代表源目录的根目录,不是文件系统根目录。 rsync
手册页对此进行了解释。如果省略前导斜杠,rsync
将尝试匹配每个路径的 end 中的 PATTERN
。这可能会导致意外排除文件。例如,假设我有一个目录 a/3/2/
,其中包含我 想要传输的一堆文件。如果我省略前导 /
并执行:
rsync -r --exclude='2/*' a/ b/
那么 PATTERN
将同时匹配 a/2/*
和 a/3/2/*
,这不是我想要的。
我知道可以用 --exclude
排除目录,如下所示:
rsync -avz --exclude=dir/to/skip /my/source/path /my/backup/path
这将省略目录 dir/to/skip
但是我想复制目录本身而不是内容 |是否有一个带有 rsync 的单行代码来实现这一点?
本质上,包括 dir/to/skip
但 排除 dir/to/skip/*
NOTE: I did search for this question. I found a lot of similar posts but not exactly this. Apologies if there is a dupe already.
尝试:
rsync -avz --include=src/dir/to/skip --exclude=src/dir/to/skip/* src_dir dest_dir
--include=src/dir/to/skip
包含目录。 --exclude=src/dir/to/skip/*
排除目录下的所有内容。
--exclude
选项采用 PATTERN
,这意味着您应该能够做到这一点:
rsync -avz --exclude='dir/to/skip/*' /my/source/path /my/backup/path
请注意,引用 PATTERN
是为了防止 shell 对其进行 glob 扩展。
由于 dir/to/skip
与模式 dir/to/skip/*
不匹配,它将被包括在内。
这里有一个例子来证明它是有效的:
> mkdir -p a/{1,2,3}
> find a -type d -exec touch {}/file \;
> tree --charset ascii a
a
|-- 1
| `-- file
|-- 2
| `-- file
|-- 3
| `-- file
`-- file
3 directories, 4 files
> rsync -r --exclude='/2/*' a/ b/
> tree --charset ascii b
b
|-- 1
| `-- file
|-- 2
|-- 3
| `-- file
`-- file
3 directories, 3 files
需要注意的是,上面PATTERN
中的前导/
代表源目录的根目录,不是文件系统根目录。 rsync
手册页对此进行了解释。如果省略前导斜杠,rsync
将尝试匹配每个路径的 end 中的 PATTERN
。这可能会导致意外排除文件。例如,假设我有一个目录 a/3/2/
,其中包含我 想要传输的一堆文件。如果我省略前导 /
并执行:
rsync -r --exclude='2/*' a/ b/
那么 PATTERN
将同时匹配 a/2/*
和 a/3/2/*
,这不是我想要的。