"ls" sorts wrongly if given large number of files

Brian Inglis Brian.Inglis@SystematicSw.ab.ca
Wed Jan 6 18:45:45 GMT 2021

On 2021-01-06 11:17, Kamran via Cygwin wrote:
> "ls" (version 8.26) sorts wrongly if given large number of files via "find" or 
> "xargs"
> For example:
> find -type f -exec ls -oS -h {} +
> OR
> find -type f -print0 | xargs -0r ls -oS -h
> Gives following data. Sorry for the long listing, and wrapped lines. But search 
> for "setup.ini" which is about 17 MB, it is sorted AFTER very small files.
> In fact it seams that sorting is restarted from that file (subsequent files are 
> again sorted). Note also that removing "-h" from "ls" command lines results in 
> the same problem.
> (output is trimmed to remove unnecessary data, i.e. perms/user/date-time, but 
> order is the same)

Shells and programs have limited command line lengths, so the target program may 
be run multiple times by find or xargs, more times when the file path names are 
long, as fewer file path names can fit in each command line, so output line 
lengths from ls and numbers of files sorted together will vary.
You can reduce this impact by changing to the closest common directory to your 
files, and using shell and ls options and features to get similar results e.g.

$ ( cd .../release/; ls -hoS **/*.{tar.*z*,ini}; )

Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in binary units and prefixes, physical quantities in SI.]

More information about the Cygwin mailing list