How to hide files and folders

The repertoire of every good magician includes making objects disappear and reappear, and the Finder is no exception. This brief article explains how you can do this with folders and files, both in the Finder and in Terminal’s command line.

Finder hiding

As far as the Finder is concerned, items whose name begins with a . (stop or period) are intended to be hidden, but if you try changing a file or folder’s name by adding a . to the start, the Finder will try to dissuade you by pretending that you really shouldn’t be doing that.

The little bit of magic you need is to reveal all hidden items first, by pressing Command-Shift-. (that’s a stop or period) before you try prefixing the name with a . If you do that, the Finder still checks if you’re sure, but this time allows you to hide the folder or file.

To unhide the item, repeat the process, this time removing the leading . Because the Finder uses caching extensively, you’ll find that you need to switch that Finder window to a different folder, then return to see the item unhidden.

Terminal hiding

While you can prefix the item’s name in Terminal if you prefer, there’s a second way to hide directories and files available in the command line. Type
chflags hidden
with a space after the second word, then drag and drop the item onto the command line, to paste its path there, and press Return. That item will then be hidden in the Finder, but without any change of name. To undo that, use the command
chflags nohidden
followed by the path and filename.

Spotlight is different

As far as the Finder is concerned, both techniques are good, and the item will be hidden from view unless you reveal hidden items by pressing Command-Shift-. But that isn’t the case for Spotlight search, where the two behave quite differently.

As far as Spotlight is concerned, items you’ve hidden using chflags are still included in any searches you make, but those hidden by prefixing their name with a . are excluded from all search results. This also applies to files with normal names that are inside a folder whose name begins with a ., making this one of the many ways of excluding files from Spotlight search.

Deep hiding

Leading stop/period characters and the hidden flag are mere amateurs compared to the most elusive files of them all, Desktop Services Stores named .DS_Store. These are almost universal in folders that have been changed in the Finder, and would ordinarily qualify as hidden on the basis of the . at the start of their name. However, Apple has decided that they should be hidden even more deeply, and they remain invisible even when you show hidden files using Command-Shift-.

.DS_Store files are where macOS keeps information about folder settings, icon locations, Finder (or Spotlight) Comments for files within that folder, and more. They were introduced when the code for the Finder was rewritten under the technical leadership of Arno Gourdol in 1999. The intention at the time was that these Desktop Services would be given a public API, but that never happened, and what’s stored in all those .DS_Store files remains opaque.

You may also come across shadow files whose names start with ._ that are used to carry extended attribute data as part of the AppleDouble file format used on some FAT file systems. They too are invisible in the Finder even when hidden files are supposed to be displayed, but are associated with individual files rather than folders.

Summary

To hide folders or files, show hidden items using Command-Shift-. then prefix their name with . (a stop or period), which also hides them from Spotlight, and hides all files within hidden folders too.
To hide folders or files from the Finder but not Spotlight, use the chflags hidden command, with chflags nohidden to unhide them.
.DS_Store files and those whose name starts with ._ are even more deeply hidden, and aren’t unhidden using Command-Shift-.