2
function ls-by-added() {
    # Doesn't work well for files having '(null)' as their DateAdded, which some do.
    mdls -name kMDItemFSName -name kMDItemDateAdded -raw -- *(D) | \
        xargs -0 -I {} echo {} | \
        sed 'N;s/\n/ /' | \
        sort --reverse | \
        sed -E "s/^.*\\+0000 //" # removes the timestamps
}

It doesn't work for dotfiles:

ls-by-added                                                                                      
foo
How Automated Recommendations Affect the Playlist Creation Behavior of Users_k2opt.pdf
research_report_dc_02_k2opt.pdf
research_report_dc_02.pdf
thebookofshaders_k2opt.pdf
thebookofshaders.pdf
A New Kind of Science
- Books alias
Hein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005)_k2opt.pdf
Hein J. L. - Prolog Experiments in Discrete Mathematics, Logic, and Computability (2005).pdf
geMsearch- Personalized Explorative Music Search.pdf
Lumanote- A Real-Time Interactive Music Composition Assistant.pdf
How Automated Recommendations Affect the Playlist Creation Behavior of Users.pdf
Computability-and-Logic_k2opt.pdf
Computability-and-Logic.pdf
(null) .rm
(null) .foo
(null) .DS_Store

What am I doing wrong? I tried mdimport . on the working dir before running the command, but it didn't help.

  • You might add that your shell is zsh to the initial post. – fd0 Aug 10 '18 at 16:55
  • @fsb Problem is the same in bash. – nohillside Aug 10 '18 at 17:31
  • Why do you want to use mdls for this (instead of ls -U)? Anyway, if md doesn't have the information all you can do is to add grep -v '(null) to the pipeline. – nohillside Aug 10 '18 at 17:35
  • @nohillside alias last-created='\ls -AtU|head -n1'. Not last-added :) – HappyFace Aug 30 '18 at 14:41

You must log in to answer this question.

Browse other questions tagged .