I know that I can sort the output of ls
by time with the -t
option.
I know that when I have so many files that they don't fit in a single ls
invocation, I can normally use xargs
(or find ... -exec ... {} +
) to let ls
be called multiple times.
How can I combine the two? I have more files than fit on the command-line, and wish to list them sorted by time. find . -type f -exec ls -t {} +
doesn't work, because supposing exactly 1000 file names fit on the command-line, and 3000 files are present, this will run ls -t [first 1000 files]; ls -t [second 1000 files]; ls -t [last 1000 files]
, where the last 1000 files find
sees may well have a modification time before any of the first 1000. It doesn't seem like anything involving xargs
or equivalent has any chance whatsoever of working, it seems like that approach is fundamentally flawed, but I cannot find a way that does work.
Best Answer
ls -t
on its own will list all files in the current directory with that sorting, without ever needing to list them on the command line at all.If you need the recursion behaviour of
find
, or to do some other tests on the files, you can havefind
generate timestamped entries, either throughstat
or through GNUfind
's-printf
option and sort it. Something like:-printf '%T@ %p\0'
generates null-separated Unix timestamp (%T@
)-filename (%p
) pairs.sort -z
is also a non-standard GNU extension, which uses null-delimited records to be filename-safe. Thesort
option is supported in most of the BSDs too, but-printf
is GNU-only as far as I know.You can cut that output back into filenames only, or any other format you like.