【问题标题】:finding and copying files with directory names查找和复制具有目录名称的文件
【发布时间】:2017-01-10 16:30:17
【问题描述】:

我有一个包含很多子目录的文件夹。我想找到html.txt 文件并将它们移动到具有相同结构的新目录中。 我正在使用

find . -name "*.html.txt" | cp -t /home/....

但它没有保留名称和目录结构。 有没有办法做到这一点?

【问题讨论】:

  • 您可以使用find . -name "*.html.txt" | cpio -pdm /home/where/ever。您可以使用find . -name "*.html.txt" | tar -cf - -T - | tar -xf - -C /home/where/ever(但它涉及两个tar 命令,比单个cpio 命令更笨拙)。毫无疑问,还有其他选择(rsync,...)。

标签: shell copy find command


【解决方案1】:

试试:

find . -name '*.html.txt' | xargs cp -t /home --parents

xargs 通常可用于将各种列表转换为参数列表,尤其是“find”的输出。您也可以使用 find 中的 exec 命令执行此操作,但我总是发现很难快速正确。

--parents 可以创建文件列表中的所有父目录。

我在 csh 中使用这种类型的命令,它应该也可以在 bash 中工作。

大部分已经在这里回答了: https://serverfault.com/questions/180853/how-to-copy-file-preserving-directory-path-in-linux

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多