CentOS

How to Clear Terminal History on Ubuntu 22.04?

How to Clear Terminal History on Ubuntu 22.04-1

Linux is mainly operated and managed via command-line utilities. A history is maintained of all the commands executed at the Terminal and is saved in the home directory but is hidden. This history can be viewed by any user who has access to the system. There might be security concerns. Therefore, a user can clear their history of terminal commands so any other user cannot view their activities and re-track their activities.

This article will demonstrate ways of clearing Terminal History on Ubuntu 22.04 LTS, organized as follows:

  • How to View Terminal History in Ubuntu 22.04?
  • How to Clear Terminal History by Using History Command?
  • How to Clear Terminal History by Deleting the Bash History File?
  • How to Clear Terminal HIstory by Modifying/Editing the .bashrc File?

How to View Terminal History in Ubuntu 22.04?

The complete history of the commands executed in the terminal can be viewed by:

$ history

From the above image, it can be seen that a history of 757 commands is saved in the system.

Additionally, the most recent Terminal history can be viewed by adding a number followed by a history command, for example, the following command will display 5 most recently executed commands:

$ history 5

Further, the 15 most recent commands can be viewed by:

$ history | tail -15

Method 1: Clear Terminal History Through History Command

The terminal history can be cleared by executing the “history” command with the “-c” flag as follows:

$ history -c

The “history” command can again be used to verify if the history is cleared:

$ history

From the above image, it can be verified that history is cleared as the output shows only one command. Previously, before the execution of the “history -c” command, there was a history of 761 commands.

Method 2: Clear Terminal History by Deleting the Bash History File

Another alternative method of clearing terminal history is to delete the bash history file (bash_history) by executing the following command:

$ ln -sf /dev/null ~/.bash_history && history -c

It can be seen from the above image, that the terminal history contained 19 files before deleting the bash history file, while after deleting the bash history file, the history contained just 1 command. History is cleared permanently.

Method 3: Clear Terminal History by Modifying/Editing the .bashrc File

Another alternative to clearing terminal history is by editing the “.bashrc” file. Before editing the file, the history can be checked by the following command:

From the above image, it can be seen that 8 commands are saved in the system.

The user can access the “.bashrc” file via a text editor, for example, Nano Text editor by executing the following command:

$ nano .bashrc

Two parameters, i.e., HISTSIZE and HISTFILESIZE needed to be modified in order to stop the history from logging in.

We can search for these parameters in the file and can edit their values to 0.

After the changes are made, we can Save and Exit the file by pressing Ctrl+O and Ctrl+X respectively. In order for the changes to take effect, we can restart the terminal and verify by command below:

It can be seen from the above image, that the terminal history contained 8 files before modifying “bashrc” file, while after deleting the bash history file, the history contained just 1 command. It proves that the terminal history has been cleared permanently.

Conclusion

Terminal History on Ubuntu can be cleared by using the “history” command, by deleting the bash history file, and by editing the .bashrc file. Although the objective of all these methods is the same, their working is different. This article has demonstrated three ways of clearing Terminal History on Ubuntu 22.04 LTS.

Similar Posts