Linux – Descending list ordered by file modification time

linuxopenwrt

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

Best Answer

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.

Related Question