Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 – RobotHumans Jul 19 '11 at 14:03
up vote 16 down vote accepted


find src/ -type f | grep -i so$ | xargs -i cp {} dst
share|improve this answer
good the +1, but I'm sure this question is a duplicate. Can't put my finger on the original at the moment though. – RobotHumans 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
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/ ';' – jáquer Jul 19 '11 at 15:44
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
Solution of @jaquer is much better because on MacOSX xargs utility does not have -i option. But his solution works like a charm. – bialix Dec 12 '12 at 13: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 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
@Stephan202 I had to use ... | xargs cp {} --parents -t dst. – warvariuc Nov 11 '15 at 7:08

You must log in to answer this question.

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