Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to be able to alphabetically sort the output of find before piping it to a command. Entering | sort | between didn't work, so what could I do?

find folder1 folder2 -name "*.txt" -print0 | xargs -0 myCommand
share|improve this question
up vote 9 down vote accepted

Leave the newlines in (just a standard -print), then sort, then remove the newlines:

find | sort | sed ':a;N;$!ba;s/\n/ /g' | xargs -0 yourcommand
share|improve this answer
One of the reasons for using -print0 is in case of embedded whitespace. Converting the newlines to spaces will just compound the problem. – Arcege Mar 16 '12 at 12:02
That cryptic sed command is explained in this SO question: Replaces a newline with whitespace. Should replace with \0, and for that purpose you would be better off with tr \\n \\000. – Javier Aug 10 '15 at 19:05

Some versions of sort have a -z option, which allows for null-terminated records.

find folder1 folder2 -name "*.txt" -print0 | sort -z | xargs -r0 myCommand

Additionally, you could also write a high-level script to do it:

find folder1 folder2 -name "*.txt" -print0 | python -c 'import sys; sys.stdout.write("\0".join(sorted(sys.stdin.read().split("\0"))))' | xargs -r0 myCommand

Add the -r option to xargs to make sure that myCommand is called with an argument.

share|improve this answer
Good one (two?)... Interestingly, though, the two methods handle . differently... With sort it winds up at the end of the list... with python it sorts to the top. (maybe python sorts with LC_COLLATE=C) – Peter.O Mar 16 '12 at 14:45
There is also the -t \0 option for sort (which is a -z synonym) – Javier Aug 10 '15 at 18:44
The problem with all these |sort solutions is that you cannot use -exec any longer. OK, although it is possible to rewrite your statement given to -exec so that it works with xargs, the question is, what about "mini-scripts"? (sh -c ...) I wouldn't call that trivial to transform a 'sh -c' mini-script with multiple commands so that it can work with xargs (if possible at all, that is) – syntaxerror Nov 20 '15 at 19:57

I think you need the -n flag for sort#

According to man sort:

-n, --numeric-sort
    compare according to string numerical value


The print0 may have something to do with this, I just tested this. Take the print0 out, you can null terminate the string in sort using the -z flag

share|improve this answer
Well, that print0 appears to be space-separating the filenames which is what I need to pass to my command, unfortunately – Industrial Mar 16 '12 at 10:46

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

find folder1 folder2 -name "*.txt" -print | sort | parallel myCommand

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

share|improve this answer
What is the justification for using GNU Parallel? To speed it up? – Peter Mortensen Sep 28 '14 at 0:18
That and you do not need to mess with \0 separated records. – Ole Tange Sep 28 '14 at 16:46

The sort command allows to use the null as tabulation character via -t \0 (as well as the -z proposed by Arcege). Therefore:

find folder1 folder2 -name "*.txt" -print0 | sort -t \0 | xargs -0 myCommand

Using \n as separator produces a better readable intermediate outputs, but as stated by Ole Tangue reply, it will make it harder to handle with xargs. However it is not that hard, as you can use -d "\n" in order to specify the delimiter:

find folder1 folder2 -name "*.txt" | sort | xargs -d "\n" myCommand
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.