bash rsync: --include-from 与 --exclude-from 实际区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19296190/
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
rsync: --include-from vs. --exclude-from what is the actual difference?
提问by Craig
In the documentation, it mentions these as being files containing lists of either patterns to include or patterns to exclude. However, that implies for inclusions, everything is considered an exclusion except where things match patterns. So for example, an include file containing:
在文档中,它提到这些是包含要包含的模式或要排除的模式列表的文件。但是,这意味着对于包含项,除了匹配模式的内容外,所有内容都被视为排除项。例如,一个包含文件:
/opt/**.cfg
Should only include any file named *.cfg that exists anywhere under a directory named opt
any where in the tree. So it would match the following:
应该只包含任何名为 *.cfg 的文件,该文件存在opt
于树中名为any where的目录下的任何位置。所以它将匹配以下内容:
/opt/etc/myfile.cfg
/some/dir/opt/myfile.cfg
/notopt/opt/some/other/dir/myfile.cfg
I'd therefore expect it to implicitly exclude anything else. But that doesn't seem to be the case, since I am seeing this in the itemized output:
因此,我希望它隐含地排除其他任何东西。但情况似乎并非如此,因为我在逐项输出中看到了这一点:
*deleting etc/rc.d/init.d/somescript
So what is the deal with --include-from
and --exclude-from
? Are they just aliases for --filter-from
?
那么与--include-from
and 有--exclude-from
什么关系呢?他们只是别名--filter-from
吗?
回答by Hari Menon
rsync
doesn't work like that. Any file with a filename pattern that does not match any of the include or exclude patterns are considered to be included. In other words, think of the include pattern as a way of overriding exclude pattern.
rsync
不是那样工作的。任何文件名模式与任何包含或排除模式都不匹配的文件都被视为包含在内。换句话说,将包含模式视为覆盖排除模式的一种方式。
From the docs:
从文档:
Rsync builds an ordered list of include/exclude options as specified on the command line. Rsync checks each file and directory name against each exclude/include pattern in turn. The first matching pattern is acted on. If it is an exclude pattern, then that file is skipped. If it is an include pattern then that filename is not skipped. If no matching include/exclude pattern is found then the filename is not skipped.
Rsync 构建命令行中指定的包含/排除选项的有序列表。Rsync 依次根据每个排除/包含模式检查每个文件和目录名称。作用于第一个匹配模式。如果是排除模式,则跳过该文件。如果它是包含模式,则不会跳过该文件名。如果未找到匹配的包含/排除模式,则不会跳过文件名。
(Emphasis mine)
(强调我的)
So, if you want to include only specific files, you first need to includethose specific files, then excludeall other files:
因此,如果您只想包含特定文件,则首先需要包含这些特定文件,然后排除所有其他文件:
--include="*/" --include="*.cfg" --exclude="*"
Couple of things to note here:
这里有几点需要注意:
The
include
patterns have to come before the excludes, because the first pattern that matches is the one that gets considered. If the file name matches the exclude pattern first, it gets excluded.You need to either include all subdirectories individually, like
--include="/opt" --include="/opt/dir1"
etc. for all subdirectories, or use --include="*/" to include all directories (not files). I went with the second option for brevity.
该
include
模式要来的前排除,因为第一个匹配的模式是被考虑的一个。如果文件名首先与排除模式匹配,则会被排除。您需要单独包含所有子目录,例如
--include="/opt" --include="/opt/dir1"
所有子目录的等,或者使用 --include="*/" 来包含所有目录(不是文件)。为简洁起见,我选择了第二个选项。
It is quirky and not very intuitive. So read the docscarefully (the "EXCLUDE PATTERNS" section in the link) and use the --dry-run
or -n
option to make sure it is going to do what you think it should do.
它很古怪而且不是很直观。所以请仔细阅读文档(链接中的“排除模式”部分)并使用--dry-run
or-n
选项来确保它会做你认为应该做的事情。
回答by UlfR
If you (like me) have a hard time to wrap your head around the FILTER RULES
-section in the man-pages but have a basic understanding of find
, you could use that instead.
如果您(像我一样)很难FILTER RULES
理解手册页中的 - 部分,但对 有基本的了解find
,则可以改用它。
Say you whant to sync everyting with a specific date (ex 2016-02-01
) in either the file-name or in a directory-name from /storage/data
to rsync_test
. Do something like this:
假设你whant到同步everyting与某一特定日期(例如2016-02-01
无论是在文件名或从一个目录名)/storage/data
来rsync_test
。做这样的事情:
cd /storage/data
find . -name '*2016-02-01*' \
| rsync --dry-run -arv --files-from=- /storage/data /tmp/rsync_test