We continue with the interesting and useful commands that we sometimes forget to use, having them implicit in our system. In this case we will use one that allows us to play a bit with our history.
Let's take a simple example, open a terminal and put:
$ nano /etc/sudoers
They will be able to realize that we cannot edit the file if we are not administrators. So let's use sudo, but in order not to repeat the command we put:
$ sudo !!
And this will repeat:
$ sudo nano /etc/sudoers
That is, the command !! will repeat the command we ran earlier in the terminal. We can also execute another command that is not previous knowing its number in the history.
Open a terminal and put:
$ history
In my case it comes out something like this:
[code] 495 cd Desktop /496 ls
497 wget -c http://cinnamon.linuxmint.com/tmp/blog/119/classic.png
498 cd
499 cvlc Music / Jamendo / The \ Patinettes \ - \ Bliss \ - \ 2011.06.03 /
500 cvlc Music / Rock /
[/ Code]
If I run the command !! the previous command is executed, which in this case would be:
$ cvlc Música/Rock/
But if you want to run for example the command:
$ wget -c http://cinnamon.linuxmint.com/tmp/blog/119/classic.png
I would just have to put:
$ !497
497 is the number in front of the command. Simple right?
Wow, I didn't know this, very good info, thanks.
This saves me from pressing the up arrow | start. Thanks for the info.
I find it very useful, this in conjunction with the Ctrl + R for command search makes the experience in the ttys even more pleasant.
I usually use a alias to filter:
alias h='history | egrep -i'
In fact now that I think about it, maybe it's better to add a function like this to the .bashrc:
h () {
# Función para listar comandos del historial
HISTERROR="Se puede utilizar como máximo un parámetro."
if [ $# -eq 0 ] ; then
history | less
elsif [ $# -eq 1 ] ; then
history | egrep -i $1 | less
else
echo $HISTERROR
fi
}
In this way, just use h to list all commands in history, or h parameter to list the commands that match the parameter (which by the way, allows regular expressions).
I use the directionals. Then I try it.