2

I was wondering if its possible to execute commands from history that match a certain regex? I know you can do something like

fc 232 248

And it will go into your history and then do revisions 232-248 consecutively.

However, if some of the commands in between are things I do not want to do, is there a way I can specify that? Right now I look up my history and pipe it out to a grep

history | grep checkout

So this will look up all terminal actions I've done with checkout I have done.

232 git checkout master files/file1.txt
234 git checkout master files/file2.txt
248 git checkout master files/file3.txt

It filters out some of the numbers so I only want to execute the ones that have been piped out. rather than everything in between

  • If I am understanding you correctly, you would like to run the results of grep as commands again? – cremefraiche Apr 29 '15 at 1:59
  • I have no idea how to do it... Nevertheless, I think what you are trying to do is kind of dangerous - you never know, so imagine the grep command get some lines that you don't want to run, but as the commands will be automatically run you won't have time to correct the errors... – jimm-cl Apr 30 '15 at 1:42
  • @cremefraiche Yes correct I think that's a good way of putting it although I'm uncertain if I would need to filter out what is spit out from grep and how I would do that. – aug Apr 30 '15 at 8:20
  • @JIMM This is true but I can simply preview what the grep should spit out so I will know what commands I'll be running. – aug Apr 30 '15 at 8:21
1

To execute all commands containing a specific string (e.g. "checkout") or regex (e.g. "checkout.+file\d") from your bash history:

grep -E "string_exp" $HOME/.bash_history | while read line; do eval "$line"; done

You can save this as a function in ~/.bashrc:

#!/bin/bash

all_prev () {
   grep -E "$1" $HOME/.bash_history | while read line; do eval "$line"; done
}

Then in future you can just type:

all_prev checkout

EDIT:

  • To avoid running many duplicate commands as your .bash_history file accumulates these over time, make sure export HISTCONTROL=ignoredups is set in ~/.bashrc if it isn't already.

  • To avoid rerunning the all_prev search containing the search term each time, use regex ^ and $ to specify chars at the start and end of the line.


However, perhaps you should also check out a shell with more powerful history filtering like zsh (or even fish).

  • 1
    This would end up running EVERY command in my history that has the word "checkout" in it. Not exactly what I want. – aug May 1 '15 at 1:24
  • @aug You could grep for whatever you want using the above command. You don't have to just search for a simple string but that's all you provide in your question. The function could be modified to include an '-E' flag so as regex could be passed as well. See change above. – py4on May 1 '15 at 1:31
  • @aug e.g. all_prev "checkout.+file\d" – py4on May 1 '15 at 1:42
  • @aug updated answer to avoid duplicate commands running and specify 'searched-for' command more exactly.. – py4on May 1 '15 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.