rsync 保留特定文件夹及其内容和结构,排除文件夹外的任何文件

rsync 保留特定文件夹及其内容和结构,排除文件夹外的任何文件

我的文件系统如下所示:

来源/

folder1/
  subfolder/
    contents
  other_subfolders
  file1
  file2
folder2/
  subfolder/
    contents
  other_subfolders
  file1
  file2
folder3/
  subfolder/
    contents
  other_subfolders
  file1
  file2
file1
file2

我想复制的几乎只有这个:

folder1/
  subfolder/
    contents
folder2/
  subfolder/
    contents
folder3/
  subfolder/
    contents

即我只想保留

  • 文件夹结构
  • 复制子文件夹及其内容
  • 忽略子文件夹外部的任何文件或子文件夹外部的任何其他文件夹

目前,我正在尝试:

$cd destination
$rsync -atvr --include="*/subfolder/" --exclude="*" source/ .

通过放入,--exclude="*"我希望排除除我放入 include 之外的所有内容,即*/subfolder/,一个名为 subfolder 的文件夹位于源中任何最高级别的文件夹内......

但是,不会复制任何文件。为什么?我缺少什么?

我也尝试过

$cd destination
$rsync -atvr --include="*/subfolder/" --exclude="*/*" source/ .

但这样做是这样的:

folder1/
  subfolder/
folder2/
  subfolder/
folder3/
  subfolder/
file1
file2

即,将文件保留在源(外部文件夹)中,创建所需的子文件夹,但不复制其中的任何内容。

编辑:应Siva的要求,放入后rsync -av --include='/' --include='/subfolder**' --exclude='*' source/ .

我得到:

访问系统:

1) 如果不使用 ssh-keys,请在密码提示处输入您的 TACC 密码 2) 在 TACC 令牌提示处,输入您的 6 位代码,后跟 。

Password:
TACC Token Code:
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LANG = "C.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
receiving incremental file list
./

sent 59 bytes  received 104 bytes  10.52 bytes/sec
total size is 3,242  speedup is 19.89

但我保证,所有这些警告与当前的问题无关,并且每次我尝试以任何方式(ssh、scp、rsync,...)访问服务器时都会弹出。

答案1

您可以通过使用相对路径指定要复制的源文件集来避免--include和的潜在复杂性--exclude

设想

我们在最顶层目录下创建示例目录和文件结构src。我们将把选择的内容复制到最顶层的目录dst

mkdir -p src/folder{1,2,3}/{subfolder,other_subfolders} dst
touch src/folder{1,2,3}/subfolder/contents src/{folder{1,2,3}/,}file{1,2}
find src | LC_ALL=C sort

rsync --dry-run -avR src/./folder*/*/* dst/

输出(从rsync

sending incremental file list
folder1/
folder1/subfolder/
folder1/subfolder/contents
folder2/
folder2/subfolder/
folder2/subfolder/contents
folder3/
folder3/subfolder/
folder3/subfolder/contents

标记/./源路径的其余部分将在目标处使用的点。删除--dry-run以使其执行所选操作。

答案2

来自男人:

如果模式包含 / (不计算尾随 /),则它将与完整路径名匹配,包括任何主要目录

尝试作为,

 rsync -av --include='/' --include='/subfolder**' --exclude='*' source/ destination/

相关内容