Skip to content
Advertisement

How to pipe a list of files returned from find to cat and sort them

I’m trying to find all the files from a folder and then print them but sorted.

I have this so far

find . -type f -exec cat {} ;

and it print’s all files but I need to sort them too but when I do

find . -type f -exec sort cat {};

I get the next error

sort:cannot read:cat:No such file or directory

and if I switch sort and cat like this

find . -type f -exec cat sort {} ;

I get the same error the it print’s the file(I have only one file to print)

Advertisement

Answer

It’s not clear to me if you want to display the contents of the files unchanged sorting the files by name, or if you want to sort the contents of each file. If the latter:

find . -type f -exec sort {} ; 

If the former, use bsd find’s -s option:

find -s . -type f -exec cat {} ; 

If you don’t have bsd find, use:

find . -type f -print0 | sort -z | xargs -0 cat
Advertisement