我正在尝试将一个大目录结构中的所有文件同步到一个根目录(即不创建子目录,但仍包括所有递归文件)。
环境:
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/**/.中的扩展值真正匹配。

09-10 00:12
查看更多