How to Clear Command Line History

Dec 17, 2012 - 7 Comments

Clear command line command history

If you’re a frequent command line user, you’re likely to have found the history command to be quite useful before, whether it’s for discovering your most frequently used commands, dumping history and searching it to find specific past commands, listing all defaults commands used, or whatever else. That said, there are some obvious situations where you may want to remove that command line history list entirely, be it for privacy or security purposes.

This article will show you how to clear command line history in a terminal. This trick works to clear command history on a Mac OS or Linux machine, or even Windows with Ubuntu shell.

How to Clear Command Line History Completely

To clear out command history, you just need to attach a -c flag to the familiar history command, looking like so:

history -c

That command when executed will manually wipe the .bash_history file, or if you’re using a different shell it should wipe that too (zsh, tcsh, bash, etc).

Obviously that file could be targeted directly with rm as well, but it’s best to stick with the history command for a variety of reasons.

If you wish to verify the command worked, type ‘history’ again as usual, and you’ll discover the only command listed is “history -c”.

The brief video below demonstrates this entire process of clearing command history, the video shows the procedure on Mac OS with the Terminal app, but everything would be the same in other operating systems that support clearing of command line history as well.

This should work the same at any bash shell, zsh shell, tcsh, and most other shells, regardless of whether the command line is in Mac OS X or linux, or even Windows linux shell (but not a DOS prompt).

Thanks to Ado for the tip idea. If you have any other helpful tips or tricks regarding clearing command history from a Terminal, share them in the comments!

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

7 Comments

» Comments RSS Feed

  1. pkt says:

    Should work with other shells as well, but .sh_history, .tsch_history etc are easy to remove anyway.

  2. bv7 says:

    Thanks I didn’t know this command It’s good :)

  3. Peter says:

    Keeping the history file can be useful:

    Bash has an instant reverse search function. To search through old commands, press cmd-R and then start typing any part of a command previously used. Quite handy…

  4. @Peter says:

    I think you mean ctrl-R.

  5. steve says:

    if you want exit without saving the history session:

    $ kill -9 $$

  6. nolan says:

    how about if you dont want history saved at all and tired of typing history -c. HISTSIZE=0 will not save anything to history.

  7. roxb says:

    bash clean history

    alias cleanH=’history -c;echo “”> ~/.bash_history’

    add alias in ~/.bashrc

    cya

  8. JPS says:

    @Peter: On OSX, CMD+R, followed by the name of a command, followed by RETURN, gives you a summary of the usage of commands based on that word (not a history of how it’s been used AS a command by a particular user).

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac