Linux 使用 Rsync 包含和排除选项按模式包含目录和文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9952000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Rsync include and exclude options to include directory and file by pattern
提问by Sheldon Chang
I'm having problems getting my rsync syntax right and I'm wondering if my scenario can actually be handled with rsync. First, I've confirmed that rsync is working just fine between my local host and my remote host. Doing a straight sync on a directory is successful.
我在正确使用 rsync 语法时遇到了问题,我想知道我的场景是否真的可以用 rsync 处理。首先,我已经确认 rsync 在我的本地主机和我的远程主机之间工作得很好。对目录进行直接同步是成功的。
Here's what my filesystem looks like:
这是我的文件系统的样子:
uploads/
1260000000/
file_11_00.jpg
file_11_01.jpg
file_12_00.jpg
1270000000/
file_11_00.jpg
file_11_01.jpg
file_12_00.jpg
1280000000/
file_11_00.jpg
file_11_01.jpg
file_12_00.jpg
What I want to do is run rsync only on files that begin with "file_11_" in the subdirectories and I want to be able to run just one rsync job to sync all of these files in the subdirectories.
我想要做的是仅在子目录中以“file_11_”开头的文件上运行 rsync,并且我希望能够仅运行一个 rsync 作业来同步子目录中的所有这些文件。
Here's the command that I'm trying:
这是我正在尝试的命令:
rsync -nrv --include="**/file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
rsync -nrv --include="**/file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
This results in 0 files being marked for transfer in my dry run. I've tried various other combinations of --include and --exclude statements, but either continued to get no results or got everything as if no include or exclude options were set.
这导致在我的试运行中标记了 0 个文件以进行传输。我已经尝试了 --include 和 --exclude 语句的各种其他组合,但要么继续没有结果,要么得到了一切,就好像没有设置包含或排除选项一样。
Anyone have any idea how to do this?
任何人都知道如何做到这一点?
采纳答案by ruakh
The problem is that --exclude="*"
says to exclude (for example) the 1260000000/
directory, so rsync
never examines the contents of that directory, so never notices that the directory contains files that would have been matched by your --include
.
问题是--exclude="*"
说要排除(例如)1260000000/
目录,所以rsync
永远不会检查该目录的内容,所以永远不会注意到该目录包含与您的--include
.
I think the closest thing to what you want is this:
我认为最接近你想要的是这个:
rsync -nrv --include="*/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
rsync -nrv --include="*/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
(which will include all directories, and all files matching file_11*.jpg
, but no other files), or maybethis:
(这将包括所有目录和所有匹配的文件file_11*.jpg
,但没有其他文件),或者可能是这样的:
rsync -nrv --include="/[0-9][0-9][0-9]0000000/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
rsync -nrv --include="/[0-9][0-9][0-9]0000000/" --include="file_11*.jpg" --exclude="*" /Storage/uploads/ /website/uploads/
(same concept, but much pickier about the directories it will include).
(相同的概念,但对它将包含的目录更加挑剔)。
回答by brandon
Add -m to the recommended answer above to prune empty directories.
将 -m 添加到上面的推荐答案以修剪空目录。
回答by AmirHossein
rsync
include exclude pattern examples:
rsync
包括排除模式示例:
"*" means everything
"dir1" transfers empty directory [dir1]
"dir*" transfers empty directories like: "dir1", "dir2", "dir3", etc...
"file*" transfers files whose names start with [file]
"dir**" transfers every path that starts with [dir] like "dir1/file.txt", "dir2/bar/ffaa.html", etc...
"dir***" same as above
"dir1/*" does nothing
"dir1/**" does nothing
"dir1/***" transfers [dir1] directory and all its contents like "dir1/file.txt", "dir1/fooo.sh", "dir1/fold/baar.py", etc...
And final note is that simply dont rely on asterisks that are used in the beginning for evaluating paths; like "**dir"
(its ok to use them for single folders or files but not paths) and note that more than two asterisks dont work for file names.
最后要注意的是,不要依赖一开始使用的星号来评估路径;喜欢"**dir"
(可以将它们用于单个文件夹或文件,但不能用于路径)并注意两个以上的星号不适用于文件名。