How to get the best out of the history command in GNU/Linux

Posted by suvi under Linux, Tricks

Anybody who has used the command line
extensively to navigate, understand and configure GNU/Linux will know
that in the course of a few months’ work it is possible to build up an
extensive history of used commands. This necessitates some pro-active
management to get the best out of it. Here are some tips to make the
most of the history command.

Please note, from the outset, that command history is only saved in
interactive shells and does not, therefore, work with shell scripts.

By default, the shell Bash is designed to retain the last five hundred commands you entered. If you want to see them, just open ~/.bash_history and scroll through it. Or, by simply typing history on a command line the terminal will list them.

If you know there will be a lot of output, then it makes sense to pipe it to less: history | less; with less,
you can see the command history one screen at a time (by pressing the
spacebar) or one line at a time (by pressing the down arrow).

If you’d rather not even be bothered with piping with less, just specify the number of commands you want (if you are pretty sure it was a quite recent one): history 25 or history | tail to output the last ten commands.

Helpfully, if you want to rerun a command and can’t quite remember its format, the history facility allows you to simply step through them one at a time by pressing the up arrow (or Ctrl + P) continuously from your command prompt until you find the one you are looking for; you can also use the down arrow (or Ctrl + N) to go to the “next” command. When you find the command you want to run, hit the return key to run it.

If you think you know the command but can’t quite recall the command exactly, then you can pipe it with grep and the first few letters of that command: history | grep -i. The i should be followed by the first letter(s) of the command you are attempting to find. Again, use less to step through them and or combine it with the down arrow: history | grep -i | less.

Read more at FreeSoftwareMagazine 

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word