Where apps hide your files
Your Mac's Terminal remembers the commands you ran, but the files are hidden and Finder will not show them. This is where they are, why there are two places, and how to search, clear or back them up.
Since macOS Catalina the default shell is zsh, and its history file is:
~/.zsh_history
Terminal also keeps per-window history files under:
~/.zsh_sessions/
Both start with a dot, so Finder and Spotlight hide them. To see them in Finder press ⇧⌘. in your home folder, or use Go > Go to Folder (⇧⌘G) and type the path. In Terminal, ls -la ~/.zsh_history ~/.zsh_sessions lists them with sizes, without opening anything.
If you changed your shell to bash years ago, the file is ~/.bash_history instead. On a Mac that has barely used bash it may not exist, or it may hold a few bytes of leftover history.
The path comes from /etc/zshrc, which sets HISTFILE to ~/.zsh_history (or the same name inside $ZDOTDIR if you set that). The same file then loads a helper written for Terminal.app, /etc/zshrc_Apple_Terminal, which gives every Terminal window its own history so a restored window comes back with the commands it had.
Each window is identified by a session ID, and its files in ~/.zsh_sessions are named after it:
<ID>.history holds that window's commands.<ID>.historynew is the open window's working history file, folded into the other two when its shell exits.<ID>.session is a short script the shell runs when Terminal restores the window; by default it just prints "Restored session" with a date.When a window's shell exits, the helper appends its new commands to both the window's .history file and the shared ~/.zsh_history, then trims both to the size limit. So ~/.zsh_history is the merged record that brand-new windows start from. On exit, at most once a day, it also deletes session files that have not changed in two weeks (it runs find with -mtime +2w); _expiration_check_timestamp records when that last happened. On the Mac used to check this guide, ~/.zsh_sessions held 145 files taking 2.9 MB.
Other terminals that run zsh write to ~/.zsh_history too. /etc/zshrc only loads a helper named after the terminal program, and macOS ships one for Terminal.app alone, so they do not use ~/.zsh_sessions.
The defaults come from /etc/zshrc:
| Setting | macOS default | Meaning |
|---|---|---|
HISTFILE | ~/.zsh_history | Where history is saved |
HISTSIZE | 2000 | Commands held in memory in a session |
SAVEHIST | 1000 | Commands written to the file |
So by default only the last 1,000 commands survive on disk, and Apple's own comments in the Terminal helper recommend larger values. Add lines such as HISTSIZE=50000 and SAVEHIST=50000 to ~/.zshrc. Run echo $HISTFILE to see which file your shell is using, because a ~/.zshrc or a framework like Oh My Zsh can point it elsewhere. (Inside a Terminal window it may show that window's .historynew file, which is the helper at work.)
By default a command typed in one tab reaches the shared file only when that tab's shell exits, so other open tabs never see it. Adding setopt SHARE_HISTORY to ~/.zshrc writes each command straight away and reads other windows' commands as you go. Apple's helper notices this option (and INC_APPEND_HISTORY) and switches its per-window history off unless you set SHELL_SESSION_HISTORY=1, so you get one shared list instead of one per window.
History often contains tokens, passwords typed on a command line and private paths, so treat the file like a secret and do not paste it into chat or a bug report. To search it without opening it in an editor, use the shell's own command:
history 1 | grep ssh
If the EXTENDED_HISTORY option is on, each line in the file starts with a timestamp (: 1730000000:0;command), so reading it raw is less pleasant than the history command. Press ⌃R at the prompt for a live reverse search.
Do this from Finder, not from a Terminal window, because every open zsh writes its history back to the file when it exits:
~, and press ⇧⌘. to show hidden files..zsh_history to the Trash. Drag .zsh_sessions there too, because each window's .history file holds its own copy of past commands.Empty the Trash only when you are sure. To remove a single line instead, such as a command that included a password, open the file in a text editor with every terminal quit, delete the line and save; the same line may also sit in files inside ~/.zsh_sessions.
To switch the per-window files off, Apple's helper documents two settings, best placed in ~/.zshenv because that file is read before /etc/zshrc: SHELL_SESSION_HISTORY=0 turns off per-window history and keeps one shared list, and SHELL_SESSIONS_DISABLE=1 turns off the whole save and restore mechanism. Deleting ~/.zsh_sessions is otherwise harmless: Terminal's restored windows lose their own history and the folder is recreated.
Copy ~/.zsh_history and, if you like your shell setup, ~/.zshrc. Migration Assistant brings home-folder dotfiles across, and Time Machine backs them up like any other file. You do not need ~/.zsh_sessions on the new Mac. The file is readable only by you (mode -rw-------), so a copy made with another account may be unreadable until you fix its owner.
Spotlight skips hidden dot-files, which is why a search for zsh_history returns nothing. Everywhere matches hidden file names too, so typing zsh_history or .history lists the main file and every window's session file at once, and ⌘T opens a Terminal in the folder holding the one you picked. It matches names only, so it shows where a history file is but not which one contains a particular command; use history 1 | grep for that.
In ~/.zsh_history in your home folder for zsh, the default shell. Terminal.app also keeps per-window history files in ~/.zsh_sessions/.
macOS saves 1000 commands by default (SAVEHIST in /etc/zshrc). Add HISTSIZE=50000 and SAVEHIST=50000 to ~/.zshrc and open a new window.
Yes. It only holds per-window history and restore scripts, and Terminal recreates it. Restored windows lose their own history, and commands from windows still open are saved again when they close.
Names starting with a dot are hidden. Press ⇧⌘. in Finder to show them, or open the path with Go to Folder.
Everywhere keeps track of every file on your Mac — including the folders Spotlight hides and drives you have unplugged. Free for a day, then $19 once.
Version 2.20.3 · macOS 13+ · 4.7 MB · notarised