I've the following problem :

I have to write something to copy a file contained in directory A. The file is unique, and has a specific extension (.jil). Only problem, directory A can contains a sub-directory B (which can contain a sub-directory C, ...) and then my file won't be located in the directory A, but in one of his sub-directories.

What command can I use to find this file without exploring all my directoties ?

  • Do you want to copy foo.jil wherever it is within A/ or do you want to only copy A/foo.jill and not A/B/foo.jil? – terdon Nov 20 '13 at 14:28
  • I want to copy foo.jil wherever it's within A/ something like : find . -name "*.jil" -exec "cp" – user1058398 Nov 20 '13 at 14:30
  • That's how find works by default, see my answer. – terdon Nov 20 '13 at 14:34

That is actually the way find works by default. You could just run

find A/ -name "*.jil" -exec cp {} target_dir/ \;

The default behavior of find is to look into all subdirectories recursively, you can control this by setting the -maxdepth/-midepth options:

   -maxdepth levels
          Descend at most levels (a non-negative integer) levels of direc‐
          tories below the command line arguments.  -maxdepth 0
           means only apply the tests and  actions  to  the  command  line
   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.