Home Command line utilities How To Clear Command Line History In Linux
clear Command line history in Linux

How To Clear Command Line History In Linux

By sk

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:

$ history

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:

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 -a
$ sudo pacman -Syyu
$ sudo wifi-menu
$ ls
$ <space>sudo ping ostechnix.com

Here <space> indicates a blank space.

Refer the following screenshot:

Avoid saving commands in history list by inserting a blank space before each command

Now, run the 'history' command to view the recent-executed commands.

$ history

Sample output:

 3 clear
 4 export HISTCONTROL=ignorespace
 5 clear
 6 uname -a
 7 sudo pacman -Syyu
 8 sudo wifi-menu
 9 ls
 10 history

clear Command line history 2

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.

$ 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

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.

Recommended read:


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.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

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

Nice tips


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