Tuesday, May 31, 2011

Hiding/Clearing history commands

Hiding certain commands from history

At times you might want to Hide some commands executed by you. This is needless to explain, we might have to do this for various reasons. But I suggest not to hide anything intentionally from history @ work :)

Solution:

Add the below entry in .bash_profile and run “source .bash_profile” or re-login to the server.  After this whatever command you want to hide, just execute it by typing a SPACE infront. The history command will not remember any commands that is preceded by space infront.

export HISTCONTROL=ignorespace


Explained with an example here:

[adevaraju@host02 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin

export PATH
export HISTCONTROL=ignorespace

[adevaraju@host02 ~]$ source .bash_profile
[adevaraju@host02 ~]$ touch file1
[adevaraju@host02 ~]$ echo "Some text" > file1
[adevaraju@host02 ~]$ echo "Some more text" >> file1
[adevaraju@host02 ~]$ ls -l file1
-rw-r--r-- 1 adevaraju domain users 25 Nov 30 08:10 file1
[adevaraju@host02 ~]$ touch -t 1111111111 file1
[adevaraju@host02 ~]$ ls -lt file1
-rw-r--r-- 1 adevaraju domain users 25 Nov 11  2011 file1
[adevaraju@host02 ~]$  rm file1                             ß Executing the ‘rm’ command with space infront
[adevaraju@host02 ~]$ history | tail -10
   27  history
   28  cat .bash_profile
   29  source .bash_profile
   30  touch file1
   31  echo "Some text" > file1
   32  echo "Some more text" >> file1
   33  ls -l file1
   34  touch -t 1111111111 file1
   35  ls -lt file1
   36  history | tail -10
[adevaraju@host02 ~]$

You can note the command ‘rm file1’ is not displayed in ‘history’ output. Since it had space in front, it was ignored by history command.

Clearing the entire history

Sometime you may want to clear all the previous history, but want to keep the history moving forward. Just execute history with –c option. Everything will go off.
[adevaraju@host02 ~]$ history -c
[adevaraju@host02 ~]$ history
    1  history
[adevaraju@host02 ~]$

1 comment:

  1. hi..Im college student, this article is very informative, thanks for sharing :)

    ReplyDelete