Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I wanted to copy all *.so files from src to dst I'd do:

cp src/*.so dst

However, I want to copy all *.so files from src and it's subdirs into dst. Any clues?

share|improve this question
    
Seems like this question has been asked before...several times –  hbdgaf Jul 19 '11 at 14:03

3 Answers 3

up vote 10 down vote accepted

Try:

find src/ -type f | grep -i so$ | xargs -i cp {} dst
share|improve this answer
    
good solution...giving the +1, but I'm sure this question is a duplicate. Can't put my finger on the original at the moment though. –  hbdgaf Jul 19 '11 at 14:05
    
Brr. So there isn't an option in cp or something. Thanks! –  Albus Dumbledore Jul 19 '11 at 14:10
7  
find can do pattern matching and execute commands. There's no need to pipe its output: find src/ -type f -name '*.so' -exec cp '{}' dst/ ';' –  jaquer Jul 19 '11 at 15:44
1  
Yes, it can, however it's often easier to read a pipeline, also the pattern matching capabilities of grep far outweigh the simple shell patterns used in the -name parameter to find. xargs is also far more powerful than the -exec parameter to find. –  Mike Insch Jul 19 '11 at 15:51
    
Fair enough. It just seemed overkill for OP's question. –  jaquer Jul 20 '11 at 15:14

If you're using Bash, you can turn on the globstar shell option to match files and directories recursively:

shopt -s globstar
cp src/**/*.so dst
share|improve this answer
    
Nice. Exactly what I hoped for :-) Still, I'll need to check it first. Thanks! –  Albus Dumbledore Jul 19 '11 at 18:23

I tried the command line suggested by Mike:

find src/ -type f | grep -i so$ | xargs -i cp {} dst

but it ended up with dumping all the files into directory dst with their relative paths lost. To keep the relative paths the command needs to be modified to this:

find src/ -type f | grep -i so$ | xargs -i cp {} dst/{}

share|improve this answer
    
+1. However, this assumes that the directories corresponding to the relative source paths exist under dst. In case that's not so, use cp --parents. Also, be sure to quote the filenames. Thus: ... | xargs -i cp --parents "{}" dst (note that the last argument to cp is now "just" the destination directory). The command can be made even shorter (and probably faster) using the -t flag: ... | xargs cp --parents -t dst. –  Stephan202 Sep 15 '13 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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