0

As you know there is an option for find command which allows you to tell if you're looking for a 'file' or 'directory'. I was wondering if specifying that would improve find performance (speed)? like:

find / -type file -name foo.bar

is faster than

find / -name foo.bar

or it does not make any difference?

2 Answers 2

3

Yes, in some cases it does improve performance.

By default find applies logical AND to its tests but the latter test is not evaluated if the former is false. So if you add a super fast test which almost always fails at the very beginning then it can save many executions of following (say: slow) tests and in this case this additional test does improve performance.

Go to directory with many files and few symlinks. Compare

time find -exec test -L {} \; -print

to

time find -type l -exec test -L {} \; -print

This is artificial yet enlightening example. The two tests (-type l and -exec test -L {} \;) do the same job, the two find commands yield the same results. However -exec creates additional process and this is relatively slow. In my home directory the first command takes two minutes; the second command takes six seconds.

4
  • (1) Good point.  I (obviously) didn’t consider the case of -type being followed by slower tests (since the question didn’t suggest that that was going to happen, but I should have thought of it anyway).  (2) What is this type program that you’re running?  I get find: ‘type’: No such file or directory. Sep 20, 2017 at 20:24
  • 1
    @G-Man Oops. I meant test; in my console I run test. Mindless mistake here. Should be OK now. Sep 20, 2017 at 20:45
  • @KamilMaciorowski I don't know about the internal mechanism. honestly, I hoped it uses file table or other file system meta-data to speed up the search process but looks like it just traverse the directory structure as Jim Dennis mentioned here. Sep 21, 2017 at 6:07
  • 1
    @AliQanavatian What is your point? All I'm saying is: if the first test fails then the rest is not evaluated at all; so you can speed things up sometimes thanks to additional test, even if it doesn't change the final output. Sep 21, 2017 at 6:16
2
  1. Your commands won’t even work, for two reasons:

    • The starting directory (or directories), if any, must always appear before the test(s), if any, so your commands would have to be

      find / -name foo.bar …
      
    • Generally you can’t say -type file; it should be -type f.

  2. Why would you even suspect that adding an additional test would improve performance?
  3. Theoretically, it could decrease performance, because you’re doing

    if (filename = "foo.bar"  AND  filetype = "f")
        print filename
    

    instead of

    if (filename = "foo.bar")
        print filename
    

    i.e., you’re giving it more work to do.  In reality, though, the difference will be negligible.  find already has to get the filetype information to see which entries are directories (and therefore need to be searched recursively).  Once it has that information, checking whether it is a plain file requires just a few CPU instructions, and is very fast.

4
  • The only case I can think of is a -exec test that involves an external process (and this process just fails silently if passed the wrong type anyway).
    – xenoid
    Sep 20, 2017 at 20:11
  • I’m not sure what you’re saying. Are you saying the same thing Kamil Maciorowski is saying? Sep 20, 2017 at 20:28
  • Yes, that's the same ting.
    – xenoid
    Sep 20, 2017 at 21:36
  • You can omit the starting directory, at least in GNU find. Man page says If no starting-point is specified, `.' is assumed.
    – sebasth
    Sep 21, 2017 at 6:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .