I have folder named datahub and it has 15 folders and more than 20 subfolder which holds .jar files. I wanted to find all .jar files from subfolders and copy back to my current directory.

I am referring Bash - create zip by finding files but this is copying subfolders as well, but i wanted to copy only .jar's to my current directory or to create one directory and copy to it.

Thanks in Advance for your help Venkat

  • 3
    What happens if more than one file has the same name? Aug 7, 2019 at 9:37
  • All the .jar names are unique not same at all.
    – Venkat M
    Aug 7, 2019 at 9:39

2 Answers 2


Assuming all file names are unique, you can use something like (from datahub directory):

find . -name "*.jar" -exec cp {} /path/to/targetFolder \;

where /path/to/targetFolder is the directory where all jar files will be copied.

  • 1
    1 - Please warn that destination directory must not be a subdir of the find search directory -- 2 - In case of filenames with spaces... use cp '{}' ...
    – cmak.fr
    Aug 7, 2019 at 9:48
  • 2
    @cmak.fr quoting is not necessary for the case of whitespace - see How does 'find -exec' pass file names with spaces? Aug 7, 2019 at 9:57
  • 1
    @cmak.fr In addition to what steeldriver said, note that find doesn't see the difference between '{}' and {}. The shell removes the quotes before passing the argument to find (and the quotes are unnecessary). Aug 7, 2019 at 10:04
  • @steeldriver and eliah-kagan : thank you i learned something today
    – cmak.fr
    Aug 7, 2019 at 10:20
  • 1
    @cmak.fr if you want to go even further down the rabbit hole see GNU find and masking the {} for some shells - which? Aug 7, 2019 at 10:24

Open Terminal and navigate to your source directory.

find ./ -name "*.jar" -print0 | xargs -0 cp -t "Destination"

This only works if the file names are unique and are not already inside the Destination folder.

You must log in to answer this question.

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