Bash: Sort files from 'find' by contents Bash: Sort files from 'find' by contents shell shell

Bash: Sort files from 'find' by contents


For completeness sake here are a few more ways of doing that:

  1. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' -exec cat {} \; | sort
  2. find -maxdepth 1 -type f -iname '*key*' -not -name '*~' | xargs cat | sort
  3. cat $(find -maxdepth 1 -type f -iname '*key*' -not -name '*~') | sort


If you would like to save the sorted output into a file, try:

find -maxdepth 1 -type f -iname '*key*'  -not -name '*~' | cat | sort > sorted.txt

otherwise just get rid of > sorted.txt and the sorted output will be printed to the terminal window.