Sub topic 14
Querying metadata and searches from Terminal
This chapter assumes you have applied tags, built a smart folder, and know where Finder stores saved searches. You also need to be comfortable opening Terminal and typing short commands. Terminal shows you the same Spotlight data Finder uses, so you can check what a tag or smart folder is really doing.
How Terminal reaches the same data
Finder tags, search criteria, and smart folders all read from the Spotlight index. Three command-line tools expose that index:
mdlsprints the metadata stored for one file.mdfindruns a Spotlight query and prints the matching paths.plutilprints property list files, including saved smart folders.
Reading a file's tags with mdls
Tags are stored in the attribute kMDItemUserTags. To print it for one file:
mdls -name kMDItemUserTags ~/Documents/report.pdfIf the file has tags, the output is a list of tag names. If it has none, you see kMDItemUserTags = (null). Use this when a tag looks applied in Finder but a search does not find the file.
Searching with mdfind
mdfind takes a query in Spotlight's query language. The form is attribute operator value. Use -onlyin to limit the search to one folder.
To list files in Documents that carry the tag Red:
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'This is the query behind a tag search in Finder. Wrap the whole query in single quotes so the shell does not interpret the inner double quotes.
Date criteria work the same way. This finds files changed in the last seven days:
mdfind -onlyin ~/Documents 'kMDItemFSContentChangeDate >= $time.today(-7)'That is the kind of rule a "Last modified" criterion in a smart folder stores.
Reading saved smart folders
By default, Finder saves smart folders in your Saved Searches folder. List it with:
ls ~/Library/Saved\ SearchesEach smart folder is a .savedSearch file. These files are property lists, so plutil -p prints them in readable form. For a smart folder named Recent Documents:
plutil -p ~/Library/Saved\ Searches/Recent\ Documents.savedSearchThe output includes the stored search query and the criteria Finder shows in its search editor. Comparing this query with your mdfind results is a quick way to see whether the smart folder or the index is at fault.
A simple diagnostic routine
When a tag or smart folder shows missing items:
- Run
mdls -name kMDItemUserTagson a file that should match. If the result is(null), the tag is not stored. - Run the
mdfindquery for the tag. If the file is missing from the list, the index has not picked it up. - Run
plutil -pon the smart folder. Check that its query matches what you intended.
Each step narrows the problem to the file, the index, or the saved search.
Exercise
A file shows a red tag in Finder, but mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"' does not list it. Which command do you run first to find out whether the tag is stored on the file, and what output tells you it is not?
Show answer
Run mdls -name kMDItemUserTags on the file. If the output is kMDItemUserTags = (null), the tag is not stored in the metadata. If the tag is listed, the problem is the index or the folder being searched.
Exercise
Why is the query in mdfind wrapped in single quotes rather than double quotes?
Show answer
The query contains double quotes and, in date queries, $time. Single quotes stop the shell from changing or expanding those parts before mdfind receives them.