brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mdfind -name searches by file name from the terminal, using Spotlight

Terminal
~/tips/tip-macos-mdfind-name

mdfind -name invoice -onlyin ~/Documents

Finds files by name using the Spotlight index, much faster than walking folders.

Spotlight is fast because it keeps an index. mdfind lets you use that index without leaving the terminal.

Use -name when you only care about file names, and -onlyin to stay inside one folder.

mdfind -name invoice -onlyin ~/Documents

According to the man page, -name matches file names only, so you will not get every document that merely mentions the word. Without -onlyin it searches everything Spotlight knows about.

It plays nicely with other tools. Add -0 and each path ends with a NUL character, so xargs -0 handles names with spaces.

mdfind -name invoice -onlyin ~/Documents -0 | xargs -0 ls -l

The caveat is the index. If Spotlight does not index a folder, mdfind will not find files in it, and a brand new file may need a moment to show up. It is also not find: you cannot walk a tree with it.

A small habit that helps: put the command in a shell alias if you search a lot. Spotlight in the menu bar is fine for opening apps, but a terminal search is easier to repeat, script and pipe into something else, and it uses the same index.

Further reading: mdfind.1 man page, On this site: mdls -name kMDItemWhereFroms tells you where a download came from, On this site: mdutil -s tells you if Spotlight is indexing a volume

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link