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.