everyday mac tools

How to exclude a folder from a file search on a Mac

· 7 min read

To keep a folder out of every search for good, add it to Spotlight’s privacy list: System Settings, Spotlight, Search Privacy. To leave it out of one search only, run the search in Terminal and filter the results with grep -v. Finder has no “except this folder” control, and the permanent route hides the folder from every app that relies on Spotlight, so use it only for folders you never want to find.

First, find out which folder is doing the drowning

It is worth ten seconds of counting before you change a setting. On the Mac this guide was written on, a developer’s machine, this command:

mdfind -name package.json -count

returned just over 12,000 results. Adding one filter shows where they live:

mdfind -name package.json | grep -c /node_modules/

All but about 200 were inside node_modules folders. That is the usual shape of the problem: one kind of folder, repeated hundreds of times, burying the handful of files you wanted.

One surprise from the same test. ~/Library/Caches held at least 100,000 files, and Spotlight had indexed none of them. If files from that folder are flooding your results, the search you are using is probably not Spotlight: it is find, or an app that builds its own catalog of the disk. That changes which of the fixes below applies.

Leave a folder out of Spotlight for good

This is the setting to use for an old archive, a folder of backups copied from a previous Mac, or a projects folder you only ever open from your code editor. The steps below are for macOS 15 Sequoia.

  1. Open the Apple menu and choose System Settings.
  2. Click Spotlight in the sidebar.
  3. Scroll to the bottom and click Search Privacy. A sheet opens with the list of excluded locations; it reads “No Locations Added” if you have never used it.
  4. Click the plus button (its tooltip reads “Add a folder or disk to exclude”), pick the folder, and click Choose.
  5. macOS asks whether you are sure you want to prevent Spotlight from searching in that folder. Confirm, then click Done.

On macOS 13 Ventura and 14 Sonoma the pane is called Siri & Spotlight and the button reads Spotlight Privacy; the list works the same way.

A few practical notes. The Library folder is hidden in the file chooser, so press Shift-Command-G and type a path such as ~/Library/Developer to get there. You can add a whole external drive, which suits a backup disk you plug in for other reasons. To undo an exclusion, select it and click the minus button; Spotlight then indexes that folder again from scratch, which takes a while if it is large.

The limit that matters for developers: the list takes specific folders, one at a time, with no patterns. There is no way to say “every folder called node_modules”. You can add the parent projects folder instead, but then your own source files vanish from search too. For that case the per-search filters further down are the better tool.

What else stops seeing an excluded folder

When you add a folder, macOS warns: “If you exclude this location from Spotlight searches, the search feature won’t work in some applications.” Take that literally. The index is shared, so an exclusion applies to everything that reads it:

  • the Spotlight window and Finder’s search field,
  • the Recents item in the Finder sidebar,
  • the search field in Open and Save dialogs,
  • mdfind in Terminal,
  • search inside apps that use Spotlight rather than their own index; Mail is the usual example, if you exclude the folder where its data lives.

What it does not change: the files themselves, browsing to the folder in Finder, and Time Machine, which keeps its own exclusion list under System Settings, General, Time Machine, Options. It is also not a way to hide a folder from someone else using your Mac. Anyone can still open it; it just will not come up in a search.

Finder can narrow a search, but not subtract a folder

Finder’s criteria rows (the plus button at the right end of the row that appears under the toolbar when you search) describe the file itself: its name, kind, dates, size. None of them is “not inside this folder”. Option-click the plus button, set the new group to None, add Name contains node_modules, and it removes the folders with that name from the results. The thousands of files inside them stay, because their own names do not contain the word.

What works in Finder is the opposite approach: scope the search to the one folder you do want. Search inside one folder only on a Mac covers that, including making it the default. If your results are coming from all over the disk, that is usually quicker than trying to list everything you do not want.

In Terminal: mdfind plus grep -v

mdfind has no flag for leaving a folder out; -onlyin includes a folder, it never excludes one. So search broadly and filter the output, which has the advantage that you can see exactly what was removed.

mdfind -onlyin ~/Projects -name package.json 2>/dev/null | grep -v /node_modules/

grep -v drops every line containing the text. The slashes on both sides make it match a folder in the path rather than any filename that happens to contain the word. To drop several folders at once, repeat -e:

mdfind -name invoice 2>/dev/null | grep -v -e /node_modules/ -e "/Old Archive/" -e /Backups/

The 2>/dev/null hides a line some versions of macOS print before the results (“Loading keywords and predicates”), which is harmless but clutters the output.

If you plan to pass the results to another command, keep filenames with spaces intact by separating them with null characters on both sides of the filter:

mdfind -0 -onlyin ~/Projects -name package.json 2>/dev/null | grep -zvF /node_modules/ | xargs -0 ls -l

-F treats the text literally and -z makes grep read and write null-separated results, and the grep that ships with macOS supports both, so nothing needs installing. mdfind: searching a Mac from the Terminal covers the rest of the query syntax.

For folders Spotlight does not index at all, find can skip a folder without even walking into it:

find ~/Projects -name node_modules -prune -o -name "*.json" -print

-prune stops find at every node_modules folder, so it is much faster than searching everything and filtering afterward.

In Everywhere: put ! in front of path:

Everywhere is a Mac file search app that catalogs every file on the Mac once, then answers as you type. Because it covers everything, including places Spotlight never indexes, dependency folders and caches show up there too, so exclusion matters more.

Its operators do this in the query itself. path: restricts results to a folder and ! means not, so combining them drops a folder:

package.json !path:node_modules
invoice ext:pdf !path:Backups !path:Caches

Each !path: term removes another folder, and they stack with ext: and size:. Nothing is changed permanently, and Spotlight’s privacy list is untouched. Press Command-slash in Everywhere to see every operator.

Keep the excluded word specific. path: matches the text you type within a folder’s path, so !path:Library would drop far more than your Library folder, including anything in the Photos library. It also searches names and paths only, not the text inside documents; for a phrase you remember from inside a file, Spotlight remains the tool.

Questions

Will excluding a folder from Spotlight speed up my Mac? It can reduce indexing work for folders whose contents change constantly, such as dependency folders that get reinstalled. It does not free any disk space. Exclude folders because they spoil your searches rather than in the hope of a faster Mac.

Does the privacy list delete or hide the files? No. The files stay where they are and open normally. They are simply left out of the index, so nothing that searches through Spotlight will list them.

Can I exclude every node_modules folder at once? Not in Spotlight’s list, which takes one folder at a time. In Terminal, grep -v /node_modules/ or find with -prune handles every one by name, and in Everywhere !path:node_modules does the same inside a single search.

I removed a folder from the privacy list and it still does not show up. Spotlight has to index it again, which can take minutes to hours depending on size. If it still will not appear, Spotlight cannot find a file you know exists lists the other causes to check.