How to clear Command line history in Linux
Why should we clear Command line history?
There are chances that you don’t want to expose the Command line history in your Linux system. Say for example, if you are a Linux trainer and you might have taught some commands to your students in the Lab computer. These commands are harmful and are not supposed to run. But the new Linux users and students might not aware of those critical commands. A curious student may search the command line history, wonder what does those commands do, and start to test them one by one. The result? He/she might break the system. Would allow that? Of course, we can re-install or repair the system in couple minutes. But, I think it is completely unnecessary if you be bit careful. So, clearing Command line history from time to time, especially in a shared computer, is a good practice. It is just an example. There might be many other reasons to clear Linux command line history.
In this brief tutorial, we will be seeing how to clear Command line history in Linux system.
Clear Command line history in Linux
There are many ways to clear Command line history. Here I have given five methods which will help you to clear the command line history.
1. Clear entire Command line history using ‘history’ command
As you might know, the ‘history’ command will display the recently-executed commands.
To view the recently-executed commands, open Terminal and ENTER:
To clear the history, just run any one of the following commands:
$ history -c
$ history -cw
The above commands will clear the recently-executed command in the Terminal.
2. Clear a Command line history using by inserting a blank space before each command
Using this method, you can clear or delete any particular command from the history. Just put a blank space (Hit spacebar from the keyboard) before any command. The command will not be recorded in history.
To do that, you must set HISTCONTROL environment variable’s value as “ignorespace” or “ignoreboth”.
Now, Let us see an example. I ran the following commands. And, I put a blank space in-front of the last command.
$ uname -a
$ sudo pacman -Syyu
$ sudo wifi-menu
$ <space>sudo ping ostechnix.com
Here <space> describes a blank space.
Refer the following screenshot:
Now, run the ‘history’ command to view the recent-executed commands.
3 clear 4 export HISTCONTROL=ignorespace 5 clear 6 uname -a 7 sudo pacman -Syyu 8 sudo wifi-menu 9 ls 10 history
As you see in the above output, the “sudo ping ostechnix.com” command is not displayed in the history command output.
3. Clear or delete specific commands from history
This is another useful way to delete a particular command. Sometimes you might want to delete some critical commands, which causes damage to the system, from the history. This method will be helpful if you don’t want to clear the entire history.
Let us see an example. I ran the following commands.
$ uname -r
Then, display the history command output using command:
3 clear 4 cal 5 time 6 ls 7 uname -r 8 history
As you see in the above output, the history command displays the recently-executed commands.
To clear or delete a particular command from the history, do:
$ history -d <number>
Here <number> represents the line number of each command. For example, let us delete the “time” command with line number “5” from the history.
To do so, run:
$ history -d 5
Now, display the command line history to see if the command is remove or not.
3 clear 4 cal 5 ls 6 uname -r 7 history 8 history -d 5 9 history
As you see, the “time” command has been removed. Similarly, you can delete any command from the history list.
4. Clear Command line history automatically at logout
Instead of manually clearing history each time, follow the below steps to automatically clear it at logout.
To do so, edit ~/.bashrc file:
$ vi ~/.bashrc
Add the following line:
Save and close the file. Now, the history will be cleared after you logout from the session.
5. Delete Command line history permanently
One problem of using all above methods is they will only remove the commands from the active session i.e currently opened Terminal. That means if you have multiple Terminals running different commands, the ‘history’ command will only delete the history from where you run this command. The commands from other Terminals will still be available.
To remove all commands from history in all sessions (Terminals), you must remove the contents of .bash_history file. As you might know already, this file holds the list of commands that we run in the Terminal over time.
You can either clear this file manually each time or setup a cron job to clear it at regular intervals.
To clear the contents of this file manually each time, run:
$ cat /dev/null > ~/.bash_history
Like I said, this will delete the entire history of all sessions. The next day, however, this file will start to record the history. You should run this command everyday to clear the contents of this file.
Alternatively, you can schedule this task at regular interval using cron jobs.
To do so, run:
$ crontab -e
Add the following commands:
00 20 * * * cat /dev/null > ~/.bash_history
Save and exit file. The history will be cleared everyday at 8 pm automatically. Please note that it will only clear the history of currently-logged-in user’s history, not the all user’s.
Why shouldn’t we clear Command line history?
Clearing Command line history is a good practice, however you must not clear history in some cases. Say for example, you want to repeat a command often. Would you type the command every time? That’s not necessary. Like I said already, the commands that you run on the Terminal will be retained in the ~/.bash_history file. Just hit the upper arrow in the keyboard, the list commands that you run recently will appear. Keep hitting the upper arrow key until you find the command that you want to run. Once you find the command that you want to run, just hit ENTER to execute. That’s it. You don’t have to type the entire command every time. Also, sometimes you won’t remember exactly how you executed a command in a previous session. In such cases, history command will come in help.
- 5 Ways To Repeat Your Last Command In Linux
- Bookmark The Linux Commands For Easier Repeated Invocation
- How To Save Commands And Use Them On Demand
In this tutorial, you have learned why should we clear the command line history, and why you shouldn’t do it in some cases. Also, you have learned the different ways to clear Command line history. If you know any other method to do it, feel free to let us know in the comment section below.
That’s all for now. If you find this tutorial useful, share it on your social networks, so that other users can also benefit from it. I will be here soon with another another interesting article, until then stay tuned with OSTechNix.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!