ddd ddd - 7 months ago 16
Bash Question

Copy files from multiple folders into one

I have the following folder structure:

>Folder1
>>date1
>>>Empty
1.jpg
2.jpg
>>>Occupied
x.jpg
y.jpg
>>date2
>>>Empty
1.jpg
2.jpg
>>>Occupied
x.jpg
y.jpg


I need to copy all the files


  • in the Empty folders to another single folder

  • in the Occupied folders to another single folder



I tried
$ cp Folder1/**/Empty/*.jpg ~/destination_folder/
but it fails giving me the error: "Argument list too long". How do I do this?

Answer

You can use find:

find Folder1 -wholename '*Empty*.jpg' -exec cp -v {} ~/destination_folder +

The + at the end of the -exec option will call cp as many times as needed to avoid the Argument list too long error but as less as possible. Meaning it will call cp always with the maximum number of arguments.

If your find should not support the + syntax, you can still use:

find Folder1 -wholename '*Empty*.jpg' -exec cp -v {} ~/destination_folder \;

which will call cp once per source file. While this will still work, it takes a bit more time.

Comments