Sunday 16 February 2014

Linux History Tricks

Showing timestamp using HISTTIMEFORMAT

# export HISTTIMEFORMAT='%F %T '

Repeat previous commands

# !! ---------> execute the last run command
 

# !-4 --------> executed the 4th command executed
                from backward
 

# !c  --------> execute the command that you run last, 
                which start with the specific word c
 

# ctrl+P -----> will display the previous command
 

# ctrl+r -----> for reverse search the commands
 

# history | more,less ----> check the command that you 
               looking for and note its line number also
 

141  08:55:33 2012-02-02 clear ( her line number is 141) to execute this command run
 

    # !141
 

# cd !^   --------> !^ will get the next argument after cd command

Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile
 

HISTSIZE=450
HISTFILESIZE=450

Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile to store the history command in .commandline_warrior file instead of .bash_history file.


# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

Eliminate the repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.


# export HISTCONTROL=ignoredups --- after giving this if u type any comands repeately it shows only once in commandline


Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.


# export HISTCONTROL=erasedups  ------> previous commands will not go, only commands that come after this will have its effect.

Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below.


# export HISTCONTROL=ignorespace  ------> and when u execute a particular command, put a space before the command and that command will not showing in the history.

example:- # export HISTCONTROL=ignorespace
 

#  service httpd stop [Note that there is a space at the beginning of service,to ignore this command from history]

Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.
 

# export HISTSIZE=0

Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history.


Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

# export HISTIGNORE="pwd:ls:ls -ltr:"

after this history will not record pwd, ls and ls -ltr

No comments:

Post a Comment