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.
Related videos on Youtube
![user1058398](https://i.stack.imgur.com/0bdQq.jpg?s=256&g=1)
Author by
user1058398
Updated on September 18, 2022Comments
-
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 over 10 yearsDo you want to copy
foo.jil
wherever it is withinA/
or do you want to only copyA/foo.jill
and notA/B/foo.jil
? -
user1058398 over 10 yearsI want to copy foo.jil wherever it's within A/ something like : find . -name "*.jil" -exec "cp"
-