Open In App

How to Clear the Terminal History in Linux

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In today’s digital era, privacy and security are the main aspects that every user should follow. As on the Linux operating system, we as the user are administrators, perform different types of command execution on the terminal. Once the command is been executed, its history is been saved in the file which is hidden from us. So if this file is been recovered, or if this history isn’t been deleted, then it can be harmful if our system is been accessed by any unauthorized person. So we need to frequently clear the terminal history using some comprehensive methods. So in this article, we will see different types of step-by-step methods through which we can delete our terminal history completely.

How to Clear the Terminal History in Linux?

For clearing the terminal history from our Linux operating system, we can follow 4 different comprehensive methods that will be helpful for us to completely clear the terminal history. Below are the methods which we will be discussing in this article:

So let’s see each of the methods with its practical usage to clear the terminal history in Linux:

Using history Command to Clear the Terminal History in Linux

In this method, we will be clearing the terminal history by using the inbuilt history command. So follow the below steps to clear the terminal history using the history command:

Step 1: Using the keyboard shortcut “CRTL + ALT + T“, open the terminal and enter the below command to see the existing history of the terminal.

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

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). So follow the below-stated steps to clear the terminal by editing the Bash History File.

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

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)

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

How to Clear the Terminal History in Linux – FAQs

How can I clear my entire terminal history in Linux?

To clear the entire terminal history, you can use the history command with the -c option. This option clears the history file, removing all previously executed commands from the history.

history -c

After running this command, your entire command history will be cleared.

Is there a way to remove a specific command from the terminal history?

Yes, you can selectively delete a specific command from the terminal history using the `history` command. First, use `history` to list all commands with their corresponding numbers. Identify the command number you want to remove and then use `history -d` followed by the command number.

history
history -d <command_number>

Replace `<command_number>` with the actual number of the command you wish to delete.

How do I clear the terminal screen without removing the command history?

The `clear` command is used to clear the terminal screen, but it doesn’t affect the command history. Simply enter:

clear

This will provide you with a clean terminal screen while preserving your command history.

Can I prevent specific commands from being recorded in the terminal history?

Yes, you can use the `HISTIGNORE` environment variable to specify patterns of commands that should be ignored in the history.

For example, to ignore commands like ls, cd, and clear, you can use:

export HISTIGNORE="ls:cd:clear"

This will prevent these commands from being recorded in the history.

Where is the terminal history stored, and can I manually edit or clear it?

The terminal history is typically stored in the` ~/.bash_history` file in the user’s home directory. You can manually edit this file using a text editor if needed. To clear the history, you can use the `echo` command to overwrite the file:

echo > ~/.bash_history

This command empties the history file, effectively clearing the terminal history.

Conclusion

In conclusion, clearing the terminal history of our Linux system is an important task for maintaining the privacy and security of our command line interface. As in this article, we have seen different types of methods through which we can clear our current session history. We have seen some inbuilt comments and also the techniques to clear the actual history file as per our compatibility. We can use any optimated and clear our history without any problem. This will benefit our privacy and security against any potential loss of historical command data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads