I need to build a script that shows files in the same order as the default Downloads folder in the dock, ordered by Date Added, newest first. I can't seem to find the option in ls
.
Any other way to do it?
I need to build a script that shows files in the same order as the default Downloads folder in the dock, ordered by Date Added, newest first. I can't seem to find the option in ls
.
Any other way to do it?
Simpler (Faster) solution:
mdls -name kMDItemFSName -name kMDItemDateAdded -raw * | \
xargs -0 -I {} echo {} | \
sed 'N;s/\n/ /' | \
sort
The date added is stored as metadata item kMDItemDateAdded
and the mdls
command will expose the data for each file passed as an argument to it.
So, to dump the date added for all files in Downloads in whatever arbitrary order * gets expanded by your shell, you can:
mdls -name kMDItemDateAdded ~/Downloads/*
You'll need to hack together some combination of find and sed/awk/perl/whatever to assemble a replacement for ls
but perhaps mdfind can be called by your script rather than needing to reinvent ls
and parsing that output.
Well, as usual, after writing the question I start digging for metadata content in files, and ended up writing this:
ls -a | \
grep -v '^\.$\|^\.\.$' | \
xargs -I {} mdls -name kMDItemFSName -name kMDItemDateAdded {} | \
sed 'N;s/\n//' | grep -v '(null)' | \
awk '{print $3 " " $4 " " substr($0,index($0,$7))}' | \
sort -r
Basically it: 1. list all files 2. filters out . and .. 3. gets the name and date added, one line after the other 4. merges every two lines into 1 line 5. extracts the date, time and name 6. sorts it in reverse (since datetime is upfront, it sorts by datetime)
Hope it helps someone else! :)
@bmike actually, the site imposed me a restriction to answer my own question only after 8 hours of posting my question, due to my lack of reputation :D
ls -A
instead of -a
so you don't have to grep -v
for .
and ..
.