+3 votes
587 views
How to clear the history of the terminal or console in Linux

in Guides by (552k points)
reopened | 587 views

1 Answer

+4 votes
Best answer

Where is the history of commands executed in the Linux terminal stored?
How to clear the history of the terminal or console in Linux.
Conclusions and clarifications.
Remove a specific command from the command history.
Delete the complete history of the terminal.
Automatically delete history when closing the terminal session.

The Linux operating system has demonstrated its great potential many times when we use the terminal. Its use is increasingly friendly and anyone with a little patience can use the terminal with ease. What we should know is that the use of the terminal in Linux implies the registration of the commands executed in the so-called history of the terminal or console . This involves certain aspects that we must know when typing commands and what better than a tutorial on how to clear the history of the terminal or console in Linux .

image

Many of you may be wondering why delete the terminal's command history , since its fundamental mission is precisely to act as a record of what we have done. We do not doubt the utility of having the history, but sometimes it can contain sensitive information such as passwords or private file paths. In the end, the reason for cleaning the history is nothing more than protecting our security or hiding our activity .

Many will be thinking that the simplest solution is to locate and delete the history file . This thought is not very misguided, but we will stop to detail the process of cleaning the bash command history in Linux.

Where is the history of commands executed in the Linux terminal stored?

First, we have to know that each user of the system has a separate command history . This in principle gives some security, but in a shared environment there may always be some security flaw and privilege escalation. The file that stores the command history is usually located in the user's personal folder and with the name .bash_history .

To find out what your user's personal folder is, simply run the echo $HOME command. On my system, my personal folder is /home/zeokat and the history file is located at /home/zeokat/.bash_history .

image

How to clear the history of the terminal or console in Linux.

The last 500 commands executed are stored in the .bash_history file. Simply run the history command to see the complete and numbered list of the executed commands. If we only want to see the last 15 history commands, we can execute the history 15 command.

image

Remove a specific command from the command history.

Sometimes we don't want to delete the entire history and give it up, but we just want to delete a specific command from the history . In this case, we have god options:

  • Edit the .bash_history file with some text editor such as Nano and delete the desired lines.
  • We can use the history -d N command, where the «N» parameter is the corresponding number of the command we want to delete . For example, if we want to eliminate the command that occupies position 120, we would execute the history -d 120 command and then save the changes with the history -w command.

Delete the complete history of the terminal.

To delete the history of the console we will use the history -c command and then history -w to save the changes.

We see that once the history is clean we need to save the changes, so this last command would always remain. Similarly, when closing the terminal session we can execute the exit command, so it would also be registered.

For this reason, if we want to delete the history completely and leave it clean without any command , we will have to execute the command:

  • Clear entire history : echo "" > ~/.bash_history && history -c
  • Log out of the console and do not register the exit command : echo "" > ~/.bash_history && history -c && exit

Automatically delete history when closing the terminal session.

If we want to delete the history automatically when closing the terminal session , we will have to edit the .bash_logout file. First we will use a text editor, in our case we will use Nano with the nano ~/.bash_logout command.

image

Now we add at the beginning of the file the echo "" > ~/.bash_history && history -c line echo "" > ~/.bash_history && history -c and save the changes. Now, every time we end a terminal session with the exit command, the history will be cleaned automatically .

Conclusions and clarifications.

We see that when we delete the history we must be very careful not to leave any trace. Remember that an expert can always find mechanisms to know if someone has used the console, for example by examining the date and time of modification of the .bash_history file.

There are also many users who fall into the error of executing the history - c command and think they have already deleted the history. Actually they have only deleted it from memory, you have to run history -w so that the cleanup is final and is reflected in the .bash_history file.


by (3.5m points)

Related questions

+5 votes
1 answer
+5 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 215 views
+5 votes
1 answer
+5 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users