Clear Terminal History in Linux

Are you looking to clear terminal history in Linux? Maybe clear bash history, erase session history, or permanently delete typed commands from the terminal? These are common questions for new Linux users. This guide explains how terminal history works and shows you how to view, manage, and completely clear command history in Linux.

Linux does not have a default terminal history password. Command history is stored locally and can be viewed or cleared at any time.

clear terminal history
Remove, delete, or clear bash command history from the terminal

About Terminal History in Linux

By default, the bash shell saves recently typed terminal commands to a hidden file named .bash_history in your home directory. The file is typically written when the shell session exits. This history allows you to reuse previous commands by pressing the up or down arrow keys.

Most systems store between 500 and several thousand commands, depending on the values of HISTSIZE and HISTFILESIZE. While convenient, command history can expose sensitive information and may need to be cleared for privacy or security reasons.

This guide focuses on bash, the default shell on most Linux distributions. Similar concepts apply to other shells such as zsh, which is also covered here.

Why Clearing Terminal History Matters

Terminal history may contain sensitive or private data, including:

  • Passwords accidentally typed into commands
  • File paths revealing system structure
  • Administrative commands showing system changes

On shared systems, Live USB environments with persistence, or temporary machines, clearing history helps prevent others from viewing past activity.

Understanding How Bash History Works

Bash stores history in two places:

  • In memory: Commands from the current terminal session
  • On disk: Commands saved to ~/.bash_history when the shell exits

Clearing only the in-memory history may not be enough. Commands already written to the history file on disk can be restored in new sessions unless the file is overwritten.

History Command Cheat Sheet

Command Description
history Display command history for the current shell
history -c Clear history stored in memory only
history -w Write current memory history to the history file
history -cw Clear memory history and overwrite the history file
cat /dev/null > ~/.bash_history Erase the history file on disk
unset HISTFILE Disable history logging entirely

How to View Terminal History

Most Linux distributions use bash as the default shell, where terminal history is stored in ~/.bash_history. Some systems, such as newer Ubuntu releases or customized setups, may use other shells like zsh. While the commands below focus on bash, the general concepts apply across most Linux distributions and shells.

  1. Display command history
    Open a terminal (Ctrl + Alt + T) and run:
    history
  2. Search history interactively
    Press Ctrl + R and begin typing to search previous commands.
  3. View root user history
    Switch to root:
    sudo su

    Then run:

    history

How to Clear Terminal History in Linux

There are multiple ways to clear terminal history in Linux. You can remove only the commands from the current session, permanently delete saved commands, or prevent the shell from recording history entirely to protect privacy on shared or portable systems.

1. Clear Session History Only

This clears commands stored in memory for the active terminal session:

history -c

Note: This does not remove commands already written to .bash_history.

2. Permanently Delete Bash History

This method clears memory and overwrites the history file for most users:

history -c && history -w

For a more aggressive wipe of the history file:

cat /dev/null > ~/.bash_history && history -c

This ensures no commands remain on disk.

3. Disable Bash History Logging

To prevent bash from saving any future command history:

  1. Edit your bash configuration file:
    nano ~/.bashrc
  2. Add the following line:
    unset HISTFILE
  3. Save the file and reload it:
    source ~/.bashrc

Clearing zsh History

On systems using zsh, history is stored in ~/.zsh_history. To clear it:

cat /dev/null > ~/.zsh_history && history -c

Unlike bash, zsh often writes history to disk immediately, so both the file and memory should be cleared.

Frequently Asked Questions

Can I delete specific commands instead of everything?

Yes. You can edit the history file directly:

nano ~/.bash_history

Remove the lines you want to delete, then save and exit the editor.

Why does history sometimes reappear after clearing?

Bash writes command history when the shell exits. If the history file is not overwritten or cleared properly, previously removed commands may return.

Does this apply to Live Linux USB systems?

Yes. If persistence is enabled on a Live Linux USB, terminal history may be saved across reboots. Clearing history before shutting down is recommended.

Does clearing history affect other users?

No. Each user’s terminal history is stored separately in their home directory. Clearing your own history does not affect other accounts.

Final Thoughts

Clearing terminal history in Linux is a simple but important task for privacy and security. Whether you want to erase session history, permanently delete command logs, or disable history tracking entirely, these methods give you full control over your terminal activity.

For more Linux tutorials, see our guide on How to Mount an NTFS Partition in Linux.