DEV Community

Asim Dahal
Asim Dahal

Posted on

Using grep and history command to find previous commands in Linux

You input command in your linux terminal which is very long and hard to remember. It is needed to input the command again to the terminal. You find it hard to search for the command that is needed. You keep pressing up arrow again and again until the required command is found. Time is wasted during this process.

This is where history command comes in. Go to your linux terminal and type the command history. You will find the list of commands you previously used like this.
Use of history command

In the above image you can see all the previous commands you have used along with their respective numbers. Now let's assume you have to use the command git clone https://github.com/asimdahall/wethanks/ which is number 56. Now you can execute the command by typing

asims-MacBook-Pro:~ !56
Enter fullscreen mode Exit fullscreen mode

which will execute the command on number 56.
Now let'say we have to search the previous command that we have used. For that, we can combine grep with the history command. Let's take a case here. I have to search for the command

xcrun simctl shutdown all

which I have used before but I don't remember the full command and only remember xcrun. So for searching the command, we can combine the history and grep command like this:

asims-MacBook-Pro:~ asim$ history|grep xcrun
Enter fullscreen mode Exit fullscreen mode

after that you will get the following output

  422  xcrun simctl shutdown all 
  452  xcrun simctl shutdown all 
  454  xcrun simctl shutdown all 
  502  history|grep xcrun

Enter fullscreen mode Exit fullscreen mode

you get all the commands where xcrun command is used with their respective numbers. Now you can execute the command like this:

asims-MacBook-Pro:~ asim$ !422
Enter fullscreen mode Exit fullscreen mode

which will then execute the needed command.

I am sorry for the bad grammar. Feedbacks and suggestions are kindly welcome. Thank you.

You can follow me on Twitter

Top comments (4)

Collapse
 
ferricoxide profile image
Thomas H Jones II

If you know the command you're trying to repeat, then !<starting-string> is great. Similarly, if you know the command you're trying to repeat, but you need to modify it, !<starting-string>:s/<SEARCH>/<REPLACE> or even !<starting-string>:gs/<SEARCH>/<REPLACE> are freaking awesome.

Note: This type of advanced command-history usage works in BASH and CSH-derived shells. Dunno about other shell-families.

Collapse
 
darkes profile image
Victor Darkes

Big fan of using history, I'd be lost if I didn't have it. My personal way of accessing it on Macs is by using Ctrl + R and that let's you do pretty much the same thing as the grep method and you don't have to do the second line to execute the expression. Give it a try!

Collapse
 
programmist profile image
Tony Childs

If your .bash_history file is large (as mine is), piping the output of the history command to grep can be very slow. For example, my current history file is over 100k lines, and it takes 10+ seconds (on my machine) for the command to complete.

A faster method is to simply grep the file directly, which takes under 1 second:

grep xcrun ~/.bash_history
Enter fullscreen mode Exit fullscreen mode

Or, even better, set up a function to do this:

# hg - History grepping function
hg () {
    grep "$1" ~/.bash_history
}
Enter fullscreen mode Exit fullscreen mode

then:

hg xcrun
Enter fullscreen mode Exit fullscreen mode

Or if you use Ripgrep (As I do):

hg () {
    rg "$1" ~/.bash_history
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ayoubbougue profile image
ayoub bougue

super useful,thanks for sharing