rsync仅复制特定子文件夹中的特定文件,而不创建空文件夹(rsync copy only specific files from specific subfolders, without creat

系统教程 行业动态 更新时间:2024-06-14 17:02:17
rsync仅复制特定子文件夹中的特定文件,而不创建空文件夹(rsync copy only specific files from specific subfolders, without creating empty folders)

想象一下像这样的文件结构:

test ├── a │   ├── 0 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   ├── 1 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   ├── 2 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   └── 3 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt └── b ├── 0 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt ├── 1 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt ├── 2 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt └── 3 ├── file0.rtf ├── file0.txt ├── file1.rtf ├── file1.txt ├── file2.rtf └── file2.txt

不知道 a和b的真实姓名。 不过,如果它们在子文件夹0 ,我想从a和b子文件夹中复制所有*.txt文件(但是它们被命名)。

如果我使用它会有用

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* test/ test2/

但是,rsync创建空文件夹1 ,我想避免。 我该怎么做呢?

上面rsync命令的输出:

sending incremental file list a/ a/0/ a/0/file0.txt a/0/file1.txt a/0/file2.txt a/1/ a/2/ a/3/ b/ b/0/ b/0/file0.txt b/0/file1.txt b/0/file2.txt b/1/ b/2/ b/3/

我想要

sending incremental file list a/ a/0/ a/0/file0.txt a/0/file1.txt a/0/file2.txt b/ b/0/ b/0/file0.txt b/0/file1.txt b/0/file2.txt

Imagine a file structure like this:

test ├── a │   ├── 0 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   ├── 1 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   ├── 2 │   │   ├── file0.rtf │   │   ├── file0.txt │   │   ├── file1.rtf │   │   ├── file1.txt │   │   ├── file2.rtf │   │   └── file2.txt │   └── 3 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt └── b ├── 0 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt ├── 1 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt ├── 2 │   ├── file0.rtf │   ├── file0.txt │   ├── file1.rtf │   ├── file1.txt │   ├── file2.rtf │   └── file2.txt └── 3 ├── file0.rtf ├── file0.txt ├── file1.rtf ├── file1.txt ├── file2.rtf └── file2.txt

Where I do not know the real names of a and b. Nevertheless, I want to copy all *.txt files from subfolders of a and b (however they are named) if they are in the subfolder 0.

It kind of works if I am using

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* test/ test2/

However, rsync creates empty folders 1, 2, 3, which I would like to avoid. How do I do this?

Output from the rsync command above:

sending incremental file list a/ a/0/ a/0/file0.txt a/0/file1.txt a/0/file2.txt a/1/ a/2/ a/3/ b/ b/0/ b/0/file0.txt b/0/file1.txt b/0/file2.txt b/1/ b/2/ b/3/

I want

sending incremental file list a/ a/0/ a/0/file0.txt a/0/file1.txt a/0/file2.txt b/ b/0/ b/0/file0.txt b/0/file1.txt b/0/file2.txt

最满意答案

事实证明答案很简单。 只需添加--prune-empty-dirs标志即可。

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* --prune-empty-dirs test1/ test2/

这就是我想要的。

It turns out the answer is simple. Just adding the --prune-empty-dirs flag does the trick.

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* --prune-empty-dirs test1/ test2/

That is what I wanted.

更多推荐

本文发布于:2023-04-21 18:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/79ad3b836ca2984444ef420335f02cb0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   定子   而不   文件夹   夹中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!