How to Clear Bash History on Linux

If you’ve ever used the command line on a Linux machine, chances are you’ve got a long history of commands logged. If you want to clear this history, there are a few simple steps that can help you do just that. This article will explain how to clear your history and create a new one.

Why clear Bash history?

In many cases, it is important to keep your bash history clear for security reasons. This is e.g. useful when building a server image to roll out multiple virtual machines.

Where is the Bash history data stored?

The first step in clearing your bash history is to find where it is stored. Usually, this is found in the ~/.bash_history file. To view this file and the commands stored within it, use the following command:

cat ~/.bash_history | more

Clearing Your Existing Bash History

To clear the Bash History on Linux, use this combination of 3 commands:

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

These commands will work on most, if not all, Linux Distributions. I've used them on Debian, Ubuntu, CentOS, AlmaLinux, and Rocky Linux.

What do these commands do?

  • The 'cat' command empties the .bash_history file of the currently logged in user.
  • The 'history -c' command cleans the history of the current user session.
  • The 'exit' command quits the current session.
Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Pete at: 2018-04-30 16:59:30

Don't forget that placing a space prior to any command will avoid adding it into the history at all.

It might be a good idea to require at least 5 characters in a command to be added as well.  Nobody needs 50 'ls' or 'mv' commands saved, right?  man bash has all the details.