ls output sorted by "date added"
Alec Jacobson
August 11, 2013
Mac keeps track of a useful bit of metadata: the date a file is added to its parent folder. Finder lets you sort a folders contents by "date added". I wanted to do this with my ls output in the Terminal, too.
find . -depth 1 -exec mdls -name kMDItemFSName -name kMDItemDateAdded "{}" \; | sed 'N;s/\n//' | grep -v '(null)' | awk '{print $3 " " $4 " " substr($0,index($0,$7))}'
which lists files in the current directory prefixed by their date added timestamps:
...
2013-07-21 17:07:26 DGPDEC.pdf
2013-08-11 10:10:49 erlenmeyer-flask-difficult-inside-outside.pdf
2013-07-21 15:10:05 first-run
2013-03-13 23:10:43 new
2013-03-16 20:48:32 NPR Music Austin 100 ZIP (71 Tracks)
2013-04-25 09:04:26 particle.gif
2013-04-25 09:05:34 particle.html
...
I saved this as an alias by appending this to my .profile:
alias lsadded='find . -depth 1 -exec mdls -name kMDItemFSName -name kMDItemDateAdded "{}" \; | sed -e "s/^kMDItemFSName = \"\(.*\)\"/ \1/g" | sed "N;s/\n//" | sed -e "s/(null)/0000-00-00 00:00:00 +0000/g" | '"awk '{print \$3 \" \" \$4 \" \" substr(\$0,index(\$0,\$6))}'"
This is especially useful when combined with sort
and sort -r
:
lsadded | sort
Source