- How to Clear Command History in Linux?
- Why Do You Need to Clear Command History?
- How to Display Commands History in Linux?
- How to Clear the Command History of the Current Session?
- How to Clear the Command History of All Terminal Sessions?
- How to Clear the History From Previous Sessions?
- Conclusion
- How to Clear BASH Command Line History in Linux
- How to clear command history in Linux
- Why should we remove the Linux Command line History?
- Removing the Linux Command line history
- Remove a Line from the bash history
- Remove or Clear all commands from the bash history
- Conclusion
- About the author
- Karim Buzdar
- How to clear bash history completely?
- 9 Answers 9
- However,
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.
- Why Do You Need to Clear Command History?
- Display Current History in Linux
- Clear Current Session’s Command History
- Clear the Command History All Terminal Sessions
- Clear the History from Previous Sessions
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:
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:
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:
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.
How to Clear BASH Command Line History in Linux
The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.
In this article, we will show you two simple ways to clear your command-line history on a Linux system.
The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.
For instance if you have typed a command that contained a password in plain-text and you don’t want another system user or an attacker to see this password, you need to delete or clear the history file.
Take a look at the command below, here the user aaronkilik has typed the database server password on the command line.
If you look into th bash history file towards the end, you will see the password typed above in there.
The bash_history file is normally located in a user’s home directory /home/username/.bash_history.
$ ls -l /home/aaronkilik/.bash_history
To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.
To delete or clear all the entries from bash history, use the history command below with the -c option.
Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.
$ cat /dev/null > ~/.bash_history
Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.
You can learn more about the bash history file and useful history commands here: The Power of Linux “History Command” in Bash Shell.
Always remember that all commands you run are recorded in a history file, so do not type plain-text passwords on the command line. If you have questions or thoughts to share with us, make use of the feedback form below.
How to clear command history in Linux
The bash history stores the records of all terminal commands which are executed by a user on the command-line Linux system. Using the history feature, you can easily locate the previously executed commands on your Linux system through the arrow keys navigation.
Why should we remove the Linux Command line History?
Most of the time, we don’t want to reveal the terminal history of other users on your Linux system. For example, if you are giving classes to your students on a Lab computer and you might have taught some harmful commands and are not recommended to use on our system. But most of the students don’t have an idea about the side effects of these critical commands. A curious student may search about the command-line history and test them to check the working of each command. It may crash your system many times. However, you can repair or re-install your system in a quick way. But, it is not a good practice. So, in this case, we want to prefer to clear the command line history after working on it. Especially when you have shared access with your friends or colleagues. We have just elaborated it with just a simple example; there could be other problems through which you want to clear the Linux command line history. In this article, we will give you a demo of how you can clear the command line history in the Linux system.
We have executed all the commands on Ubuntu 20.04 terminal application in this article. Therefore, it is necessary to open the terminal window on your system. Click on the ‘Activities’ located at the left corner on your system’s desktop, and then using the application search bar, you will search the ‘terminal’ keyword. After completing the search, click on the terminal icon and launch it.
Removing the Linux Command line history
If you want to view the history of your terminal command then, type the following command on the terminal:
Normally, the command line history stores in a file named ‘bash_history’. This file you may locate in a particular user’s home directory /home/username/.bash_history. Execute the below-given command to locate the bash_history file:
Note: A root user can observe the command history of all user’s on your system. But, the standard user can only view their own command line history.
Remove a Line from the bash history
If you want to remove just a single line from the bash history file, then you can use the -d option with history command and enter the targeted line number, which you want to remove.
For example, we want to clear a command that contains your password where you have entered a password in a plain text; you can easily find the line number in the history file and clear it by executing the following command:
Remove or Clear all commands from the bash history
You can also clear or delete all entries from the bash history file. So, use the option -c with the history command. You can run the following command on the terminal to clear or delete the all bash history:
Alternatively, by running the following command, you can remove or delete all history of last executed commands in the bash history file permanently.
Conclusion
In this article, we have presented a better understanding of the history command and why we need to clear it. From the above all mentioned command, I hope now can clear the history of your system easily. But, always remember that what you perform on the terminal all commands recorded in a bash history file, so it is recommended that you never use passwords in a plain-text format on the Linux command line. If you have any queries and thoughts related to this article then, please share your ideas with us.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
How to clear bash history completely?
I want to clear all previous commands from the history of my server. I used history -c and it seems all things are cleared but when I ssh to the server, all the commands are still there. How can I clear them permanently?
9 Answers 9
The file ~/.bash_history holds the history.
To clear the bash history completely on the server, open terminal and type
cat /dev/null > ~/.bash_history
Other alternate way is to link ~/.bash_history to /dev/null
However,
One annoying side-effect is that the history entries has a copy in the memory and it will flush back to the file when you log out.
To workaround this, use the following command (worked for me):
cat /dev/null > ~/.bash_history && history -c && exit
you can also put above command in .bashrc & .bash_logout . what it mean when you login u will have clear history & when you logout out your history will be cleared
I tried to do it on a raspberry box while connected via SSH. I added the above command ( cat /dev/null > ~/.bash_history && history -c && exit ) to both .bashrc & .bash_logout as suggested by @Qasim . Now as soon as I connect via SSH the remote host closes the connection (after printing the motd) . Help :/
Goes to show what happens when you blindly do things without reading properly and without understanding the processing and data flow. It also shows the pitfalls of bad communication. What devav2 did was run it one time, and what Qasim should have written is to remove the exit command before adding it in login script.
In every open bash shell (you may have multiple terminals open):
Why: As noted above, history -c empties the file ~/.bash_history . It is important to note that bash shell does not immediately flush history to the bash_history file. So, it is important to (1) flush the history to the file, and (2) clear the history, in all terminals. That’s what the commands above do.
For some reason this does not work on Ubuntu 14.04, probably others. It should but it doesn’t. If you issue the command «history -cw» you can confirm with the up arrow that the history isn’t there anymore, but if you start another terminal window (in Unity desktop) with shift + click on the terminal icon (I have it pinned in the launcher) the commands history are back, no matters how many times you do «history -cw». «cat /dev/null > ~/.bash_history» is the only way that worked for me.
It’s history -c and then history -w, you first clear the history then write the changes. This is easily confirmed by closing the terminal and opening it again, with -w first the commands are there again, with -w last history is effectively cleared.
execute the following commands to clear history forever
This answer would be even more helpful if it explained what these flags do (and therefore why/when they’re the right commands to use).
There’s another much simpler one: running history -c on the terminal prompt and gone are all entries in the bash_history file.
If you read the OP’s question you would know that using history -c is the exact method that led to this question.
Clear the current shell’s history:
When you log out, your current shell’s history is appended to ~/.bash_history, which is a cache of previous shells’ histories, to a maximum number (see HISTFILESIZE in «man bash»).
If you want to remove the history altogether, then you essentially have to empty out ~/.bash_history which many of the above entries have suggested. Such as:
This clears the current shell’s history and then forces the current shell’s history (empty) to overwrite ~/.bash_history. or to be more accurate, it forces it to overwrite HISTFILE (which defaults to ~/.bash_history).