How to Clear the Terminal History in Linux?

A Linux system is mostly managed through various command line utilities. When the commands are executed, the history of the executed commands is stored in a file (usually named “.bash_history”), which is hidden and is available inside your Home directory. In certain moments, the history of the terminal needs to be cleared so that other users may not look into the activities. In this tutorial, methods of deleting the history of the terminal will be explored. This tutorial will also explain the below-mentioned sections:

Let’s start the tutorial!

How to Display the History in Linux?

The “history” command is the most used to display the history of the commands run in the terminal. It can be used as follows:

$ history

Another method to display the History of the terminal is by displaying the contents of the “bash_history” file located in the root directory by using the “cat” command:

$ cat ~/.bash_history

The history has been displayed on the screen.

Now, let’s head over to clearing terminal history.

What are the Methods to Clear the History of Linux?

There are two different approaches by which we can clear the history of the terminal:

  • Using the history Command
  • Using the bash History File

Both methods are explained below in detail.

Method 1: Using the history Command Utility

The first method to clear the entire history of the terminal is by using the “history” command with its “c” option. The “c” option is used to clear the history of the terminal and can be run:

$ history -c

After clearing the history by using executing the above command, again display the history of the terminal using the command:

$ history

The history of the terminal has been cleared.

Method 2: Using the Bash History File in Linux

Another method of clearing the history of the terminal is by deleting the bash file, which stores the history of the executed commands in the terminal. This file is located in the root directory, and its contents can be displayed using the command:

$ cat ~/.bash_history

We can see that the history is still available in the bash_history file, and the content can be deleted by using the command:

$ cat /dev/null > ~/.bash_history

With the execution of the above command, the history of the terminal is cleared permanently and can be verified by again using the command:

$ cat ~/.bash_history

How to Clear the History at Each Logout?

There is another recommended method to delete the history of the executed commands before logout, and this can be done by using the command:

$ cat /dev/null > ~/.bash_logout

The history has been deleted.

How to Delete a Specific Command From History?

If you want to clear any specific line from the history of the terminal, then you can use the “-d” option with the history command utility. For example, we display the history of the terminal again by using the command:

$ history

If we want to delete line 1 from the above history, then run the command:

$ history -d 1

Confirm the execution of the above command by displaying the history:

$ history

The command “echo history -c” has been deleted from the history.

That’s all about this tutorial!

Conclusion

To clear the terminal history in Linux, either run the “history -c” command or clear the history permanently by executing the “cat /dev/null > ~/.bash_history” command. In this write-up, both the above-mentioned methods have been explained in detail and apply to all distributions of Linux.