Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am querying specific directories to see if I find files with a specific pattern in them and I have been trying it with two different commands:

Standard find command:

find -L $unix_dir\*/ -maxdepth $maxdepth -name \"$filepattern\"

and a tree and grep command replacement:

tree -L $maxdepth -l -i -f $unix_dir*/ | grep '$filepattern'

Is the second one supposed to be faster for some reason? I find it's sometimes faster but sometimes just as fast as the other command, and I am unsure what's the theory behind both. Any ideas?

share|improve this question
Are you first trying the find command then immediately executing the tree | grep command? If so, the directory structure is probably being cached in memory while being parsed by find. – Rain Apr 18 '13 at 5:33

I'd say the second one is slower as it involves two processes tree and grep while the first one has only one process find.

Additionally, tree writes the names of all files it finds to its output stream which is then consumed by grep. At the same time, find prints only the names of matching files.

When using find, it tries to match only the file name to your specified $filepattern. In the second case, the pattern is applied to the full path of the file. Thus, the second case has more data to process.

On the other hand, the bottle neck in this test is disk IO. Both find and tree traverse the directories.
If you run each test one after another, the OS will cache the data about the directories and file names, therefore command will complete faster.

share|improve this answer

The command find write more characters into the screen because it writes always the full path to the file. Also, the comparation with a filepattern can be longer in the find command (depends on the implementation). E.g, with pseudocode:

  • find

    • Extract filename
    • Compare filename with filepattern
  • tree

    • Compare filename with filepattern
share|improve this answer
I would expect the writing of the fullpath is not an issue, because the tree options I am using to the same. – 719016 Apr 17 '13 at 10:29

You must log in to answer this question.

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