With the terminal: Repeat the previous command with !!

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?


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Mauritius said

    Wow, I didn't know this, very good info, thanks.

  2.   sieg84 said

    This saves me from pressing the up arrow | start. Thanks for the info.

  3.   Maxwell said

    I find it very useful, this in conjunction with the Ctrl + R for command search makes the experience in the ttys even more pleasant.

  4.   Hugo said

    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).

  5.   Lucas Matthias said

    I use the directionals. Then I try it.