【问题标题】:Linux cp command to preserve file strucutreLinux cp 命令保存文件结构
【发布时间】:2019-08-14 18:09:02
【问题描述】:

所以我有一个这样的 linux 文件结构:

/base/dir_a/a/this_d=1_another/testfile
/base/dir_a/a/this_d=2_another/testfile
/base/dir_a/a/aa/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=2_another/testfile

我想将所有包含d=1 的文件夹复制到dir_b

/base/dir_a/*d=1* -> /base/dir_b

所以最后的dir_b应该是:

/base/dir_a/a/this_d=1_another/testfile
/base/dir_a/a/aa/this_d=1_another/testfile
/base/dir_a/a/ab/this_d=1_another/testfile

我尝试了类似以下的副本(2 个命令..)

cp /base/dir_a/a/*/*d=1* -> /base/dir_b;
cp /base/dir_a/a/*d=1* -> /base/dir_b;

但这会尝试将this_d=1_another 文件夹复制到/base/dir_b,这会导致问题,因为我所有的嵌套文件夹都命名相同。

我该如何解决这个问题?

【问题讨论】:

    标签: linux shell sh


    【解决方案1】:

    如果您可以使用 rsync,那么使用它是最简单的:

    rsync -avP --include=*/ --include=**/*d=1* --exclude='*' /base/dir_a /base/dir_b
    

    但是,这将创建一堆空目录作为副作用。

    【讨论】:

    • 无法访问 rsync。我正在使用hadoop distcp,它本质上模仿了cp,这就是为什么我询问cp,因为它会比hadoop distcp获得更多的观众。
    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多