Thursday, 18 June 2015

How to clear terminal history in Linux


Yup you read it correctly. How would you clear terminal history in Linux? Before we see how that is done lets address even better question why would be clean our command line history? I am not going to answer this  question for you :). It is same as asking why would you clear your browser history or launch incognito mode ? Sometime we have too - that's all I have.

 For Linux users who have not heard about this command just type history in your console and you can see your previously typed commands  there. 

I personally use it a lot. Specially to SSH to various linux servers (who would remember IP address of each). I simply do 

  • history | grep ssh
or even better option would be
  • (Ctrl + R) Start typing your command.
 But let's stick to our agenda for today. We will see how to clear our terminal history.

Clear terminal history in Linux

To clear the entire history you will have to execute command
  • history -c   OR
  •  history -cw

Note : This may just clear history for current terminal and may not be reflected to in other terminals. This did not work for me on my Ubuntu 14.04 machine. 

Deleting the source

Let's go a step ahead. All the bash history is stored in file 
  • ~/.bash_history
Go ahead and read that file. You can see your history getting saved there. So now you know the source. You can simply delete the history from this file.

Note : ~ notation is shortcut for current user. History is stored in a file called bash_history which is in root folder of the user.

To clear this file simply use following command
  • >~/.bash_history
Note : You will have to relaunch terminal to see the changes.

More details on your history File

Now you know where your history file is. Root folder of your user with file name - bash_history. But this is stored in your environment variable $HISTFILE
  • echo $HISTFILE
For me I get following result :

aniket@aniket-Compaq-610:~$ echo $HISTFILE

So to disable logging history what you can do is use following commands -

  • unset HISTFILE OR
You can also do
  • Turn Off : set +o history
  • Turn On : set -o history

Related Links

t> UA-39527780-1 back to top