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
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.
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.