Search This Blog


Friday, April 25, 2008

Fast way to execute sequential commands from the command history

When I had to navigate to a subdirectory in a deep hierarchy, I found myself do a lot of cd's.
$ cd /home/peter
$ cd level1
$ cd level2
$ cd level3
$ cd level4
$ pwd

If I ever need to revisit that level4 subdirectory, re-running those same commands in the same order from the command history can be a real chore. You have to keep finding your way back in history to execute the next command.

bash has a short cut, Cntl-O (as in Control-Oh), that is your friend here.

Simply go back to the first command in the series (cd /home/peter).
Hit Cntrl-O (instead of Enter), and it will run that command, and automatically display the next command for you (cd level1).

You can do one of several things at this point:

  • You can hit Cntl-O to run the currently displayed command, and display the next one (cd level2).
  • You can hit Enter to run the current command, and terminate the sequence (next command is NOT displayed).
  • You can hit Cntrl-C, and the current command is NOT executed, and the next command is NOT displayed.

Cntl-O can be a real time saver.
StumbleUpon Toolbar


aht said...

The shell builtin command 'fc' is created for this.

List last 20 commands:
fc -l -20 -1

Edit last 5 cmds, execute when done:
fc -5 -1

Peter Leung said...

Thanks, you are right.
fc can do that too.

Anonymous said...

man, time server is NOT exactly that thing :)))

Did you mean: time-saver?

otherwise, great hint!

Peter Leung said...

Typo corrected. Thanks