How do I list all the files in a directory and subdirectories in reverse chronological order?

UnixFindLs

Unix Problem Overview


I want to do something like ls -t but also have the files in subdirectories included. But the problem is that I don't want the output formated like ls -R does, which is like this:

[test]$ ls -Rt
b       testdir test

./testdir:
a

I want it to be formatted like the find command displays files in subdirectories. I.e:

[test]$ find .
.
./b
./test
./testdir
./testdir/a

But what find doesn't seem to do is order the result chronologically by last update time.

So how can I list all the files in a directory and subdirectories, in the format that find does, but in reverse chronological order?

Unix Solutions


Solution 1 - Unix

ls -lR is to display all files, directories and sub directories of the current directory ls -lR | more is used to show all the files in a flow.

Solution 2 - Unix

Try this one:

find . -type f -printf "%T@ %p\n" | sort -nr | cut -d\  -f2-

Solution 3 - Unix

If the number of files you want to view fits within the maximum argument limit you can use globbing to get what you want, with recursion if you have globstar support.

For exactly 2 layers deep use: ls -d * */*

With globstar, for recursion use: ls -d **/*

The -d argument to ls tells it not to recurse directories passed as arguments (since you are using the shell globbing to do the recursion). This prevents ls using its recursion formatting.

Solution 4 - Unix

Try find . -type d or find . -type d -ls

Solution 5 - Unix

find -type f -print0 | xargs -0 ls -t

Drawback: Works only to a certain amount of files. If you have extremly large amounts of files you need something more complicated

Solution 6 - Unix

try this:

ls -ltraR |egrep -v '\.$|\.\.|\.:|\.\/|total' |sed '/^$/d'

Solution 7 - Unix

The command in wfg5475's answer is working properly, just need to add one thing to show only files in a directory & sub directory:

ls -ltraR |egrep -v '\.$|\.\.|\.:|\.\/|total|^d' |sed '/^$/d'

Added one thing: ^d to ignore the all directories from the listing outputs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanView Question on Stackoverflow
Solution 1 - UnixrashmiView Answer on Stackoverflow
Solution 2 - UnixmarcoView Answer on Stackoverflow
Solution 3 - UnixParakletaView Answer on Stackoverflow
Solution 4 - UnixBobbyView Answer on Stackoverflow
Solution 5 - UnixyankeeView Answer on Stackoverflow
Solution 6 - Unixwfg5475View Answer on Stackoverflow
Solution 7 - UnixPankaj TiwariView Answer on Stackoverflow