Why should we clear Command line history?
There are chances that you don't want to expose the Command line history of your Linux system. Say for example, as a Linux trainer/tutor you might have taught some commands to your students in the Lab computer. Those commands might be harmful and are not supposed to use. But the students are not aware of the consequences 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 you allow that? Of course, we can re-install or repair the system in couple minutes. But, I think it is completely unnecessary if you are 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 could be many other reasons to clear Linux command line history. In this brief tutorial, we will be see 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.
1. Clear entire Command line history using 'history' command
As you might know, the 'history' command will display the last executed commands.
To view the previously executed commands, run:
To clear the history, just run any one of the following commands:
$ history -c
$ history -cw
The above commands will clear the command line history.
2. Avoid saving commands in history list by inserting a blank space before each command
Using this method, you can eliminate a specific command getting saved in the history list. 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”.
Edit your ~/.bashrc file:
$ nano ~/.bashrc
Add the following line at the end:
Save and close the file. Run the following command to take effect the changes.
$ source ~/.bashrc
Or, simply log out and log back in to apply the changes.
To verify it, run a few commands. And put a blank space in-front of any command. I put a blank space in last command.
$ uname -a
$ sudo pacman -Syyu
$ sudo wifi-menu
$ <space>sudo ping ostechnix.com
Here <space> indicates 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
Sometimes you might want to delete some particular commands from history list, instead of clearing the entire history.
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 last executed commands.
To clear or delete a particular command from the history, use -d flag with the command prefix number:
$ history -d <number>
Here <number> represents the line number of each command.
For example, to delete the "time" command with line number "5" from the history, run:
$ history -d 5
Now, display the command line history to see if the command is removed 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, you can 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
All of the above methods will only remove the commands from the active session i.e currently opened terminal. 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, you must remove the contents of .bash_history file. This file holds the list of commands that we run in the terminal. You can either clear this file manually each time or setup a cron job to clear it at regular intervals.
To manually clear the contents of this file, run:
$ cat /dev/null > ~/.bash_history
Like I said, this will delete the entire history. The next day 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 automatically cleared at 8 pm everyday.
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 specific command often. Would you type the same 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 some lengthy and complex commands in a previous session. In such cases, you can use "history" command to retrieve the previously executed commands.
- How To Avoid Duplicate Entries In Bash History In Linux
- How To Clear A Specific Command From Bash History In Linux
- How To Display Bash History Without Line Numbers
- 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.