tips and tricks: history
One thing over looked by most newbies to linux is the history command.
At a command prompt type history.
user@system:~$ history
…
544 ls -lha
545 clear
546 man history
547 history
A list of several hundred commands will be returned. Not only will this let you see what the command you ran a few days ago was, but it will also allow you to quickly run that command again. Lets say I wanted to run a clear again. I could just type “clear”, but if clear was some other command with a ton of switches and paths that could take me a while.
Instead we can just do the following
user@system:~$ !545
The simple ! and number of clear has now executed that command.
You might be thinking that it is to much work to always look up the number for the command you want in the history. Well in that case we can sub in some words.
user@system:~$ !man
The above command has now just executed the last thing in history that started with man. We could have also just typed “!m”.
Other uses of the history can include command counts. For example lets say we did a ls 9 commands ago. We can call the 9th command back from current by using a “!-number”.
user@system:~$ !-9
The above executes the 9th command back in the history.
We can also use history correct typos or speed up changes in the last command.
Let say we enter the command
user@system:~$ ls *.cfg
This then lists all the files in the current dir that end with “.cfg”. What we really wanted to do was show all “.log” files, we can do this with string subs.
user@system:~$ ^cfg^log^
The above command tells the system to execute the last command, but replaces any mention of “cfg” with “log”.
Although these examples are very small if you picture them on larger more complex commands you start to realize the time it could save.
This is only a small part of the time saving features in the history of BASH.
For more information on this you should check out the manpage for it by typing “man history”.
October 6th, 2006 at 1:01 pm
I ran the history command in the system i’m using (unix) and it works fine. However the !number command doesnt work. Strangely there is no entry for “history” in man…
October 9th, 2006 at 7:38 am
The above was all done in Debian GNU/Linux, I am not very up on pure UNIX commands, but from what I have seen and read !365 should work just the same in UNIX as in Linux.
You might want to check out this link as it says it is done in UNIX: http://www.december.com/unix/ref/history.html