Does specifying -type option improve find performance in linux?

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?

Ali Ghanavatian

Posted 2017-09-20T18:19:21.770

Reputation: 101

Answers

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.

Kamil Maciorowski

Posted 2017-09-20T18:19:21.770

Reputation: 38 429

(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. – G-Man Says 'Reinstate Monica' – 2017-09-20T20:24:24.970

1@G-Man Oops. I meant test; in my console I run test. Mindless mistake here. Should be OK now. – Kamil Maciorowski – 2017-09-20T20:45:34.657

@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.

– Ali Ghanavatian – 2017-09-21T06:07:20.390

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. – Kamil Maciorowski – 2017-09-21T06:16:40.853

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.

G-Man Says 'Reinstate Monica'

Posted 2017-09-20T18:19:21.770

Reputation: 6 509

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 – 2017-09-20T20:11:39.450

I’m not sure what you’re saying. Are you saying the same thing Kamil Maciorowski is saying?

– G-Man Says 'Reinstate Monica' – 2017-09-20T20:28:04.893

Yes, that's the same ting. – xenoid – 2017-09-20T21:36:25.877

You can omit the starting directory, at least in GNU find. Man page says If no starting-point is specified, `.' is assumed. – sebasth – 2017-09-21T06:42:18.130