Does "$ mdls -recursive" exist?

478 Views Asked by At

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.

1

There are 1 best solutions below

2
Benjamin W. On BEST ANSWER

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.