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

StackOverflow https://stackoverflow.com/questions/4909751

  •  29-10-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

Try this one:

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

OTHER TIPS

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

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.

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

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

try this:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top