Sunday, February 14, 2010

The Command History feature

Adapted from: http://www.talug.org/events/20030709/cmdline_history.html

Well there are a ton of linux commands. The general trend is, you want to tweak something, you search, you google, and somehow accomplish it, and then a couple of months later, get in a situation you have to do it all over again, and with no memory how you did it.
Enter the bash history feature....

Type at the terminal.
> history
The list of commands you see are the recent commands you have typed, along with an index number. You can certainly look for the command yourself and then type it character by character by hand, but there are better ways.

First, you must be familiar with the reverse search concept, which is actually pretty intuitive. All search features search your command memory in the reverse order in time, ie the first command you are given is the one you typed latest. The older commands are accessed later. Get it?

There are three ways the history is generally accessed.

The ! operator
>!text
This runs the command whose fist part matches whatever text you have given it. however, remember the text part cannot contain blank spaces..
ex
>gedit abcd
>!g
gedit abcd
You can also give arguments to the commands, like
> !g xxx
gedit abcd xxx

Grep

Remember the index numbers we got along with the commands with the history command? We can also call them with the ! operator.
Just type !num at the terminal, and it will run the relevant indexed command.
>!534
gedit abcd
But its quite a pain to scroll down the history list searching for the required command. The standard command format in such cases is
> history | grep 'what-ever-you-remember-about-the-command'
Remember 'what-ever-remember-about-the-command' should be a contiguous piece of string in the command.
Ex, I am searching for what options I gave mplayer last time, so that it looped the playlist 5 times
>history | grep 'mplayer' | grep 'loop'
I recommend us to stop for a couple of minutes here and take a few deep breaths so fully sink this in, and think of all the situations where we would have loved to do this. B-)

ctrl-r feature
Here whatever you type is searched backwards in time. You can erase what you have written, but remember the direction of search is always the same! That means that once you have reached a command, you can change your query, but the search will be limited to the time earlier that command execution.

Time for an example.
Suppose you don't remember where was the C++ source file you were editing last time. However, chances are, you had compiled it with g++ before leaving. So press r while pressing ctrl, and get the reverse-i search prompt. Now type 'g++'(without the quotes)
(reverse-i-search)`g++': g++ h_def2.o h_def.o -o h_def
erase and type cd
(reverse-i-search)`cd': cd /home/name/Projects/cmm
You get the command you had given the terminal to change to the code directory! The technique might not be useful always, but you get the idea.

Thats it for this time I guess, I hope you learned something useful.

No comments: