How to Clear Your Linux Terminal Command History

If you press the “up” arrow on your keyboard while working in a Linux terminal, the shell automatically recalls the last command you executed. By default, the Bash shell quietly records a massive, rolling log of the last 1,000 commands you have typed into the system.

While this is incredibly useful for recalling a complex SSH string you typed three days ago, it is a massive security risk if you accidentally typed a database password directly into the command line in plaintext. Anyone who gains access to your user account can easily read this history file and extract your sensitive data.

To protect yourself, you must completely purge this historical record.

How to Clear the Current Session History

The Bash shell keeps the history of your currently active terminal session floating in live RAM. You must flush this live memory buffer first.

  1. Open your Linux terminal.
  2. Type the following exact command:
    history -c
  3. Press Enter.

The -c flag stands for “clear.” This instantly wipes the active memory buffer. If you press the “up” arrow right now, nothing will happen. However, you are not finished. This only cleared the RAM; the permanent history file saved on your hard drive is still fully intact.

How to Delete the Permanent History File

When you gracefully close a terminal window, Bash automatically takes the live RAM buffer and writes it into a hidden text file located in your user’s home directory called .bash_history.

  1. To eradicate the permanent record, you must delete this hidden file. Type the following command:
    rm ~/.bash_history
  2. Press Enter.

The file is now completely deleted from the hard drive.

The very next time you open a brand new terminal window, Bash will realize the file is missing and will automatically create a brand new, completely blank .bash_history file to start recording fresh, ensuring your previous mistakes are permanently erased.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.