csch
csch

Reputation: 4846

Copy every file of entire directory structure into base path of another

I have a directory-tree with a lot of files in it. I'd like to copy all of those files into one new directory, but with all files located in the base of the folder.

So I have something like this:

    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

And I'd like something like this:

    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

I'm pretty sure there is a bashcommand for that, but I haven't found it yet. Do you have any ideas?

CS

Upvotes: 22

Views: 13828

Answers (4)

cctan
cctan

Reputation: 2023

you are looking for ways to flatten the directory

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

find all files iname in case insensitive name mode.
cp copy once to --target-directory named /newfolder/.
{} expand the list from find into the form of /dir/file.jpg /dir/dir2/bla.jpg.

Upvotes: 13

gbin
gbin

Reputation: 3000

On zsh:

cp /source/**/* /destination

Upvotes: 2

Grégory Roche
Grégory Roche

Reputation: 172

$ cd images && cp ** new_folder/

Upvotes: 0

Kaz
Kaz

Reputation: 58666

find /source-tree -type f -exec cp {} /target-dir \;

Upvotes: 34

Related Questions