2

How does one clear all the entered commands in terminal? I am not talking about the clearcommand, which merely clears the screen. I want to clear all the commands that I have entered , so the window would completely clean. I've tried:

1. Quitting Terminal
2. Restarting my computer
3. The clear command

I am thinking there must be some way, because I've probably entered in about 50 million commands, and there are only 505 in my history.

3
  • The command history is stored in a file, that's why it is persisting. I'm sorry I don't know where that file is in macs but on *nix systems it is usually in ~/ as a dot-file, named depending on what shell you're using.
    – deed02392
    Mar 31, 2012 at 18:37
  • In bash, $HISTFILESIZE determines how many commands are kept, typically 500, with older ones being replaced by newer ones. That's why you don't see all commands you've entered. Cameron already said how to clean the history.
    – lupincho
    Apr 1, 2012 at 16:46
  • @deed02392, the name is similar on mac, e.g. for bash, it is ~/.bash_history
    – Cameron
    Apr 1, 2012 at 18:23

4 Answers 4

8

This should work (at least for bash):

$ history -c

Followed by quitting the terminal.

EDIT: Chris Page notes in the comments that quitting the terminal is unnecessary, as there is also a command to clear scrollback in most terminals. On Mac, e.g., this is under View > Clear Scrollback or ⌘K

3
  • Quitting Terminal or closing the terminal window/tab should be completely unnecessary to clear the shell command history.
    – Chris Page
    Apr 1, 2012 at 11:06
  • The part about quitting the terminal was because the poster wanted to know how to make the window "completely clean". Just clearing the history will only make the history go away, but you can still see what was executed during that login just by scrolling up in most terminal emulators. AFAIK, the only way to get rid of that information is to quit the terminal.
    – Cameron
    Apr 1, 2012 at 18:20
  • View > Clear Scrollback erases the scrollback text.
    – Chris Page
    Apr 6, 2012 at 2:50
7

For Mac Terminal user

delete commands history with the following command $ echo '' > ~/.bash_history

For Mac zsh user

delete zsh command history with the following command $ echo '' > ~/.zsh_history

2
  • 1
    This is the only solution on this page that worked for me... although it did not apply until after I quit and relaunched the terminal. In other words, use the above command, don't test scrollback or it will re-register it in history. Instead quit terminal, relaunch terminal, and then test he history is successfully deleted. One side effect: the above command will be the sole remaining command in the scrollback history.
    – ThisClark
    Feb 6, 2023 at 16:58
  • I have tried everything, and only echo '' > ~/.zsh_history worked for me for zsh. Thanks so much for sharing @vikbert Of course. Totally makes sense! Feb 16 at 20:37
3

If previous methods does not work, here is another one (build in command):

zsh terminal

$ history -p (aka fc -p)

See: https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html#Shell-Builtin-Commands

3
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 5, 2023 at 17:19
  • This only temporarily clears on window instance. When you open up again, the history reappears. The previous answer is the correct one for macOS. And zsh does not recognize history -p. Feb 16 at 20:34
  • @MariaCampbell, have you tried the above with a single terminal instance? For the moment I can't test this.
    – LucianDex
    Feb 18 at 17:21
1

You can clear all your history on Mac from the edit menu of the terminal, edit > clear scrollback or just enter 'alt + command + k'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .