recent: the files you actually opened¶
The problem¶
"What was that file I had open on Tuesday?" Sorting by modified date answers a different question: what was written to. Sync clients, builds, package managers and caches write thousands of files a day, so the ones you opened sink. A file you only read is never modified, so it never shows up at all.
The method¶
macOS already records the answer. When an app opens a file through the system
(double-click, File > Open, open FILE), Spotlight sets kMDItemLastUsedDate
on it. Writing does not. One query lists what you opened:
mdfind -0 -onlyin ~ -attr kMDItemLastUsedDate \
'kMDItemLastUsedDate > $time.now(-2592000)' # last 30 days
recent wraps it and fixes the rough edges of the raw answer:
-0keeps a name with a newline in one record; the path is split off at the lastkMDItemLastUsedDate =, so a name holding that text still reads whole.- Spotlight prints UTC; the list shows local time.
- Noise is left out by folder name, not substring:
node_modules,.git,.venv,Library/Caches, app containers, iCloud app folders. iCloud Drive itself stays in. - The index lags behind deletes, so each answer is checked on disk before it
counts:
recent 10still prints ten. - Typed names are escaped (
\ " * ?) before they enter the query.
recent # last 30 files you opened
recent pdf -d 90 # PDFs opened in the last 90 days
recent -n invoice # names containing "invoice"
recent -m # by modified date instead
recent -p | head -1 # newest path, for scripts
Limits¶
- macOS only, and only folders Spotlight indexes.
- Files read from a terminal (
cat, an editor in the shell,./script) are not "opened"; use-mfor those. - Folders opened in Finder appear too.
- A home-wide query is slow while Spotlight rebuilds its index;
--rootnarrows it.
Code¶
methods/recent/ in the methods repository: one Python file (standard library), a README and a 40-check test.