Linux How can I copy files of the same extension located in several subdirectories to a separate file?

I have a folder with many subdirectories, each with a *.nr file. There are 1000 subdirectories, each of which contains at least one *.nr file. Is there a quick way to copy all of these *.nr files into one directory?

I can write a quick python script to iterate over files, but this seems like overkill if there is a quick command line way to do this.

I work for Google, but I’m not sure which terms I should look for.

Thanks!

+4
source share
3 answers

sort of

 find /path/to/src -name "*.nr" -exec cp \{\} /path/to/dest \; 
+9
source

If you are on a system with GNU cp , this will do it faster:

find /path/to/src -name "*.nr" -print0 | xargs -0 cp -t /path/to/dest


Copy all .c files in my src directory:

 time find ~/src -name "*.c" -exec cp \{\} ~/src/Cfound/ \; . . . real 0m1.838s user 0m9.530s sys 0m1.110s time find . -name "*.c" -print0 | xargs -0 cp -t ./Cfound/ . . . real 0m0.057s user 0m0.010s sys 0m0.060s 
+4
source

The easiest way in bash:

 for F in */*.nr; do cp $F otherdir/ ; done 
0
source

Source: https://habr.com/ru/post/1469269/


All Articles