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

2 Answers 2

1

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.

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

1
  • Yes, post edited accordingly Commented Jun 7, 2020 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .