How can I run the ls command with all its options and list all files in directories recursively and sort it based on last modification times?

ie ls -lth on all files in directories recursively

link|improve this question

53% accept rate
feedback

2 Answers

First, which flavor of unix are you using? Systems based on BSD will have a different option set for ls from System V.

Second, http://unixhelp.ed.ac.uk/CGI/man-cgi?ls lists a -R switch to force a recursive listing, and -c to base its sort on ctime, or modification time.

The trick is to verify that your flavor of *nix uses a version of ls that includes those switches.

link|improve this answer
feedback

It sounds like you want a global sort, rather than a per-directory sort like you would get with ls -Rlth.

If the total number of files is not too large, this can be accomplished using the find command to gather the names and pass them to ls for sorting:

find . -type f -exec ls -lth {} +

or alternatively

find . -type f -print0 | xargs -0 ls -lth

(On some older systems, the find command does not support either -exec ... + or -print0. In that case you could use -print instead of -print0 and omit -0, but then it will not work with file names containing spaces or other special characters.)

Caveat: If the number of files that you are sorting is large then there may be too many to list as arguments to a single ls command. In that case ls will be called multiple times and the sorting will only be correct within each group of files.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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