Tuesday , March 5 2024
Home / Linux / Way to clear your command history from the terminal in Mac OS X and Linux

Way to clear your command history from the terminal in Mac OS X and Linux

After typing a few commands in the terminal you may eventually want to flush it out. A quick (but insufficient way) to clear the command history on your Mac or Linux box is to type:

history -c

This purges all the commands for the current session stashed in a file called: ~/.bash_history.

That’s good but I’m going to show you a better way to kill the history for all sessions that way if you have multiple terminal windows open you’ll make sure your history is deleted from all of them.

But first let’s explore the .bash_history file.

You can check out your most recent commands by typing

tail ~/.bash_history

Bash viewing the full history

or to see the full shebang just type:

less ~/.bash_history

Filenames preceded by a dot are hidden so ~/.bash_history isn’t visible in a basic directory listing unless you explicitly attempt to view it.

You can list all the files (including hidden files) on your Mac or Linux computer by typing the following command in the terminal:

ls -la

Note the a after the l. This tells Linux to list all files.

.bash_history and a few of his bash friends show up.

Viewing Bash History in Linux and MacOS X

I ran the pwd (Print Working Directory) command in the third line from the bottom of the screenshot to show you that .bash_history is actually stored in my home folder.


That’s what the little tilde before the forward slash means: it’s just an alias for my user directory.
When history fails

Sometimes you might notice that history -c doesn’t actually clear all your history.

If you have multiple terminal windows open, history -c only clears the history from the currently active terminal window. All the other terminals retain the history and if you log off and then back on again all your history will be sitting there waiting for you.

Therefore we need to type an additional command: history -w to clear the history from all terminal sessions.

As a shortcut to typing the word history twice you can usually compress the command like so:

history -cw

Mac OS X history -cw command to clear terminal history
That did the trick on my Mac!

“Null” you know how to do it

But that’s not all.

There’s one other way to kill your bash history. This last trick overwrites your command history with empty content:

cat /dev/null > ~/.bash_history && history -cw && exit

I know the command looks monolithic so let me break it down:

First you’ll notice the command begins with my admiration for felines…


Actually, in the Linux world, cat has nothing to do with those selfish furry critters who only care about themselves and hiss at you when you try to pet them.

cat is short for catinate because it’s often used to chain files and redirect text inputs.

In this case, we’re saying:

cat /dev/null > .bash_history

Which means we want to dump the contents of a file called /dev/null to our command history file, .bash_history.

Now why would we do that?

/dev/null is an interesting guy.

He’s perhaps the most enigmatic file on your Linux or Mac because he’s a black hole.

Any data written to /dev/null get’s




I honestly don’t know what it’s used for but programmers have their purposes.

The bottom line is that we’re overwriting .bash_history with an empty file then flushing the command history and exiting.

Those && ampersands are like chains that link together separate commands on a single line.

When it’s time to dump the command history on your Mac or Linux boxes you have three options:

history -c
history -cw
cat /dev/null >~/.bash_history && history -cw && exit

That last one works great when you’re done for the day because it exits the session.

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

How to Change Hostname in centos

$ vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=newHostName and: $ vi /etc/hosts newHostName localhost localhost.localdomain localhost4 …

Leave a Reply

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