0

We have almost 300 subdirectories in main directory. Now we want to copy all files from subdirectories in a single command to Destination directory. Is it possible?

Main Directory
     SubDirectory
        SubsubDirectory1
           xx.jpg
           xx,jpg
        SubsubDirectory2
           xx.jpg
           xx,jpg
     SubDirectory
        SubsubDirectory1
           xx.jpg
           xx,jpg
        SubsubDirectory2
           xx.jpg
           xx,jpg
     SubDirectory
        SubsubDirectory1
           xx.jpg
           xx,jpg
        SubsubDirectory2
           xx.jpg
           xx,jpg

I found below command can do work. But it is hard to mention each and every subdirectory name in the command.

cp "Main Directory"/*/Subsubdirectory1/* destdir
0

The find command is a good tool for that kind of operation, best in combination with xargs. Try something like:

find "Main Directory" -type f -print0 | xargs -0 cp -t destdir

and have a look at man find, man xargs and man cp for details on how this works.

| improve this answer | |
0

Like this, using in recursive mode (**) :

shopt -s globstar

cp -l **/*.jpg 'Main Directory'/

I use -l switch to avoid wasting disk space, it create hard links

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.