6

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?

719016
  • 4,683

2 Answers2

1

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.

0

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
Zhen
  • 903