Linux and MacOS terminals allow you to view past history of commands. It is useful if you want to repeat any of your past commands, or correct any of your past commands quickly and try it again. However, sometimes you may need to clear terminal history in Linux & MacOS to avoid subsequent users from seeing what you have done, or worked on. In this article, we will look at different ways to clear terminal history in Linux & MacOS.
How to Clear Terminal History in Linux & MacOS
There are two main ways to clear terminal history – either you can clear your current session’s history or you can clear your entire history. You can use these commands for almost every Linux distribution.
1. Clear Current Session’s History
You can easily list your current session’s past commands using history command. You can easily clear it with history -c command.
$ history -c
This command will clear your current session’s history and also prevent your current session’s commands from being written to long term history.
2. Clear Entire Bash History
If you want to clear your entire bash history, that is, including the commands from your past session, then run the following command.
$ rm ~/.bash_history
If you get any error message saying permission denied, then use sudo keyword at the beginning of the above command.
bash_history file keeps a list of all your session commands. If you don’t want to delete your entire bash history, you may open it in bash_history file in a text editor, navigate to the appropriate command and delete.
$ sudo vi ~/.bash_history
However, please be careful before you delete your entire bash history. It might contain useful long commands that you may need in future. Keeping history of commands allows you quickly jump to a specific past command, big or small, without typing much and re-execute it quickly.
Also, please note, that each user in your system has a separate history. So even if you delete history of your present session or entire bash, other users will still be able to see their history. To delete other user’s history you will need to switch to that user first and then execute the above commands as per your requirement.
This can only be done by root or users who can switch users.
On a separate note, if you only want to clear your screen of past commands and not delete any history, you can simply use clear command.
In this article, we have learnt how to clear terminal history. You can use these commands for not only bash shell but other shells also.
Also read:
How to Show Asterisk for Password in Ubuntu
How to Verify PGP Signature of Downloaded Software in Linux
How to Convert RPM to DEB files in Linux
How to Install DEB Files in Ubuntu
How to Provide Sudo Access to User in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.