Thursday 12 November 2009

How to tweak your Linux machine’s history command

How to tweak your Linux machine’s history command: "Linux

If you are somewhat familiar with using the Linux or UNIX command line interface it is quite likely that you have used the history command. For those not familiar with it the history command allows you to search through the commands you have already run on your computer’s command line.


An example of its usage is:


# history | grep “kill”


This will search for a command containing the term kill through the history of commands you have run on your computer. This is a basic example of the history command. There are certain limitations in this command. Let’s take a look at how you can tweak it a bit to work more effectively for you. One of the limitations I find bugging me quite often is that if you are running two terminal session at the same time the commands from the first session are erased from the history of commands. Not the smartest way the command should work, I think. Well let’s fix it then.


Log in to your account and launch a shell terminal. In your user’s home directory you should find a file called

.bashrc. If this file does not exist, create it. To open the file use a command like the following, replacing user with your username:

# vim /home/user/.bashrc


Add the following lines to the .bashrc file without any changes whatsoever:


shopt -s histappend

PROMPT_COMMAND=’history -a’


And voila, problem solved. Henceforth all your commands from single sessions as well as simultaneous sessions will be written to the command history file, and no overwriting will happen.


Another useful change I like to make to the history command’s configuration is removing duplicates from the history of commands. This way I get access to a lot more data and get it quicker. Open the .bashrc file once again and add the following lines in a new line:


export HISTCONTROL=”ignoredups”

export HISTIGNORE=”&:ls:[bf]g:exit”


Save the file and exit the text editor. Now the history command will check to see if a command already exists in the log file or not before adding any new information. A last modification I make to the history command’s configuration is adding spell check. Add the line shown below to the file .bashrc to help the history command check for spelling errors in command and suggest a valid command:


shopt -s cdspell


Save and exit. Now your Linux machine will be able to figure out when you type gerp instead of grep.


---
Related Articles at Simple Help:







"

No comments:

Sike's shared items