Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
3  
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

2 Answers 2

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. –  130490868091234 Apr 17 '13 at 10:29

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.