How to Clear All History in Linux/Ubuntu Terminal or Bash Permanently

How to clear all history in linux/ubuntu terminal or bash permanently?

You can clear your bash history like this:

history -cw

How to delete a single command from history in bash

So turns out it's solved by deleting the command from ~/bash_history

Unlimited Bash History

Set HISTSIZE and HISTFILESIZE in .bashrc to an empty string:

HISTSIZE= 
HISTFILESIZE=

In bash 4.3 and later you can also use HISTSIZE=-1 HISTFILESIZE=-1:

n.  Setting HISTSIZE to a value less than zero causes the history list to be
unlimited (setting it 0 zero disables the history list).

o. Setting HISTFILESIZE to a value less than zero causes the history file size
to be unlimited (setting it to 0 causes the history file to be truncated
to zero size).

bash --version to check your bash version.

Clear a terminal screen for real

Use the following command to do a clear screen instead of merely adding new lines ...

printf "\033c"

yes that's a 'printf' on the bash prompt.

You will probably want to define an alias though...

alias cls='printf "\033c"'

Explanation

\033 == \x1B == 27 == ESC

So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.

Edit

Here are a few other ways of doing it...

printf "\ec" #\e is ESC in bash
echo -en "\ec" #thanks @Jonathon Reinhart.
# -e Enable interpretation of of backslash escapes
# -n Do not output a new line

KDE

The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...

clear && echo -en "\e[3J"

Or perhaps use the following alias on KDE...

alias cls='clear && echo -en "\e[3J"'

I got the scroll-back clearing command from here.

How to: Unlimited Bash/shell history?

Add this to your .bashrc (Linux) or .bash_profile (MacOS):

export HISTFILESIZE=
export HISTSIZE=

There you go, unlimited history. Currently I have 27000 entries :)

From man bash:

If HISTFILESIZE is not set, no truncation is performed.

That means .bash_history is never truncated

Also the same seems to apply to HISTSIZE, although I couldn't find that documented.

Another neat feature I'm going to try is this:

If the HISTTIMEFORMAT variable is set, time stamps are written to the history file, marked with the history comment character, so they may be preserved across shell sessions, like the following:

export HISTTIMEFORMAT="%F %T "

Let me know if you have tried that already...

Preserve bash history in multiple terminal windows

Add the following to your ~/.bashrc:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Execute a command without keeping it in history

Start your command with a space and it won't be included in the history.

Be aware that this does require the environment variable $HISTCONTROL to be set.

  • Check that the following command returns ignorespace or
    ignoreboth:

     echo $HISTCONTROL
  • To add the environment variable if missing, the following line can be added to the Bash profile. E.g., to file %HOME/.bashrc.

     export HISTCONTROL=ignorespace

After sourcing the profile again, space-prefixed commands will not be written to $HISTFILE.

Clear history in Mongo shell

I think this is what you are looking for How to really clear the terminal.
reset command will do the job.

Or you can try command + K button to clear scrollback buffer in OS X as mentioned here.



Related Topics



Leave a reply



Submit