98

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?

dan
  • 43,914
  • 47
  • 153
  • 254

8 Answers8

153

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.

Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
rashmi
  • 1,539
  • 2
  • 9
  • 3
  • 4
    Sorry, but I don't understand how come that this lists stuff in home directory? Flag "R" is for listing subdirectories recursively, I see no reference to home directory in ls man page. As I got it, it is listing subdirectories wherever you are. – Muhamed Huseinbašić Apr 18 '18 at 09:21
  • 2
    Good and direct answer :) – RPichioli May 27 '20 at 12:22
52

Try this one:

find . -type f -printf "%T@ %p\n" | sort -nr | cut -d\  -f2-
marco
  • 4,455
  • 1
  • 23
  • 20
  • 4
    Just FYI, this only works with GNU's find. Well, at least, the find on FreeBSD doesn't support the printf command. – Ricky Morse Nov 04 '14 at 19:36
  • 1
    Isn't `find . -type f -printf "%p\n"` enough? – Telmo Trooper Feb 26 '20 at 14:07
  • @TelmoTrooper not if you want the sorting. The extra character count goes into sorting by last modification time. If you do not care about this sorting, your version is enough. – detuur Nov 29 '21 at 15:08
25

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.

YoYo
  • 9,157
  • 8
  • 57
  • 74
Parakleta
  • 1,121
  • 10
  • 19
  • @YoYo Thank you for removing my redundant explanation line at the bottom. I attempted to submit an edit with the single-character change, but it was rejected by SO for being too small of a change. Do you know if there is a way around this? – Taylor D. Edmiston Sep 08 '16 at 18:50
8

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

Stunner
  • 12,025
  • 12
  • 86
  • 145
Bobby
  • 81
  • 1
  • 1
3
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

yankee
  • 38,872
  • 15
  • 103
  • 162
  • you missed to add the search domian: `find . -type f -print0 | xargs -0 ls -t` but then your solution works on mac os unlike the others listed here – d00d Nov 08 '16 at 15:59
2

try this:

ls -ltraR |egrep -v '\.$|\.\.|\.:|\.\/|total' |sed '/^$/d'
Andrew
  • 14,325
  • 4
  • 43
  • 64
wfg5475
  • 31
  • 1
1

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

Community
  • 1
  • 1
0

If its ok to use GUI, try nautilus . and then search from the search bar. By default, this search lists all the files in directory and subdirectories. However, sorting in reverse chronological order is not possible.

The_Learner
  • 589
  • 1
  • 6
  • 16