Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wonder why find does not behave in the same way in Solaris as in Cygwin or Linux.

I have a bunch of directories that have files called CS##########. Each # is a digits but there are always 10 digits after CS.

When using find in Cygwin the list comes out ordered: CS000000001 to CS00002345. When using find in Solaris the list come out completly unordered.

I wonder if this is actually OS-related, or if it is due to the implementation of find.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

find does not sort its output. The order of the file names depends on the order in which the directory entries appear in the filesystem. You can't control that, whether on Solaris or Windows.

On some filesystems, if you create a directory and add files to it, the files will appear in the order in which they are added. This is probably why they're sorted under Windows. But that isn't the case everywhere (I don't think it's ever the case on Solaris's zfs, for example), and if you start removing files in the directory and adding others, the order is unpredictable with most filesystems.

A few filesystems do have predictable , but most don't. Mac OS/OSX's HFS+ and Linux's Reiserfs sort file names in byte lexicographic order (note that the process's locale may have a different order). Linux's ext2/ext3/ext4 don't sort. It's not something you can count on.

If you need sorted output from find, pipe it into sort.

share|improve this answer
    
If I do >2, then >1, and run find on Debian with GNU findutils, I get 1 first, and then 2. Am I missing something? (I have never seen find returning unsorted results here) –  Teresa e Junior Mar 27 '13 at 1:16
    
Just tried the same commands on Haiku, and 2 comes before 1, but never on Debian... Haiku: GNU find 4.2.33, Debian: GNU find 4.4.2 –  Teresa e Junior Mar 27 '13 at 1:20
1  
@TeresaeJunior Indeed mkdir a; touch a/1; touch a/2; find a and mkdir a; touch a/2; touch a/1; find a seem to reliably list a/1 before a/2. I don't know why. But now try rm 1; touch 3 or mv 1 3. –  Gilles Mar 27 '13 at 1:23
    
Now it works :) I actually have some shell scripts that rely on find's "sorting" function. Time for changing them... –  Teresa e Junior Mar 27 '13 at 1:26
    
thanks a lot, I was believing it was more about OS live you just mentioned. So I guess it's time to change the script to provide a "%" on the running state. –  BitsOfNix Mar 27 '13 at 2:34

Your Answer

 
discard

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.