9

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?

  • 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
5

Simpler (Faster) solution:

mdls -name kMDItemFSName -name kMDItemDateAdded -raw * | \
xargs -0 -I {} echo {} | \
sed 'N;s/\n/ /' | \
sort
  • 1
    yep, much better :) – Ramiro Araujo Aug 3 '15 at 19:40
  • Could someone detail what each of those lines do? Also, can this solution be performed in Terminal as individual command lines (sequentially, one after the other), or only within a script? – EJ Mak Sep 20 '18 at 15:37
4

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.

  • 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
  • 2
    Paste that one liner in by editing my answer - share the knowledge! – bmike Mar 21 '13 at 20:55
2

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

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

man ls

 -U      Use time of file creation, instead of last modification for
         sorting (-t) or long output (-l).
  • 1
    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
  • 1
    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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .