Descending list ordered by file modification time

3

1

How can I generate a list of files in a directory [for example, "/mnt/hdd/PUB/"] ordered by the files modification time? [in descending order, the oldest modified file is at the lists end]

ls -A -lRt would be great: https://pastebin.com/raw.php?i=AzuSVmrJ

But if a file is changed in a directory, it lists the full directory, so the pastebined link isn't good [I don't want a list ordered by "directories", I need a "per file" ordered list]

OS: OpenWrt [no Perl -> not enough space for it :( + no "stat", or "file" command].

LanceBaynes

Posted 2011-02-28T15:38:01.243

Reputation: 3 510

Does your find have -printf? By the way, don't forget to mark answers as accepted when you can and upvote as often as you can. – Paused until further notice. – 2011-02-28T16:42:00.623

Answers

1

Use find and sort:

find YOUR_START_DIR -type f -print0 |
    xargs -r -0 ls -l | 
        sort -k 6.2,6.5nr \
             -k 6.7,6.8nr \
             -k 6.10,6.11nr \
             -k 7.2,7.3nr \
             -k 7.5,7.6nr

the long list of k options after sort define the year, month, day, hour and minute as sort keys and order by them in that order.

Files saved on the same minute won't get ordered. If you want to go down to seconds and more, add "--full-time" to the ls command, and add new keys at the end of the sort command.

rems

Posted 2011-02-28T15:38:01.243

Reputation: 1 850

This is dependent (as is the OP's pipeline) on the format of the output of ls. – Paused until further notice. – 2011-02-28T16:41:40.423

Sure, completely dependant on the ls output format. – rems – 2011-02-28T16:45:01.403

0

Here's an ugly answer that partially works for me in cygwin:

 ls -A -lRt --full-time | sort | uniq | grep -v '^total\|:$'
  • Recursively lists files, displays full timestamps for every file
  • sorts them
  • discards duplicates
  • Greps out a few non-file entries such as "total files in directory X" and the header rows for files you're recursing into.

Daniel J. Pritchett

Posted 2011-02-28T15:38:01.243

Reputation: 148

sort -u instead of sort | uniq – Paused until further notice. – 2011-02-28T16:39:55.650

1This doesn't work for me. Doing it this way sort will order alphabetically from the start of the line. – rems – 2011-02-28T16:46:39.297

@rems Quite so... I spent another hour or two tweaking it and learned a bit about regexes and sed (and sort -k) along the way. Never did get a perfect solution though. – Daniel J. Pritchett – 2011-02-28T22:36:00.297

0

"Changing" the file in the directory may well update the directory's modification time. But if you are not interested in the directories, grep them out:

ls -AlRt | egrep -v '^d'

mpez0

Posted 2011-02-28T15:38:01.243

Reputation: 2 578