linux - rsync 使用 shopt globstar 和 **/。 - 如何排除目录?

标签 linux bash ubuntu-12.04 rsync

我正在尝试将大型目录结构中的所有文件同步到单个根目录中(即不创建子目录但仍包括所有递归文件)。

环境:

  • Ubuntu 12.04 x86
  • RSYNC 版本 3.0.9
  • GNU bash 版本 4.2.25(1)

到目前为止,我已经从 bash 脚本调用了这个命令,它运行良好并提供了所需的基本核心功能:

shopt -s globstar
rsync -adv /path/to/source/**/. /path/to/dest/. --exclude-from=/myexcludefile

myexcludefile 的内容是:

filename
*/ 
# the */ prevents all of the directories appearing in /path/to/dest/

# other failed attempts have included:
directory1
directory1/
directory1/*

我现在需要排除位于源代码树中某些目录内的文件。但是,由于在所有目录中查找的 globstar 方法,rsync 无法匹配要排除的目录。换句话说,除了我的 /*filename 规则,其他所有内容都被完全忽略。

因此,我正在寻找有关排除语法的一些帮助,或者是否有另一种方法可以将许多目录的 rsync 实现到一个不使用我的 globstar 方法的目标目录。

如有任何帮助或建议,我们将不胜感激。

最佳答案

如果你想从 globstar 匹配中排除目录,你可以将它们保存到一个数组,然后根据文件过滤该数组的内容。

例子:

#!/bin/bash

shopt -s globstar

declare -A X
readarray -t XLIST < exclude_file.txt
for A in "${XLIST[@]}"; do
    X[$A]=.
done

DIRS=(/path/to/source/**/.)
for I in "${!DIRS[@]}"; do
    D=${DIRS[I]}
    [[ -n ${X[$D]} ]] && unset 'DIRS[I]'
done

rsync -adv "${DIRS[@]}" /path/to/dest/.

运行:

bash script.sh

请注意,exclude_file.txt 中的值应该真正匹配 /path/to/source/**/. 中的扩展值。

关于linux - rsync 使用 shopt globstar 和 **/。 - 如何排除目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18899888/

相关文章:

linux - 符号链接(symbolic link)检查 - Linux Bash 脚本

python - 来自导入 : GObject 的 undefined variable

c++ - 如果 numeric_limits<double>::has_infinity 为 false,建议的解决方法是什么?

不能让不同的编译器使用特定的头文件?

linux - 如何在ubuntu中安装npm install xbox-controller?

linux - Bash 将所有标准输出都输出到控制台和文件

Python GET 在浏览器中不起作用

c++ - 安装新的 g++ 后无法编译

linux - 解析行python

linux - 如何在 Linux 中显示 .xed 文件?