The syntax for find
is:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
In your case -iname
and -type
are both expressions. So, there is no problem with using one before another.
From Description:
GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or),at which point find moves on to the next file name.
- For
find -type d -iname "*manifest*"
: It first test for directory only and then test the name matches with "*manifest*"
.
- For
find -iname "*manifest*" -type d
: It first test the name matches with "*manifest*"
and then test directory only.
And executing different orders may result in performance difference for a huge finding.
And in order to optimize, find
provides Optimization options as follows:
-Olevel
Enables query optimisation. The find program reorders tests to speed up execution while preserving
the overall effect; that is, predicates with side effects are not reordered relative to each other.
The optimisations performed at each optimisation level are as follows.
0 Equivalent to optimisation level 1.
1 This is the default optimisation level and corresponds to the traditional behaviour. Expres‐
sions are reordered so that tests based only on the names of files (for example -name and
-regex) are performed first.
2 Any -type or -xtype tests are performed after any tests based only on the names of files, but
before any tests that require information from the inode. On many modern versions of Unix, file
types are returned by readdir() and so these predicates are faster to evaluate than predicates
which need to stat the file first.
3 At this optimisation level, the full cost-based query optimiser is enabled. The order of tests
is modified so that cheap (i.e. fast) tests are performed first and more expensive ones are per‐
formed later, if necessary. Within each cost band, predicates are evaluated earlier or later
according to whether they are likely to succeed or not. For -o, predicates which are likely to
succeed are evaluated earlier, and for -a, predicates which are likely to fail are evaluated
earlier.
In order to analyse the optimization with your current command-line syntax, you can send it for debugging with -D
and get optimized command-line.
opt Prints diagnostic information relating to the optimisation of the expression tree;
Finally find -D opt -type d -iname "*manifest*"
outputs:
Optimized command line:
( -iname *manifest* [0.8] -a [0.4] [need type] -type d [0.4] )
-D opt
and-Olevel
– Hauke Laging Jan 29 '16 at 8:15find
result? – Hongxu Chen Jan 29 '16 at 8:37-type
and-iname
are test expressions – Pandya Jan 29 '16 at 8:38find
internally distinguish test orders? – Hongxu Chen Jan 29 '16 at 8:39gfind
is known to violate POSIX rules when it tries tooptimize
stat()
usage and (in case that the nonstandard primary-noleaf
was not specified) makes the illegal assumption that the number of hardlinks for a directory is2+number-of-subdirs.
It is however unlikely thatgfind
ignores the order of the rules, so it is expected that it helps to first specify the simple tests in a list. – schily Jan 29 '16 at 10:45