Managing your command history is a fundamental skill for anyone working in a Linux environment, whether you are a system administrator, a developer, or a casual user. In real terms, the shell history feature is incredibly convenient for recalling complex commands, but it also stores sensitive data like passwords, API keys, and file paths that you may not want lingering on a shared or multi-user system. Knowing how to delete history on Linux effectively ensures your privacy and keeps your terminal workspace clean. This guide covers everything from clearing the current session buffer to permanently wiping specific entries or the entire history file across different shells.
Not obvious, but once you see it — you'll see it everywhere.
Understanding How Linux History Works
Before diving into deletion methods, it helps to understand the mechanism. zsh_historyfor Zsh. As you type commands, they are stored in the active memory buffer. This file is usually located at~/.When you open a terminal, the shell (typically Bash, Zsh, or Fish) loads a history file into memory. That said, bash_historyfor Bash or~/. By default, these commands are only written to the physical history file on disk when the shell session exits cleanly.
This distinction between in-memory history (the current session) and on-disk history (the saved file) is critical. So if you delete the file but keep the terminal open, your current session commands will overwrite your deletion the moment you close the window. Conversely, clearing the memory buffer does not touch the saved file on the hard drive until you save or exit.
Clearing History in Bash (The Default Shell)
Bash is the most common default shell on Linux distributions. It provides the built-in history command with several flags to manage the list That's the part that actually makes a difference. Took long enough..
Clearing the Current Session Only
If you want to wipe the slate clean for the current terminal window without affecting the saved history file, use the -c flag:
history -c
This command clears the history list in RAM. On the flip side, if you open a new terminal tab immediately after, you will still see your old history because the ~/. bash_history file remains untouched Worth keeping that in mind..
Deleting the Entire History File Permanently
To remove the history completely—both the current session buffer and the saved file—you need a two-step approach. First, clear the memory, then delete the file, and finally ensure the shell doesn't save the current session upon exit No workaround needed..
- Clear the in-memory history:
history -c - Delete the physical history file:
rm ~/.bash_history - (Optional but recommended) Exit the shell immediately without saving:
Note: Usingkill -9 $kill -9 $terminates the current shell process instantly, preventing thehistory -w(write) operation that usually happens on a gracefulexit.
Deleting Specific Lines
Often, you only need to remove a single mistaken command (like one containing a password) rather than everything. Use the -d flag followed by the line number.
- View history with line numbers:
history - Delete the specific entry (e.g., line 1024):
history -d 1024 - Write the changes to the file immediately:
history -w
You can also delete a range of lines using a loop or sed directly on the file, but the history -d method is safer as it handles file locking and formatting automatically.
Preventing Commands from Being Saved (HISTCONTROL)
Prevention is better than cure. You can configure Bash to ignore commands that start with a space or duplicate entries. Add this to your ~/.bashrc:
export HISTCONTROL=ignoreboth:erasedups
ignorespace: Lines starting with a space are not saved.ignoredups: Duplicate consecutive lines are not saved.ignoreboth: Enables both.erasedups: Removes previous duplicate entries from the entire history.
After editing ~/.Consider this: bashrc, run source ~/. bashrc to apply changes. Now, typing secret_command (with a leading space) executes the command but keeps it out of history entirely.
Managing History in Zsh (Z Shell)
Zsh handles history slightly differently, offering more granular control via options set in ~/.zshrc. Because of that, the history file is typically ~/. zsh_history (or defined by $HISTFILE).
Clearing All History
Zsh does not have a direct history -c equivalent that works identically to Bash. The most reliable way to clear everything is:
# Clear current session memory
fc -p
# Or simply delete the file and restart shell
rm ~/.zsh_history
exec zsh
The fc -p command pushes the history list onto a stack, effectively creating a new empty list for the current session. That said, deleting the file and restarting the shell (exec zsh) is the cleanest "nuclear option."
Deleting Specific Entries
You can use the fc builtin to edit the history file interactively:
fc -R # Reload history from file
fc -e - 10 10 # Edit/delete line 10 (opens editor)
Alternatively, since Zsh history is a plain text file (usually with timestamps), you can safely edit ~/.zsh_history with sed, vim, or nano to remove specific lines, provided no other Zsh instances are writing to it simultaneously That's the whole idea..
Useful Zsh History Options
Add these to ~/.zshrc for better privacy hygiene:
setopt HIST_IGNORE_SPACE # Ignore commands starting with space
setopt HIST_IGNORE_DUPS # Ignore consecutive duplicates
setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicates first when trimming
setopt HIST_FIND_NO_DUPS # Don't display duplicates during search
setopt HIST_SAVE_NO_DUPS # Don't write duplicates to history file
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks
setopt INC_APPEND_HISTORY # Write to history file immediately, not on exit
The INC_APPEND_HISTORY option is particularly powerful; it writes commands to the file instantly. This means if you delete the file (rm ~/.zsh_history), the next command you type creates a brand new, clean history file immediately And it works..
Managing History in Fish Shell
Fish (Friendly Interactive Shell) stores history in ~/.local/share/fish/fish_history (or $XDG_DATA_HOME/fish/fish_history). It uses a structured format (YAML-like) rather than plain text.
Clearing History
Fish provides a dedicated, user-friendly command:
history clear
This command prompts for confirmation before wiping the entire history database. To force it without a prompt (useful for scripts):
history clear --force
Deleting Specific Commands
You can delete specific entries by searching for them first:
history search "sensitive_string"
This outputs the matching entries with their IDs. You can then delete by ID:
history delete --id
You can also delete the last n commands:
history delete -n 5
Advanced Techniques: Secure Deletion and Automation
Standard file deletion (rm) simply unlinks the file from the filesystem; the data remains on the disk until overwritten. If you are on a shared server or handling highly sensitive data (like encryption passphrases), standard deletion might be insufficient Still holds up..
Using shred or wipe
To overwrite the history file