2

Can the mdls command be used recursively in the macOS terminal? Is there an alternative that will get me a list of every file along with all the mdls info? ls has an option to get me some, but not nearly as many as mdls.

This is a followup question to In the macOS terminal, "ls | mdls" commands are only working for the home directory.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • What do you actually want to do? There are options but you surely don’t want/need `mdls` output for every single one of 8 million files in your HOME directory and below? – Mark Setchell Aug 02 '18 at 10:34

1 Answers1

3

If you use the globstar shell option (Bash 4.0 or newer1), you can do something like this:

shopt -s globstar
mdls -name kMDItemFSName -name kMDItemDateAdded **/*

The output will look something like

kMDItemDateAdded = 2018-07-10 15:33:04 +0000
kMDItemFSName    = "File1.txt"
kMDItemDateAdded = 2018-07-11 17:18:11 +0000
kMDItemFSName    = "File2.txt"

with the disadvantage that path information is lost.

If you have many, many files, resulting in a command line that gets too long, you can loop over the files instead:

for f in **/*; do
    printf '%s\t%s\n' "$f" "$(mdls -name kMDItemDateAdded "$f")"
done

with output that looks something like

File1.txt       kMDItemDateAdded = 2018-07-10 15:33:04 +0000
File2.txt       kMDItemDateAdded = 2018-07-11 17:18:11 +0000

or you could use find:

  • GNU find:

    find -printf '%p\t' -exec mdls -name kMDItemDateAdded {} \;
    
  • BSD find:

    find . -exec printf '%s\t' {} \; -exec mdls -name kMDItemDateAdded {} \;
    

1 Which, in macOS, you'd have to install first, for example using Homebrew.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116