How to Clear the Bash History in Ubuntu Linux

When you use the terminal in Ubuntu Linux, the Bash shell automatically records every command you type and saves it to a hidden history file. This is incredibly useful for recalling complex commands using the “up” arrow key or searching past actions with Ctrl + R. However, if you have accidentally typed a sensitive password in plain text or need to clear your tracks on a shared server, you must know how to properly wipe this history.

Understanding the .bash_history File

Your command history is stored in a hidden text file named .bash_history, located in your user’s home directory (~/.bash_history). By default, this file stores the last 1000 commands. However, Bash also maintains a “live” history of commands in the system’s active memory (RAM) for the current terminal session. To truly clear your history, you must clear both the file and the active memory.

How to Clear the Current Session History

If you want to clear the commands you have typed during your current, active terminal session before they are written to the permanent file, you can use the built-in history command.

  1. Open your terminal window.
  2. Type the following command and press Enter: history -c

The -c flag stands for “clear.” This wipes the active history list from your current session’s memory. If you press the “up” arrow key immediately after running this, you will notice no previous commands appear.

How to Delete the Permanent History File

Clearing the current session does not remove commands from previous days that are already saved to your hard drive. To wipe everything completely, you must target the history file.

  1. Open the terminal.
  2. Type the following command to overwrite the permanent file with an empty file: cat /dev/null > ~/.bash_history
  3. Alternatively, you can simply delete the file using: rm ~/.bash_history

If you choose to delete the file, Bash will automatically generate a fresh, empty .bash_history file the next time you open a terminal.

The Ultimate Wipe Command

If you have typed a sensitive password and want to be absolutely certain your history is wiped from both the live session and the permanent disk file immediately, combine the commands into one execution:

history -c && history -w

The -c clears the active memory, and the -w flag forces Bash to immediately write this new “empty” state to the .bash_history file on disk, overwriting whatever was there previously.

Get the best tech tips delivered straight to your inbox.

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