Recursive copy of specific files in Unix/Linux? [closed] Recursive copy of specific files in Unix/Linux? [closed] linux linux

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 globstarcp **/*.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 ";"