How to clear commands history of the terminal?

Hi guys! I looked at some tutorials on the subject, but I can’t delete the commands from the terminal’s history. I even accessed “man history” in the terminal, which reports being the command “history clear”, but this gives an error, as well as “history -c” .

See the man page: man history:

history clear
              Erase the history list.  The current keep  limit  is  retained.   The  history
              event numbers are reset.

Okay, but I want to erase the indexes, and especially the commands themselves, and the one you showed isn’t doing that either.

Oh, you did mention you tried that in the first post. The useless, contradictory screenshot (you used clean there, not clear) distracted me, I’ve removed it.

I’m not sure if what you want is possible. Poke the internet, the answer must be there somewhere. :wink:

I’ll have to look even further then! :grinning_face_with_smiling_eyes: Some tutorials I found suggest that “history -c” is common for any linux, so I hoped it would work! :smirk:

Maybe you just want to remove
~/.bash_history
?

1 Like

doesn’t work either friend.

The way to clear out the history of the running shell ─ but this will not touch the history of newly started shells, nor of other already running shells ─ is… :arrow_down:

history -c

However, if you want to completely clean out the history of one particular running shell and of any new shells that will be started, the best approach would be… :arrow_down:

history -c && > ~/.bash_history

Explaining the syntax, the first command ─ which you already know ─ empties the command history of the running shell. The && is a logical “AND” operator, meaning that the second command will be executed only if the first command was successful. And the second command, in turn, truncates the history file, ~/.bash_history.

Now, the above is for GNU Bash. If you are instead running zsh, then I don’t know where it keeps its history file or what name it has ─ it’ll probably be something easily identifiable, though ─ but more importantly, I seem to have read that zsh shares its history across all running shells, probably through some common memory page.

Anyway, the command I’ve shown you above will clear your command history in the running shell window and will truncate the history file. Other running shell windows will however still have their command history too, so you’d need to run… :arrow_down:

history -c

… in every open shell environment as well.

Lastly, here’s a tip on account of shell history… If you are running GNU Bash, then add the following to your ~/.bash_profile:arrow_down:

HISTCONTROL=ignoreboth:erasedups
HISTSIZE=1000
HISTIGNORE='[ \t]*'
export HISTCONTROL HISTSIZE HISTIGNORE

You’ll have to log out and back in for the changes to take effect, but what the above does is…

  1. It will make the shell history ignore two successive duplicate commands; and…
  2. It will make the shell ignore commands that start with one or multiple spaces.

In other words, if you want to use a command that you don’t want to include in the shell history, simply type a space or a tab first and then type the command.

As a last trick ─ one that I do not use myself, but it’s a possibility ─ if you want your shell history to have only a certain number of commands in it but no other ones, you can “freeze” it in place by making it immutable… :arrow_down:

chattr +i ~/.bash_history

Do however bear in mind that this also means that the file can no longer be renamed or deleted.

Please do not post screenshots of terminal output. Instead, copy and paste it here and use the Preformatted text </> button.

3 Likes

You’re running zsh. Therefore my advice regarding bash is not applicable. Please see the zsh manual on account of…

  • its history command;
  • its environment variables; and
  • what file it stores the command history in.
man zsh
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.