How to Clear Bash History in Linux?

Clearing the bash history in Linux is done to remove the record of previously entered commands. This can be useful for various reasons, including maintaining privacy and security by removing potentially sensitive commands from the history, freeing up memory and disk space used by the history file, and many more. 

This article will explain various methods to clear bash history in Linux. 

Method 1: Clears the Entire History File 

The “history” command with the “c” option clears the entire history file and is a quick way to remove all records of previously entered commands in the terminal:

$ history -c

The output shows that the previous history of bash has been deleted.

Method 2: Delete a Specific Line From the Bash History

The “history -d [line number]” command allows you to delete a specific line from the history. For instance, the command will delete the “third” line from the history: 

$ history -d 3

The outcome of the above command deletes the 3rd line from the bash history.

Method 3: Clear the History File Manually

Users can clear the history file manually by opening the “.bash_history” file with a text editor and removing the contents. For instance, open the “.bash_history” file using the “nano” editor as below: 

$ sudo nano ~/.bash_history

Users can select all the content and press the “Delete” key to clear the bash history.

Method 4: Clear History of the Current Session Only

To clear the history of the current session, execute the “unset HISTFILE” command before you exit your shell. This will prevent the current session’s history from being saved to the history file:

$ unset HISTFILE

The above execution won’t be available to view when you next start a new shell session.

Method 5: Securely Delete the History File

The “shred” command securely deletes the specified file by overwriting it. It securely deletes the file as “.bash_history”, preventing anyone from recovering the deleted commands:

$ shred ~/.bash_history

The output shows that the “.bash_history” file has been successfully deleted.

Conclusion

Linux offers the “history” command with the “c” option to clear the bash history in Linux. It removes all the records of previously typed commands from the terminal. Additionally, users can delete a specific line from the history via the “history -d [line number]” command, clear the current session history using the “unset HISTFILE” command, and securely delete the history file via “shred ~/.bash_history” command. This article has explained various methods to clear the bash history in Linux.

Join 10 Select Businesses Growing with Us! 500M+ Views Across Our Blogs & YouTube Channels – Be Part of Our Success. Click here to learn more. Click here if you are interested. ×