Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "find ... -exec sort" and "find ... | sort"

Tags:

shell

What's the difference between these two commands?

find . -name "*.cpp" -exec sort \;
find . -name "*.cpp" |  sort
like image 362
znlyj Avatar asked Dec 04 '25 18:12

znlyj


1 Answers

The first command runs sort (without an argument) for every file which is found by the criteria specified. This is (as Mat has pointed out in his answer) is quite useless. The way to get sort to run for every file is to say find . -name "*.cpp" -exec sort {} \;. This would be like running sort a.cpp; sort b/c.cpp; ... for every file matched.

The second produces a list of .cpp files and then pipes the list through sort producing a sorted list of cpp files.

like image 119
Noufal Ibrahim Avatar answered Dec 06 '25 15:12

Noufal Ibrahim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!