Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - Copy files from directory

Tags:

bash

copy

I have a folder which contains some folders:

main
  \_ Dir 1
  \_ Dir 2
  \_ ...
  \_ Dir 40

I need to open each sub-folder, copy all the files and paste them in another folder, the same folder for all this sub-folder.

How can I do that in a smart way?

The only thing that comes to my mind was create a list with the name of all folders and then use a simple script to open, copy and paste but I'm sure that there is a faster way than write all the names.

like image 425
Christian Giupponi Avatar asked Aug 31 '15 07:08

Christian Giupponi


People also ask

How do I copy files from one directory to another in bash?

Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ). For example, you can copy grades. txt from the home directory to documents .

How do I copy a file in Linux bash?

The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy. Then, state the location at which the new file should appear. The new file does not need to have the same name as the one you are copying.


1 Answers

Try:

cp main/*/* /path/to/otherfolder/

If you want to be warned before overwriting a file, use the -i option:

cp -i main/*/* /path/to/otherfolder/
like image 157
John1024 Avatar answered Oct 16 '22 04:10

John1024