linuxbashfindls

Sort a find command by date


I have a script:

newerthan="2016-02-08"
olderthan="2016-04-29"
find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -ls

This list files:

16481    0 -r--r--r--   1 root     root         4096 Mar 16 11:41 /sys/module/sunrpc/srcversion
 16482    0 -r--r--r--   1 root     root         4096 Mar 13 04:42 /sys/module/sunrpc/initstate
 16483    0 -r--r--r--   1 root     root         4096 Mar 16 11:41 /sys/module/sunrpc/refcnt
 16485    0 -r--r--r--   1 root     root         4096 Mar 17 11:41 /sys/module/sunrpc/sections/.note.gnu.build-id
 16486    0 -r--r--r--   1 root     root         4096 Mar 12 11:41 /sys/module/sunrpc/sections/.text

Is it possible to sort by date, the result ?


Solution

  • If you want to print the filenames along with the date in sorted order:

    find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -printf "%T+\t%p\n" | sort
    

    If you want to print only the filenames in sorted order:

    find / -type f -name "*" -newermt "$newerthan" ! -newermt "$olderthan" -printf "%T+\t%p\n" | sort | awk '{print $2}'