Crash Override Crash Override - 9 months ago 45
Linux Question

Linux Shell Command: Find. How to Sort and Exec without using Pipes?

Linux command

with argument
does a GREAT job executing commands on files/folders regardless whether they contain spaces and special characters. For example:

find . -type f -exec md5sum {} \;

Works great to run
on each file in a directory tree, but executes in a random order. Find does not sort the results, and requires piping to
to get results in a more human-readable ordering. However, piping to
eliminates the benefits of

This does not work:

find . -type f | sort | md5sum

Because some filenames contain spaces and special characters.

Also does not work:

find . -type f | sort | sed 's/ /\\ /g' | md5sum

Still does not recognize spaces are part of the filename.

I suppose I can always sort the final result later, but wonder if someone knows an easy way to avoid that extra step by sorting within

Answer Source

With BSD find

A -s argument is available to request lexographic sort order.

find . -s -type f -exec md5sum -- '{}' +

With GNU find

Use NUL delimiters to allow filenames to be processed unambiguously. Assuming you have GNU tools:

find . -type f -print0 | sort -z | xargs -0 md5sum