Recursive copy of specific files in Unix/Linux? [closed]
rsync
is useful for local file copying as well as between machines. This will do what you want:
rsync -avm --include='*.jar' -f 'hide,! */' . /destination_dir
The entire directory structure from . is copied to /destination_dir, but only the .jar files are copied. The -a ensures all permissions and times on files are unchanged. The -m will omit empty directories. -v is for verbose output.
For a dry run add a -n, it will tell you what it would do but not actually copy anything.
If you don't need the directory structure only the jar files, you can use:
shopt -s globstar
cp **/*.jar destination_dir
If you want the directory structure you can check cp
's --parents
option.
If your find has an -exec switch, and cp an -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 ";"
cp --parents `find -name \*.jar` destination/
from man cp
:
--parents
use full source file name under DIRECTORY
tar -cf - `find . -name "*.jar" -print` | ( cd /destination_dir && tar xBf - )