Finding a specific file in several sub-directories

8,172

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
          arguments.
   -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.
Share:
8,172

Related videos on Youtube

user1058398
Author by

user1058398

Updated on September 18, 2022

Comments

  • user1058398
    user1058398 almost 2 years

    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 ?

    • terdon
      terdon over 10 years
      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?
    • user1058398
      user1058398 over 10 years
      I want to copy foo.jil wherever it's within A/ something like : find . -name "*.jil" -exec "cp"