Understand the importance of -depth option in find command.

Hi All,

Can you please help me in understanding the importance of -depth of find.

I am trying to execute below code.

find . -mtime +5  -name  "*"  -depth -exec ls -l {} \;

But it is throwing below error.

find: warning: you have specified the -depth option after a non-option argument -mtime, but options are not positional
(-depth affects tests specified before it as well as those specified after it). 
Please specify options before other arguments.

From the error I can understand that there is problem with the position of - depth

But I just want to know the concept in this.

Kindly help me with your suggestions.

It is not an error it is just a warning. Probably it will go away if you specify

find . -depth -mtime ....
1 Like

Referring to the find manpage, -depth is an option while -mtime and -name are tests.
This section (also from the find manpage) describes the "problem with the position of -depth" pretty good, imho. Hope this helps.

   OPTIONS
       All options always return true.   Except  for  -daystart,  -follow  and
       -regextype,  the  options  affect  all tests, including tests specified
       before the option.  This is because the options are processed when  the
       command  line  is parsed, while the tests don't do anything until files
       are examined.  The -daystart, -follow and -regextype options  are  dif
       ferent  in  this respect, and have an effect only on tests which appear
       later in the command line.  Therefore, for clarity, it is best to place
       them  at  the  beginning of the expression.  A warning is issued if you
       don't do this.
3 Likes