Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
Display it where? –  Rob Mar 21 '13 at 16:53
    
Command line!.Got it! I ended up writing a one liner that does the trick. Since I dont have much reputation, I cannot answer my own question yet, I'll do it later. –  Ramiro Araujo Mar 21 '13 at 17:03
    
Welcome to the site Ramiro - you should be able to answer your own question any time - but there will be a restriction for you to "mark as accepted" your answer. Feel free to edit my answer to suit your needs or provide your own answer if/when you like. –  bmike Mar 21 '13 at 18:02

3 Answers 3

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.

share|improve this answer
1  
Yes! I found it later and made a beautiful one liner that teached me a lot of things :) Thanks for the help –  Ramiro Araujo Mar 21 '13 at 20:01
1  
Paste that one liner in by editing my answer - share the knowledge! –  bmike Mar 21 '13 at 20:55
up vote 1 down vote accepted

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

share|improve this answer
    
oh, I split the one liner in several lines for clarity only :) –  Ramiro Araujo Mar 22 '13 at 2:58
    
Use ls -A instead of -a so you don't have to grep -v for . and ... –  mfilej Nov 10 '13 at 10:12

man ls

 -U      Use time of file creation, instead of last modification for
         sorting (-t) or long output (-l).
share|improve this answer
    
nope, I need date added, not date created. Same as the default downloads folder in the dock –  Ramiro Araujo Mar 21 '13 at 17:05
    
how is date added not the same as date created? –  Jason Mar 21 '13 at 17:10
    
Jason - date created is when the file was created so when Safari downloads a file from another computer, it was clearly created for that download or could have been created months or years before the download happens. See my answer for details on where this data gets stored on OS X. –  bmike Mar 21 '13 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.