How to Password Protect a Folder on Mac Using Terminal (hdiutil Guide)
Password protect folder Mac Terminal guide: create an AES-256 encrypted sparse bundle with hdiutil, mount and eject it, and see why zip -e is weaker.
You can password protect a folder on a Mac from Terminal by wrapping it in an AES-256 encrypted sparse bundle with hdiutil create. You mount it with hdiutil attach when you need the files and eject it with hdiutil detach when you are done. It uses the same disk image encryption macOS uses elsewhere and is much stronger than zip -e, but it has sharp edges that this guide covers.
What the Terminal method actually does
macOS has no "lock this folder" switch in Finder. The built-in way to get a password on a folder is to copy it into an encrypted disk image. A sparse bundle is a disk image stored as many small band files inside a .sparsebundle package. It only uses the space your data needs, and it grows as you add files.
When the image is unmounted, the contents are unreadable without the password. When it is mounted, it behaves like any other volume under /Volumes.
The tool for this is hdiutil, which ships with macOS. Nothing needs to be installed.
Before you start
- Make a separate backup of the folder. You are about to create an encrypted copy and delete the original, and a typo in the wrong place can cost you data.
- Pick a password you can recover. If you lose it, the data is gone. There is no reset.
- The examples use a folder called
Privatein~/Documents. Change the paths to match yours.
Step by step: create, mount and eject an encrypted sparse bundle
1. Enter the password without putting it in your history
If you type the password inside a command, it ends up in your shell history file. Read it into a variable instead:
printf 'Password: '; read -rs PW; echo
The -s flag keeps the typing hidden, and the password is not written to history. It does stay in that shell session's memory until you unset it in step 3.
2. Create the encrypted sparse bundle
printf '%s' "$PW" | hdiutil create \
-encryption AES-256 \
-stdinpass \
-type SPARSEBUNDLE \
-fs APFS \
-volname "Private" \
-srcfolder ~/Documents/Private \
~/Documents/Private.sparsebundle
What each part does:
-encryption AES-256encrypts the image with AES-256.-stdinpassreads the password from standard input instead of prompting.printf '%s'sends it without a trailing newline.-type SPARSEBUNDLEmakes a growable, band-based image.-fs APFSformats the volume inside as APFS.-volnameis the name the volume shows when mounted.-srcfoldercopies the contents of that folder into the new image.- The last argument is where the
.sparsebundleis written.hdiutiladds the extension if you leave it off.
The image is sized from the source folder. If you expect to add a lot of data later, you can set a larger maximum with -size, for example -size 20g. Sparse bundles only use the space they hold. Test this on a copy first, and check the result in step 3.
3. Clear the variable and check the result
unset PW
ls -d ~/Documents/Private.sparsebundle
Then mount the image and look inside before you touch the original. Run the attach command without piping a password, and hdiutil prompts for it:
hdiutil attach ~/Documents/Private.sparsebundle
ls /Volumes/Private
If your files are listed, the copy worked. If you plan to script this, hdiutil attach -stdinpass works the same way as in step 2.
4. Delete the original folder yourself
The original is still sitting there, unencrypted. Terminal will not remove it for you.
Warning: destructive.
rm -rdeletes immediately and bypasses the Trash. Only run it after you have mounted the sparse bundle, confirmed that every file is inside, and made a separate backup. Check the path twice.
rm -r ~/Documents/Private
Two caveats. Deleting a file does not guarantee it cannot be recovered, especially on an SSD. Also, earlier Time Machine backups or local snapshots may still hold unencrypted copies of the folder.
5. Mount it when you need the files
hdiutil attach ~/Documents/Private.sparsebundle
Enter the password at the prompt. The volume appears at /Volumes/Private and in the Finder sidebar. You can also double-click the .sparsebundle in Finder, which shows a password dialog. That dialog has an option to remember the password in your Keychain, which is convenient but means anyone logged in as you can mount it.
6. Eject it when you are done
hdiutil detach /Volumes/Private
If it reports the volume is busy, quit the apps that have files open from it and try again. hdiutil detach -force exists, but it can discard unsaved changes, so treat it as a last resort.
An encrypted sparse bundle is only protected while it is unmounted. While it is mounted and your Mac is unlocked, the files are readable like any other.
Why zip -e is weaker
The common shortcut is:
zip -er Private.zip Private
This works and prompts for a password, but the Info-ZIP zip that ships with macOS uses the old ZipCrypto scheme for -e. It is not AES. ZipCrypto is known to be weak against known-plaintext attacks. A zip archive also exposes its file names and sizes to anyone who opens the listing, even without the password. Avoid zip -e -P password, which puts the password on the command line.
A zip is still fine for casually keeping a file out of an email preview. For anything you would be upset to leak, use an encrypted image. Our guide to password protecting a zip file on Mac covers when a zip is acceptable and how to make the password worth something.
Comparing the options
| Method | Encryption | File names hidden | Password prompt | Works offline | Main drawback |
|---|---|---|---|---|---|
zip -e |
ZipCrypto (weak) | No | Terminal prompt | Yes | Weak scheme, visible listing |
hdiutil sparse bundle |
AES-256 | Yes, while unmounted | Terminal or Finder dialog | Yes | Manual workflow, you delete the original |
| Disk Utility image from folder | AES-128 or AES-256 | Yes, while unmounted | Finder dialog | Yes | Same manual workflow, GUI steps |
| FileVault | XTS-AES-128 full disk | Yes, at rest | Login | Yes | Protects the whole disk, not single folders |
Disk Utility (File > New Image > Image from Folder) produces the same kind of encrypted image without Terminal. FileVault is excellent for a lost or stolen Mac, but once you are logged in, every folder is readable. It does not help with a single private folder. For a wider comparison, see 4 ways to password protect a folder on Mac.
The honest downsides of the Terminal route
The hdiutil approach is solid cryptography with a manual workflow. Know these before you rely on it:
- Password handling is on you. A password typed straight into a command lands in shell history. The
read -rspattern above avoids that, but it is easy to forget when you are in a hurry. - You must delete the original. Nothing links the sparse bundle to the folder it came from. If you skip step 4, you have an encrypted copy and an unprotected original.
- No visual cue. Finder shows a
.sparsebundlepackage, not a locked folder. There are no badges or placeholders, so after a few months it is easy to forget which items are protected. - Easy to lose the password. There is no recovery. If the password lives only in your head, a typo in your notes or a forgotten phrase means the data is gone.
- Mounted means exposed. If you forget to detach, the volume stays readable until you eject it, log out or shut down.
- Repetition. Every folder needs the same sequence, and each lock or unlock means a command or a few clicks.
None of this makes the method bad. It is a good fit for one or two folders you rarely touch, or for scripts you control. It gets tedious when you protect many items or open them often.
Doing it from a right-click with Finderlock
Finderlock does the encryption for you instead of disk images you manage yourself. When you protect an item, it encrypts it into its own .secured container with AES-256-GCM (Apple CryptoKit), with a key derived from your password using PBKDF2-HMAC-SHA256 (150,000 iterations, random 32-byte salt). The workflow is different from the Terminal method:
- Right-click a file or folder in Finder and choose Protect.
- Set a per-item password, or use Touch ID on plans that include it. The password is stored in the macOS Keychain, so you are not pasting it into a shell.
- The item is encrypted into its own container, and a placeholder file stays at the original location so you can see something is protected.
- Double-click the placeholder to unlock it. This does not need the main app window. Right-click offers Lock to seal it again, and Unlock is there too.
A few things it adds on top of the manual method:
- Auto-lock on inactivity and on sleep. Pro adds smart rules for logout, user switch, screen saver, system lock and process exceptions.
- Lock-on-close, which detects when the app that opened a file quits.
- Touch ID through a Keychain key with
biometryCurrentSetaccess control, or an optional app-wide master password with a 5-minute session. - A menu bar app and dashboard with search and drag-and-drop. Bulk operations are on Pro and above.
- Backups of the encrypted containers to iCloud or Google Drive, plus a recovery tool that uses the master password.
- On the Lifetime plan, Shred, a multi-pass secure delete in the DoD 5220.22-M style. Shredding is not guaranteed to be effective on SSDs, so do not rely on it there.
Finderlock runs locally. The only network use is the license check, optional opt-in analytics, updates and any backup destination you choose. It needs macOS 13.5 or later (the Finder right-click menu and badges need macOS 14) and is distributed from finderlock.com. Details are on the security page and in the docs.
If the Terminal method already works for you, keep using it. Finderlock is for when you want encryption without retyping commands or tracking which folders you have protected. The free plan covers 3 protected items. Personal, Pro and Lifetime are one-time purchases, at list prices of $49, $99 and $149.
FAQ
Can you password protect a folder on a Mac from Terminal?
Yes. Terminal cannot put a password on a plain folder, but hdiutil create can copy it into an AES-256 encrypted sparse bundle. You then mount that bundle with a password whenever you need the contents.
Is hdiutil encryption secure?
With -encryption AES-256 and a strong, unique password, it is a well-regarded approach and is the native macOS image encryption. The weak points are usually the password, a leftover unencrypted original, or leaving the image mounted.
How do I open an encrypted sparse bundle again?
Run hdiutil attach ~/Documents/Private.sparsebundle and enter the password, or double-click the .sparsebundle in Finder. Eject it with hdiutil detach /Volumes/Private or from the Finder sidebar.
Is zip -e safe enough to protect a folder?
For serious use, no. The macOS zip -e uses ZipCrypto, which is weak, and it leaves file names visible. Use an encrypted sparse bundle or a zip tool that supports AES.
What happens if I forget the password?
You lose access to the contents. AES-256 has no backdoor, and hdiutil has no reset. Keep the password in a password manager, and keep a backup of anything you cannot replace.
Next step
If you only have a folder or two, run the commands above on a test folder first, confirm you can mount and eject it, then do the real one. If you would rather not manage this by hand, try Finderlock free and protect up to 3 items from the right-click menu. Comparing other tools? See best Mac encryption apps.