History: Improving the use of the console

Port

In this, my first article for DesdeLinux, I want to make a brief reference to a not very well-known, but very practical console or shell command of any Linux. I mean history.

Almost everyone who uses Linux knows that if we press the up arrow in a terminal, it will show us the command used previously and that if we continue pressing we will go backwards in that list of commands. Turn that !! we will execute the last command used. Well this is thanks to history, but its potential does not end there, since if we execute it directly, we will obtain a list with the last thousand commands:

sebastian:~$ history
1543 ping -c 2 desdelinux.net
1544 clear
1545 sudo apt-get update
1546 uname
1547 history

The list itself will be longer, but a few examples are enough. Since now we can see all the history of commands used together with a number that will allow us to locate the one we want within said list. If you wish you can also do that history show the execution date of the same.

So, writing ! plus the line number, the shell will execute the command that corresponds to that line:

sebastian:~$ !1543
ping -c 2 desdelinux.net
PING desdelinux.net (192.31.186.28) 56(84) bytes of data.

But, we can also execute by letters, since if we put !+un will execute the line "1546", because run the last command that starts with those letters.

sebastian@soporte-mesi01:~$ !un
uname
Linux

But be careful, as this can be risky, since we do not know which command is the last one that begins with those letters and so the best way to recover a previously used command is with the key combination CTRL+r, where a search engine will offer us and, as we write, it will show us the last complete command that matches. So with squeeze enter will run it.

If, on the contrary, we want to list all the times we use a command, we must use grep:
sebastian:~$ history | grep uname
1499 uname -r
1500 uname -a
1546 uname
1549 uname
1550 history | grep uname

With which it will only list the lines where the searched word is, in this case «uname«.


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.   elav said

    Great! Thanks for the tips

  2.   static said

    Congratulations, excellent tip

  3.   kuk said

    This was looking for, thank you !!!

  4.   Victor said

    I did not know the Ctrl + r, I am going to practice it so it remains in the brain database.
    You could also add to the post the command ¨ $ history -c ¨ which, as you know, is used to erase the history, often useful if we don't want to leave clues or if we get too dirty by typing some command wrong.
    Greetings and thanks for the post!!

    1.    thalskart said

      The one to use the -c parameter I knew it, but the truth is I have never used it

  5.   Alberto said

    Very interesting shortcut 'Ctrl + r'. Add that if we have several terminals open, the 'history' command only shows that of that terminal until another new terminal is opened.
    It can also be used with the 'tail' command (to see the latest commands) or with 'less' to see them by page.

    Regards!

  6.   Gabriel said

    They are those little tips that from time to time you forget, but are very useful, thank you 😉

  7.   Vito said

    Awesome.
    Thanks to this, instead of writing rsync -avl -stats -progress / media / data / WEBS / / media / WD / WEBS
    I just had to write! 496, and from now on it will be!
    I always regret that I started late with Linux, and I always feel lucky to have started Linux.
    Thanks for these commands. Surely they are in the man, but I never look at it, just like when I buy a gadget, the last thing I look at is the manual.
    Thank you.
    Yours sincerely.

    1.    Daniel Rojas said

      Vito, keep in mind that the number of the line in the history will change, as more lines are added to it. What I recommend you do to avoid typing the entire line is to add it as an alias to your .bashrc, that way just by typing the name of the alias you have assigned it, the entire line you assigned will be executed.

      Regards!

      1.    Vito said

        Thank you.
        The number was the example of my case. That number was on my record, and with that I tried the tip. I know it changes, and every time I run it, the same command appears with a new number.

        I know about the aliases in the .bashrc and I only use it to turn off the monitors with "pan" instead of the appropriate command (sleep 1 && xset dpms force off), but I don't use aliases much, because as there are many commands I use, then I have the problem of having to remember the alias; And I end up having twice as many problems. Remember the command or remember the command alias.

        This way, it is economical for me to remember "history" or even easier, "history | grep rsync »for when I have to back up my work. The latter is an example of one of the utilities that I see.

        Definitely, I find it super useful, at least in my case, to preserve my brain RAM which is very scarce. So I don't have to remember dozens of commands.

        Thanks for your answer.

        Best regards.

  8.   Diego said

    It is also very useful to execute (without quotes) "history> historia.txt" and thus save the commands that we have executed in a text file.

    1.    thalskart said

      Yes, you can even always filter using grep in the middle, for example with something like
      history | grep uname >pru.txt
      And then, the txt would only save the results with the command «uname» 😉

  9.   Joaquin said

    Best: Ctrl + r

  10.   Joseda said

    Good article and very useful. Thanks for the info 🙂