Open In App

How to Clear the Terminal History in Linux

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Linux Terminal stores every command you execute in a history file like .bash_history for Bash or .zsh_history for Zsh, making it convenient to access previous commands. However, this history can become a privacy or security issue, especially when dealing with sensitive tasks or working on shared systems.

In this article, we’ll explore four effective methods to clear terminal history in Linux.

Method 1: Clear the History Using the 'history' Command

The history command provides a straightforward way to view and clear your terminal history -

Step 1: Open the terminal using the shortcut CTRL + ALT + T, then type the following to view the current history:

history
Checking Existing History
Checking Existing History

Step 2: After opening the terminal, to clear the entire command history for the current session, we need to enter the following command:

history -c
Using history command with -c to clear
Using history command with -c to clear

Step 3: To verify whether the history is been cleared or not, we can once again use the history command. After executing the command, you can see the difference between Step 1 and the current step output.

history 
Terminal History Cleared
Terminal History Cleared

Method 2: Clearing the Terminal History by editing the Bash History File

In this method, we will be clearing the terminal history by editing the Bash history file (.bash_history).

Step 1: Open the terminal and enter the below command to see the existing contents of the Bash History File. This will return the overall existing history of the terminal.

vim ~/.bash_history
Opening bash history file in Vim
Opening bash history file in Vim

Step 2: Now, press the 'gg' to move to the beginning of the file.

Use gg key to move to beginning of the file
Use the gg key to move to the beginning of the file

Step 3: Next, press the "Shift + d" command twice to delete all the lines in the file. Save the file by using the command ":wq".

Using shortcut to delete all lines
Using a shortcut to delete all lines

Step 4: To check whether the history is been cleared or not, we can use the history command in the terminal and see the results,

history
Terminal History Cleared
Terminal History Cleared

Method 3: Using the 'unset HISTFILE' Command to Clear the Terminal History in Linux

This method, allows us to temporarily disable the saving of command history for our current session by unsetting the 'HISTFILE' environment variable. Follow the below steps:

Step 1: Open the terminal and execute the below command to disable command history for the current session.

unset HISTFILE
Using unset command to disable storing of command history
Using the unset command to disable storing of command history

Step 2: Now, after executing the above command the commands that will be further executed will not be stored in the history file. So to verify that, we can run many commands.

echo '"This command will not be saved"
Executing sample command
Executing sample command

Step 3: To verify whether the history is been saved or not, we can use the history command. You can see that the command that we executed in Step 2 is not saved in the history file

history
Terminal History cleared
Terminal History cleared (not saved)

Method 4: Using the 'reset' Command to Clear the Terminal History in Linux

In this method, we will be using the reset builtin command to clear the terminal history. Follow the below steps to clear the terminal history using the reset command.

Step 1: Open the terminal by using the shortcut and execute the below command to check the existing history.

history
Checking Existing History
Checking Existing History

Step 2: Now, run the below command to clear the display and command history from the terminal.

reset
Screenshot-(914)-min
Using the reset command to reset terminal

Step 3: To verify whether the terminal is reset or not, we can once again use the reset command in the terminal and see the difference between Step 1 and the current step.

history
Terminal History Cleared
Terminal History Cleared

Conclusion

Clearing the terminal history in Linux is an important step to protect your privacy and security, especially when working with sensitive data or shared systems. In this guide, we covered four effective methods: using the history command, editing the .bash_history file, unsetting the HISTFILE variable, and using the reset command.

Each method has its unique purpose, from completely wiping history to temporarily disabling it for specific sessions. By following the steps outlined, you can ensure your command history stays private and your terminal environment remains secure and clutter-free.


Next Article

Similar Reads