http://www.linuxnewsblog.com - Your Daily News

Saturday, April 18, 2009

Fine tune your Linux command line History


Introduction

History is one of the Linux commands I use the most, maybe I am too lazy to write a command again, so I keep looking in the history log for commands even as short as:

sleep 5 && s2disk

Which hibernate my Laptop after five seconds, so, continuing with my series of posts about History, I will write this time about histcontrol variable.

This environment variable, will control how history stores the commands you enter in the shell.

Options

There are four options at which this variable could be set, and also the unset position, the four possible set positions are:

  • ignorespace: Which make that any command entered in the shell starting with a space will not be stored in the history log.
  • ignoredups: When set to this value, history will only save the first occurrence of a given command, and not all repetitive ones, i.e., if you enter

    free

    And the using the arrows, run it again, and again, and again, history will only save the first one, and not all others.

  • ignoreboth: Is a combination of the two above
  • erasedups: Will make History to erase the previous occurrence of a command when it is entered again.

How to use it

You just need to set the variable in your environment using:

export HISTCONTROL=ignoredups

or put a line like the one below in your .bashrc file in your home directory.

export HISTCONTROL=erasedups

Examples

ignorespace

export HISTCONTROL=ignorespace

Output:

ggarron@gentoo ~/Desktop $ history | tail -6  510  history  511  export HISTCONTROL=ignorespace  512  history | tail -6  513  clear  514  history | tail -6  515  history | tail -6ggarron@gentoo ~/Desktop $  lsarchlinux-2009.02-2-ftp-i686.img.torrentggarron@gentoo ~/Desktop $ history | tail -6  511  export HISTCONTROL=ignorespace  512  history | tail -6  513  clear  514  history | tail -6  515  history | tail -6  516  history | tail -6ggarron@gentoo ~/Desktop $

You may see that the ls command was not logged, because it has a space preceding it.

ignoredups

export HISTCONTROL=ignordups

Output:

ggarron@gentoo ~/Desktop $ export HISTCONTROL=ignoredupsggarron@gentoo ~/Desktop $ history | tail -6  514  history | tail -6  515  history | tail -6  516  history | tail -6  517  clear  518  export HISTCONTROL=ignoredups  519  history | tail -6ggarron@gentoo ~/Desktop $ lsLinux-101-Hacks.pdf  archlinux-2009.02-2-ftp-i686.img.torrentggarron@gentoo ~/Desktop $ lsLinux-101-Hacks.pdf  archlinux-2009.02-2-ftp-i686.img.torrentggarron@gentoo ~/Desktop $ lsarchlinux-2009.02-2-ftp-i686.img.torrentggarron@gentoo ~/Desktop $ history | tail -6  516  history | tail -6  517  clear  518  export HISTCONTROL=ignoredups  519  history | tail -6  520  ls  521  history | tail -6ggarron@gentoo ~/Desktop $

You may see that only one occurrence of ls was logged.

erasedups

export HISTCONTROL=erasedups

Output:

ggarron@gentoo ~/Desktop $ export HISTCONTROL=erasedupsggarron@gentoo ~/Desktop $ history | tail -6  513  clear  514  export HISTCONTROL=ignoredups  515  ls  516  clear  517  export HISTCONTROL=erasedups  518  history | tail -6ggarron@gentoo ~/Desktop $ lsarchlinux-2009.02-2-ftp-i686.img.torrentggarron@gentoo ~/Desktop $ history | tail -6  471  clear  472  export HISTCONTROL=ignoredups  473  clear  474  export HISTCONTROL=erasedups  475  ls  476  history | tail -6ggarron@gentoo ~/Desktop $

You may see that the ls command between clear and export HISTCONTROL=ignoredups with the number 515, was deleted, and now we have the 475, you may also note that a lot of occurrences of ls have been deleted as my log went from 518 commands recorded to 476!

Previous posts about history

[CRTL+R] Search in your last used commands
Small tip - Call a command from history
history, a command to display your last used commands
Navigate on Linux command shell history



Read More...

[Source: Linux Blogs2k - Posted by FreeAutoBlogger]

0 Comments: