How to hide a file or folder in macOS Finder

By

Published · Updated

Tagged: Mac

Learn how to hide a file or folder in the macOS Finder from the terminal using chflags hidden, how to see hidden items again, and the gotchas: Terminal still shows them, and some copies drop the flag.

I was slightly annoyed by macOS putting a Public folder in my home folder, with no reason.

I couldn’t delete it as it’s a system folder.

So I found out a way to hide it instead.

Hide a file or folder with chflags

Open Terminal and run chflags hidden followed by the path:

chflags hidden ~/Public

Finder hides the item immediately. If it remains visible, relaunch Finder or press Command-Shift-Period to check whether Finder is currently showing hidden files.

It works the same way for a single file:

chflags hidden ~/Desktop/notes.txt

Quote paths that contain spaces, for example chflags hidden "$HOME/My Folder".

You don’t need sudo for files and folders you own, and that includes the default folders in your home folder like Public.

Make it visible again

To unhide the item, run:

chflags nohidden ~/Public

If you can’t remember the exact path, press Command-Shift-Period in a Finder window. Finder now shows hidden items, dimmed. Find the one you want, and then run chflags nohidden on it. Press Command-Shift-Period again to go back to the normal view.

Tip: you can drag a file or folder from Finder into the Terminal window to paste its full path, already escaped.

Check if an item is hidden

The ls command with the -lO options shows the file flags. A hidden item has hidden in the flags column, and a normal one shows -:

ls -lOd ~/Public
drwxr-xr-x+ 6 flaviocopes  staff  hidden 192 Feb 28  2023 /Users/flaviocopes/Public

The -d option lists the folder itself instead of its contents.

This is the same trick Apple uses for the Library folder in your home folder. Run ls -lOd ~/Library and you’ll see the hidden flag on it.

Hide a folder and everything in it

chflags hidden only changes the item you pass. The files inside a hidden folder keep their own flags, but you don’t see them anyway, because you can’t open the folder in Finder.

If you want every file inside to have the flag too, use -R:

chflags -R hidden ~/Public

Be careful with -R and nohidden. If you run chflags -R nohidden on a folder, you also unhide files inside it that were hidden on purpose.

Gotchas

It only hides the item from Finder. The man page describes the flag as “Hide item from GUI”. In Terminal, ls still lists hidden items, even without -a. Anyone with access to your Mac can still find and open the file, so this is not a way to protect private files.

Some copies lose the flag. I tested this. mv keeps it, and so does cp -pR. A plain cp -R creates a visible copy, and a folder compressed with zip comes back visible when you unzip it. If you hide something and then move it around with scripts, check it again with ls -lO.

Dot files are a different thing. A file whose name starts with a dot, like .env, is hidden by Finder and by ls (unless you use ls -a). That works by name, so renaming the item is another way to hide it. For a system folder like Public you can’t rename it, so chflags is the way to go.

Always show hidden files in Finder

Command-Shift-Period toggles hidden items in Finder. If you want Finder to always show them, run:

defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder

To go back to the default:

defaults write com.apple.finder AppleShowAllFiles -bool false
killall Finder

This shows both dot files and items with the hidden flag.

Want me to talk about your product? You can sponsor this site.

Related posts about mac: