【问题标题】:Copy every file of entire directory structure into base path of another将整个目录结构的每个文件复制到另一个的基本路径中
【发布时间】:2016-03-08 10:10:40
【问题描述】:

我有一个目录树,里面有很多文件。我想将所有这些文件复制到一个新目录中,但所有文件都位于文件夹的底部。

所以我有这样的事情:

    images
    ├── avatar.png
    ├── bg.jpg
    ├── checkbox.png
    ├── cross.png
    ├── datatables
    │   ├── back_disabled.png
    │   ├── back_enabled.png
    │   ├── forward_disabled.png
    │   ├── forward_enabled.png
    │   ├── sort_asc.png
    │   ├── sort_asc_disabled.png
    │   ├── sort_both.png
    │   ├── sort_desc.png
    │   └── sort_desc_disabled.png
    ├── exclamation.png
    ├── forms
    │   ├── btn_left.gif
    │   ├── btn_right.gif
    │   ├── checkbox.gif
    │   ├── input
    │   │   ├── input_left-focus.gif
    │   │   ├── input_left-hover.gif
    │   │   ├── input_left.gif
    │   │   ├── input_right-focus.gif
    │   │   ├── input_right-hover.gif
    │   │   ├── input_right.gif
    │   │   ├── input_text_left.gif
    │   │   └── input_text_right.gif
    │   ├── radio.gif
    │   ├── select_left.gif
    │   ├── select_right.gif

我想要这样的东西:

    new_folder
    ├── avatar.png
    ├── bg.jpg
    ├── checkbox.png
    ├── cross.png
    ├── back_disabled.png
    ├── back_enabled.png
    ├── forward_disabled.png
    ├── forward_enabled.png
    ├── sort_asc.png
    ├── sort_asc_disabled.png
    ├── sort_both.png
    ├── sort_desc.png
    ├── sort_desc_disabled.png
    ├── exclamation.png
    ├── btn_left.gif
    ├── btn_right.gif
    ├── checkbox.gif
    ├── input_left-focus.gif
    ├── input_left-hover.gif
    ├── input_left.gif
    ├── input_right-focus.gif
    ├── input_right-hover.gif
    ├── input_right.gif
    ├── input_text_left.gif
    ├── input_text_right.gif
    ├── radio.gif
    ├── select_left.gif
    ├── select_right.gif

我很确定有一个 bash 命令,但我还没有找到它。你有什么想法吗?

CS

【问题讨论】:

    标签: linux bash find directory cp


    【解决方案1】:
    find /source-tree -type f -exec cp {} /target-dir \;
    

    【讨论】:

    • 这确实是最好的解决方案。您是否可以分解命令并解释哪个“操作员”做了什么? f.e 类型?会很酷!
    【解决方案2】:

    您正在寻找扁平化目录的方法

    find /images -iname '*.jpg' -exec cp --target-directory /newfolder/ {} \;
    

    find所有文件iname在不区分大小写的名称模式下。
    cp复制一次到--target-directory命名为/newfolder/。
    {}将列表从find展开到表单中/dir/file.jpg /dir/dir2/bla.jpg.

    【讨论】:

    • “cp:非法选项--”对我不起作用。 Kaz 的回答对我有用
    • @alexey 我无法重现你的错误,你是在什么环境下运行的?
    • 哦,你是对的 - 我使用的是 OSX (10.11.2) 内置的 bash,已知已过时:GNU bash,版本 3.2.57(1)-release (x86_64 -apple-darwin15)
    • 它也不适用于基于 alpine 的 docker,我得到以下cp: unrecognized option: target-directoryBusyBox v1.28.4 (2018-07-17 15:21:40 UTC) multi-call binary.
    【解决方案3】:

    在 zsh 上:

    cp /source/**/* /destination

    【讨论】:

      【解决方案4】:
      $ cd images && cp ** new_folder/
      

      【讨论】:

      • 此解决方案正确回答了问题。她和Zsh下的解决方案非常相似。那么,为什么 -1 ???
      • 它不会复制隐藏文件 BTW
      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 2012-02-15
      相关资源
      最近更新 更多