Problem
All *.jar files in the directory and its subdirectories must be copied. What can I do in a UNIX/Linux terminal to do this? The command cp -r *.jar /destination dir fails.
Asked by Yury Pogrebnyak
Solution #1
rsync can be used to copy files locally as well as between machines. This will accomplish your objectives:
/destination dir rsync -avm —include=’*.jar’ -f ‘hide,! */’
Only the.jar files are copied, not the complete directory structure from. to /destination dir. The -a option ensures that all file permissions and timings are preserved. Empty folders will be skipped using the -m option. The -v option specifies verbose output.
Add a -n for a dry run; it will inform you what it will do but will not duplicate anything.
Answered by Sean
Solution #2
If you don’t need the directory structure only the jar files, you can use:
shopt -s globstar
cp **/*.jar destination_dir
If you wish to see the directory structure, use the —parents option of cp.
Answered by uzsolt
Solution #3
If your locate command has a -exec option and your copy command has a -t option:
find . -name "*.jar" -exec cp -t /destination_dir {} +
If you find doesn’t provide the “+” for parallel invocation, you can use “;” but then you can omit the -t:
find . -name "*.jar" -exec cp {} /destination_dir ";"
Answered by user unknown
Solution #4
cp --parents `find -name \*.jar` destination/
from man cp:
--parents
use full source file name under DIRECTORY
Answered by Bartosz Moczulski
Solution #5
tar -cf - `find . -name "*.jar" -print` | ( cd /destination_dir && tar xBf - )
Answered by Pavan Manjunath
Post is based on https://stackoverflow.com/questions/9622883/recursive-copy-of-specific-files-in-unix-linux