Terms of Use For FixedByVonnie

By proceeding to access fixedByVonnie.com, you expressly acknowledge, and agree to, all of the following:

fixedByVonnie.com is a personal website and blog owned by Security Plus Pro LLC, which is being presented for informational purposes only. The views on this website are solely those of the website owner (and not those of any employer or of any professional associations affiliated with the website owner).  Any views expressed in this website and any information presented on this website, or in any of its blog entries, should not be relied on for any purpose whatsoever other than as the personal opinions of the website owner.  The website owner expressly disclaims any and all liability for any information presented on this site.  The owner of this website and its blog posts shall not be held liable, and shall be held harmless, for any errors or omissions in any information or representations contained in this website, or in any of its blog entries.  The website owner also expressly disclaims any liability for the current or future availability of any such information. The website owner makes no representations as to the accuracy or completeness of any information on this website or which may be found by following any link on this website. The website owner shall not be held liable for any losses, injuries, damages, claims, or causes of action, from the display or use of any information on this website or in any of its blog entries. If you use the information on this website, or on any of its blog entries, you do so solely at your own risk.

Here's the best way to clear your command history from the terminal in Mac OS X and Linux - fixedByVonnie

Here’s the best 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.

~/.bash_history

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

Ahh, 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…

cat

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

Chucked.

Discarded.

Vaporized.

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.

The Bottom 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

Connect with Vonnie on Twitter

Posted in Apple, Linux, Mac OS X 10.9 Mavericks Tagged with: ,