Sub topic 13
Where smart folders live on disk
This chapter assumes you can already create and save a smart folder in Finder, and that you know Spotlight metadata is what the criteria search. You also need basic Terminal use: running a command and quoting a path that contains spaces. The goal is to see what a smart folder is as a file, so you can back it up, move it, inspect it, and explain odd behavior.
A smart folder is a small file, not a folder
A smart folder holds no files. When you save one, Finder writes a single file with the extension .savedSearch. That file stores a Spotlight query and a few settings, such as the search scope and the criteria rows you see in the editor. Each time you open the smart folder, Finder reads the file, runs the query against the Spotlight index, and shows the current matches.
This explains several behaviors:
- The results are always current, because nothing is copied into the smart folder.
- Deleting a smart folder removes only the saved query. The matching files stay where they are.
- Deleting an item from inside the results does delete the real file, because the item shown is the real file.
- The same file can appear in many smart folders at once, because each one is only a query.
The default location
Finder saves smart folders in the Saved Searches folder inside your personal Library. The Save dialog lets you pick another location, so this is only the default. The Library folder is hidden by default. To open it, hold Option and choose Go > Library in Finder, or press Command-Shift-G and type ~/Library/Saved Searches.
You can also list the files from Terminal:
ls ~/Library/Saved\ SearchesEach smart folder you saved to the default location appears as Name.savedSearch. The backslash before the space keeps the shell from splitting the path in two. Quoting the whole path works too.
If the list is empty, you probably saved your smart folders elsewhere, such as Documents or the Desktop. You may also never have saved one. A search left unsaved in a Finder window does not create a file.
What happens when you save
The Save dialog asks for a name and a location. It also has an Add To Sidebar option, which is on by default. Saving creates the file and, if that option is on, adds a sidebar entry that points at it. The sidebar entry and the file are separate things, and this matters when you tidy up:
- Removing the item from the sidebar leaves the
.savedSearchfile on disk. - Deleting the file while the sidebar entry remains leaves a dead entry that no longer opens anything.
- Renaming the file changes the name of the smart folder in Finder.
Reading a saved search
A .savedSearch file is a property list (plist). A plist is Apple's structured settings format, and it may be stored as text (XML) or in a compact binary form. Opening a binary plist in a text editor shows unreadable characters. The plutil tool prints it in a readable form:
plutil -p ~/Library/Saved\ Searches/Recent\ Documents.savedSearchReplace Recent Documents with the name of one of your own smart folders. The output is a dictionary of keys and values. The exact contents vary with the macOS version and with how you built the search. You will commonly see these entries:
| Key | What it holds |
|---|---|
| `RawQuery` | The Spotlight query as text, for example a comparison on `kMDItemFSContentChangeDate`. |
| `RawQueryDict` | A dictionary with the same query plus options, including the search scopes. |
| `SearchCriteria` | The settings that Finder's criteria editor shows, such as the scope and the criteria rows. |
| `CompatibleVersion`, `Version` | Format version numbers. |
Treat RawQuery as the part that does the work. The criteria rows in SearchCriteria exist so that Finder can redraw the editor the way you left it.
To print a single value, use PlistBuddy, which ships with macOS:
/usr/libexec/PlistBuddy -c "Print :RawQuery" ~/Library/Saved\ Searches/Recent\ Documents.savedSearchIf you need the whole file as readable XML, write a converted copy instead of editing the original:
plutil -convert xml1 -o /tmp/recent.xml ~/Library/Saved\ Searches/Recent\ Documents.savedSearchThe -o option sends the result to a new file, so the original stays untouched.
Connecting the file to the query
The stored query is the same kind of query mdfind accepts. This is the practical payoff of knowing the file format. You can run the logic of a smart folder without Finder, which is useful for testing a query before you build it. For example, a smart folder for recent changes under Documents is equivalent to:
mdfind -onlyin ~/Documents 'kMDItemFSContentChangeDate >= $time.today(-7)'A tag-based smart folder works the same way:
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'If the mdfind command returns what you expect but the smart folder does not, the problem is probably in the scope or the saved criteria, not in the index. Compare the RawQuery text with your test query and check the search scopes in the plist.
Moving, copying, and backing up
Because a smart folder is one small file, it is easy to handle:
- Back up: copy the
.savedSearchfiles to a backup folder. They are tiny. A normal backup of your home folder includes~/Library/Saved Searchesif it backs up the Library. - Copy to another Mac: place the file in the same folder on the other Mac and open it. It will work if the same macOS features are available.
- Keep one anywhere: you can save a smart folder to the Desktop or a project folder and open it by double-clicking. It does not need to be in
Saved Searchesto work. - Share: send the file by email or AirDrop. The recipient gets your query, not your files.
Scope is the main thing that does not travel well. A scope can name a specific folder path. If that path does not exist on the other Mac, the smart folder returns nothing, even though the file opens without an error. Also, the results depend on the other Mac's own Spotlight index and on which items carry the tags the query uses. A query on kMDItemUserTags finds files tagged on that Mac, not yours.
Common problems with the file
| Symptom | Likely cause | What to check |
|---|---|---|
| `ls` shows no files | The smart folders were saved in another location, or never saved | Search for `*.savedSearch` with `mdfind -name .savedSearch`, or check the Save dialog's location next time |
| Sidebar entry does nothing | The file was moved or deleted | Look for the file, then remove the entry and add it again |
| The folder opens but is empty | The scope names a folder that no longer exists, or Spotlight does not index it | Check the scope in the criteria editor and the Spotlight privacy settings |
| `plutil` reports an error | The file is damaged or is not a plist | Restore it from a backup |
The mdfind -name command in the first row is a quick way to locate saved searches wherever they are. If it finds nothing, the Spotlight index may not include that location, which leads to the troubleshooting chapter at the end of this tutorial.
Summary
A smart folder is a .savedSearch property list that stores a Spotlight query and its criteria. The default location is ~/Library/Saved Searches, the sidebar entry is a separate pointer to that file, and plutil -p shows what Finder will run. With this model you can back up, move, and debug smart folders by reasoning about one small file and one query.
Exercise
You delete the file Recent Documents.savedSearch from ~/Library/Saved Searches, but the Recent Documents entry still shows in the sidebar. Do your documents still exist, what happens when you click the entry, and how would you recreate the smart folder with the same query?
Show answer
The documents still exist, because the smart folder only stored a query and never held the files. Clicking the entry does nothing useful, since the sidebar item points at a file that is gone. To recreate it, either restore the file from a backup, or rebuild the search in Finder with the same criteria, save it, and remove the dead sidebar entry. Before deleting, you could have read the query with plutil -p or PlistBuddy -c "Print :RawQuery", then tested it with mdfind.
Exercise
Write the Terminal command that lists the files in ~/Documents carrying the tag Red, and say which smart folder key holds the equivalent query.
Show answer
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'. The equivalent query is stored in the RawQuery key of the .savedSearch property list, and also appears inside RawQueryDict.