How to Clear Command History in Linux?

In Linux, the history command is a utility that allows you to view a list of previously entered commands in the terminal. It is a useful utility to overlook the commands and then reuse them easily.

While using too many terminal commands, Linux administrators recommend clearing the terminal’s history regularly. In this post, we will list the possible ways to clear the command history in Linux.

Let’s start the article with the importance of clear command history.

Why Do You Need to Clear Command History?

There are several reasons you might want to clear the command history in Linux:

  • Privacy: The command history is stored in a file on your computer, and it can be accessed by anyone who has access to that file. Clearing the command history can help to protect your privacy by preventing others from seeing the commands you have typed.
  • Security: If you have typed sensitive information, such as passwords or confidential data, into the terminal, clearing the command history can help to protect that information from being accessed by others.
  • Convenience: The command history can become cluttered over time, making it difficult to find specific commands. Clearing the command history can help clean up the list and make it easier to find the commands you need.
  • Debugging: If you are having problems with your terminal or with a specific command, clearing the command history can help to resolve those issues.

How to Display Commands History in Linux?

To display the current history in Linux, the “history” command is utilized in the terminal below:

$ history

The output displays all the previous history in this terminal.

How to Clear the Command History of the Current Session?

For clearing the command history in Linux, utilize the history command followed by the “-c” option. This will clear the current session’s command history.

To clear the command history, you can use the following command:

$ history -c

Note that this will only clear the command history for the current terminal session.

How to Clear the Command History of All Terminal Sessions?

The “.bash_history” file contains the commands run on all terminal sessions. To clear the command history for all terminal sessions, run the below script:

$ cat /dev/null > ~/.bash_history

This will overwrite the “bash_history” file with an empty file.

How to Clear the History From Previous Sessions?

Use the history command with the -w option for writing the current history to the .bash_history file and overwrite it with the current session’s commands:

$ history -w

This can be useful if you want to preserve the current session’s command history but clear the history from previous sessions.

That is how clear the command history

Conclusion

Linux offers the “history” utility with the “-c” option to clear the command history. It clears the current sessions. Additionally, the “-w” option is helpful to clear all previous sessions of command history by maintaining the current session history. It is a useful way to manage and maintain your terminal environment. This article has demonstrated all multiple methods to clear the command history in Linux.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×