history -c command in Ubuntu

What we will discuss here would be the opposite to what we did through history -w command in previous article. By default, all the commands we type in terminal are stored in BASH history file after we log out. And, there are times when we don’t want our BASH history file to be written i.e. we want to clear the command history of current session. For such a scenario, we use history -c command in Ubuntu.

Just to give an overview, all the commands we type in BASH shell are stored in cache. And, once we log out these commands are written to BASH history file. If you want to know your BASH history file issue the following command –

echo $HISTFILE

Furthermore, if we just want to access only a select few previously stored commands through shell. In that case, non-essential commands in BASH history file would create a hindrance. If there is too many of those then it would either make us type the commands itself or use more command-line options to have an access to previously entered commands. This entirely defeats the purpose of having those commands stored in BASH history file.

So, to remove those non-essential commands we use history -c

history -c command in Ubuntu

Let’s understand what we have discussed above with the help of an example. Use the following commands in terminal –

ls
ls -l

These are two different ways to list files and directories. Now, we can either logout to write the BASH history file or just issue the following –

history -w

If you want to know more the history -w command then, refer to first paragrah in the article. history -w mainly writes commands we have typed in current session to the BASH history file without the need for logging out.

Check for BASH history file –

cat /home/$USER/.bash_history

Now, this time we see how history -c would work. Again issue the following two commands in terminal –

ls -a
ls -al

And, to clear the commands from cache –

history -c

This time even if you logout or use history -w command, it won’t write the BASH history file. Check the file again –

cat /home/$USER/.bash_history

In conclusion, we have covered history -c command in Ubuntu.

Similar Posts