Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 219
csch Avatar asked Mar 21 '12 08:03

csch


People also ask

How do I copy all contents from one directory to another?

To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.

How do you copy and entire directory structure?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

What command do you use to copy files from all subdirectories to another directory?

To copy a directory with all subdirectories and files, use the cp command.

How do I copy a full path in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied.


3 Answers

find /source-tree -type f -exec cp {} /target-dir \; 
like image 147
Kaz Avatar answered Sep 24 '22 14:09

Kaz


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.

like image 25
cctan Avatar answered Sep 21 '22 14:09

cctan


On zsh:

cp /source/**/* /destination

like image 23
gbin Avatar answered Sep 25 '22 14:09

gbin