Search code examples
linuxbashsortingstat

Listing multiple files in a specific format in BASH


I have lots of files in a directory and I want to list all files in a specific format by creating date order (newer files first)

I can do this with stat and sort in the directory of files:

Commands:

cd /path 
stat -c '%.19y %n' * | sort -nr

Output: 2014-01-25 16:44:52 Filename1.txt 2014-01-24 16:34:17 Filename2.txt

It is fine. This is what I want exactly.

But when I try to run outside of the directory, command shows filenames with /path

Commands:

cd / 
stat -c '%.19y %n' /path/* | sort -nr

Output: 2014-01-25 16:44:52 /path/Filename1.txt 2014-01-24 16:34:17 /path/Filename2.txt

How can I list file names without path?


Solution

  • Provided you have GNU find, you can use that instead

    find /path -printf '%TY-%Tm-%Td %TH:%TM:%.2TS %f\n'
    
    2015-10-12 04:54:24 file