Search code examples
linuxbashshellrsynccp

Recursive copy of a specific file type maintaining the file structure in Unix/Linux?


I need to copy all *.jar files from a directory maintaining the folder structure of the parent directories. How can I do it in UNIX/Linux terminal?
The command cp -r *.jar /destination_dir is not what I am looking for.


Solution

  • 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.