Home Linux Commands How To Clear Command Line History In Linux

How To Clear Command Line History In Linux

By sk
Published: Last Updated on 22,369 Views

In Bash, all the commands that you run in the Terminal are stored in a text file named in .bash_history in your home directory. We can use the history command to display a list of the commands entered since you started the session. For some reason, you may want to delete all or a specific command from the Bash history. In this brief guide, let us learn how to clear command line history in Linux with examples.

Before getting to the topic, first let us see why should we clear the command line history and why shouldn't we do it in some cases.

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, if you are 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 may not fully aware of the consequences of such critical commands.

A curious student might search the command line history, and wonder what does those commands will do, and start to test them one by one. The result? He/she might inadvertently break the system.

Would you allow that? Of course, we can re-install or repair the system in couple minutes. However, 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.

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.

As I said earlier, the commands that you run on the Terminal will be retained in the ~/.bash_history file. You can retrieve the previously executed commands by pressing the UP arrow in the keyboard.

When you hit the UP, the list of commands that you run recently will appear. Just keep hitting the UP 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.

Sometimes you can'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.

So if you already cleared the Bash history, you can access the previously entered commands. This is why you should decide whether you want to clear the command line history or not.

Without further ado, let us see how to clear command line history in Linux.

Clear Command line history in Linux

There are many ways to clear Command line history. Here I have listed five ways.

1. Clear entire Command line history using history command

As stated earlier, the history command will display the last executed commands.

To view the previously executed commands in Linux, run:

$ history

To clear the history, just run any one of the following commands:

$ history -c

Or

$ 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 space bar 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:

export HISTCONTROL=ignorespace

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.

$ clear
$ uname -mrs
$ sudo pacman -Syyu
$ sudo wifi-menu
$ ls
$ <space>sudo ping ostechnix.com

Here <space> indicates a blank space.

Refer the following screenshot:

Put a blank space before a command to avoid saving it in history in Linux
Put a blank space before a command to avoid saving it in history in Linux

Now, run the history command to view the recently executed commands:

$ history

Sample output:

 3 clear
 4 export HISTCONTROL=ignorespace
 5 clear
 6 uname -mrs
 7 sudo pacman -Syyu
 8 sudo wifi-menu
 9 ls
 10 history
clear Command line history in Linux
clear Command line history in Linux

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.

Let me show you an example. I am going to run the following commands in the Terminal.

$ clear
$ cal
$ time
$ ls
$ uname -r

Then, display the history command output using command:

$ history

sample output:

 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.

$ history

Sample output:

 3 clear
 4 cal
 5 ls
 6 uname -r
 7 history
 8 history -d 5
 9 history
Clear or delete specific commands from history
Clear or delete specific commands from 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:

unset HISTFILE

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 Bash history in all sessions, you must remove the contents of .bash_history file. This file contains 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 time 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.

Conclusion

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 in Linux. If you know any other method to do it, feel free to let us know in the comment section below.

You May Also Like

1 comment

Binh Thanh Nguyen November 9, 2017 - 11:03 am

Nice tips

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More