Why You Might Need to Clear Your Linux Terminal History
The Linux terminal (specifically the Bash shell) automatically records every command you type and saves it to a hidden file called .bash_history in your home directory. This feature is incredibly useful for recalling long commands or seeing what actions you previously performed using the up arrow or the history command.
However, if you accidentally typed a password in plain text, entered sensitive server IP addresses, or simply want to clean up a cluttered history file on a shared machine, you will need to clear your Bash history. Doing this ensures that your past commands cannot be viewed by other users or compromised if the system is breached.
Method 1: Clear the Current Session History in Memory
Bash stores the history of your current, active session in the system’s RAM. It only writes these commands to the .bash_history file when you close the terminal. To clear the history that is currently stored in memory, use the following command:
history -c
Running this command immediately wipes the history list for your current terminal window. If you type the history command right after running this, you will see that the list is now empty.
Important Note: This command does not delete the commands saved in the .bash_history file from previous sessions. It only clears the active memory buffer.
Method 2: Permanently Delete the History File
If you want to completely erase all past commands that have been permanently saved to your disk, you need to clear the contents of the .bash_history file. The safest and most effective way to do this is to truncate the file to zero bytes.
Run the following command in your terminal:
cat /dev/null > ~/.bash_history
This command takes nothing (/dev/null) and overwrites your existing history file with it, effectively emptying it without deleting the file itself or breaking permissions.
The Complete Solution: Clear Memory and Disk History
If you want to ensure that absolutely no trace of your previous commands remains, you must combine both methods. If you only delete the file but leave the memory intact, Bash will simply write the memory contents back to the file when you log out.
To perform a complete wipe, run these three commands in order:
- Clear the history file on the disk:
cat /dev/null > ~/.bash_history - Clear the current session memory:
history -c - Force Bash to write the empty memory to the file (ensuring it is blank):
history -w
After running these steps, your Bash history is completely gone, providing a clean slate for your next session.
How to Prevent a Specific Command from Being Saved
Instead of clearing your entire history, you can prevent Bash from saving individual, sensitive commands in the first place.
By default, most modern Linux distributions are configured to ignore commands that begin with a space character. If you need to type a password or a sensitive API key, simply add a space before the command:
mysql -u root -pMySecretPassword
Because there is a space before mysql, Bash will execute the command but will not record it in the history file or the active memory buffer.