use*_*413 14 linux backup shell rsync regex
我正在尝试运行 rsync 以根据文件名模式(不区分大小写)沿路径递归复制一些文件。这是我为运行 rsync 所做的:
$ rsync -avvz --include ='*/' --include='.*[Nn][Aa][Mm][E].*' --exclude='*' ./a/ ./b/
Run Code Online (Sandbox Code Playgroud)
没有被复制,调试输出显示:
[sender] hiding file 1Name.txt because of pattern *
[sender] hiding file 1.txt because of pattern *
[sender] hiding file 2.txt because of pattern *
[sender] hiding file Name1.txt because of pattern *
[sender] hiding directory test1 because of pattern *
[sender] hiding file NaMe.txt because of pattern *
Run Code Online (Sandbox Code Playgroud)
我试过使用: --include='*[Nn][Aa][Mm][E]*'和其他组合,但还是不行。
关于如何使用正则表达式包含一些文件的任何想法?
spa*_*kie 10
我建议使用 rsync 的过滤器选项。对于您的示例,只需键入:
rsync -vam -f'+ *[Nn][Aa][Mm][E]*' -f'+ */' -f'- *' a b
Run Code Online (Sandbox Code Playgroud)
第一个过滤规则告诉 rsync 要包含哪些模式。需要第二条规则来告诉 rsync 检查其遍历的所有目录。为了防止包含空目录,它们被-m选项明确排除。最后一个过滤器规则告诉 rsync 处理到目前为止仍然不匹配的所有剩余模式。
rsync 不会说正则表达式。您可以使用 find 和 grep,尽管它有点神秘。查找目标文件:
find a/ |
grep -i 'name'
Run Code Online (Sandbox Code Playgroud)
但它们都以“a/”为前缀——这是有道理的,但我们最终想要的是一个 rsync 可接受的包含模式列表,因为“a/”前缀不适用于 rsync 我'将删除它:
find . |
grep -i 'name' |
cut -d / -f 2-
Run Code Online (Sandbox Code Playgroud)
还有一个问题——我们仍然会遗漏子目录中的文件,因为 rsync 不会搜索排除列表中的目录。我将使用 awk 将任何匹配文件的子目录添加到包含模式列表中:
find a/ |
grep -i 'name' |
cut -d / -f 2- |
awk -F/ '{print; while(/\//) {sub("/[^/]*$", ""); print}}'
Run Code Online (Sandbox Code Playgroud)
剩下的就是将列表发送到 rsync - 我们可以使用参数 --include-from=- 在标准输入上为 rsync 提供模式列表。所以,总而言之:
find a/ |
grep -i 'name' |
cut -d / -f 2- |
awk -F/ '{print; while(/\//) {sub("/[^/]*$", ""); print}}' |
rsync -avvz --include-from=- --exclude='*' ./a/ ./b/
Run Code Online (Sandbox Code Playgroud)
请注意,源目录“a”是通过两个不同的路径引用的——“a/”和“./a/”。这是微妙但重要的。为了使事情更加一致,我将进行最后一项更改,并始终将源目录称为“./a/”。但是,这意味着 cut 命令必须更改,因为 find 的结果前面会有一个额外的“./”:
find ./a/ |
grep -i 'name' |
cut -d / -f 3- |
awk -F/ '{print; while(/\//) {sub("/[^/]*$", ""); print}}' |
rsync -avvz --include-from=- --exclude='*' ./a/ ./b/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42532 次 |
| 最近记录: |