Monday, August 20, 2018

Bash Tip - Quickly Find and ReUse Bash Command

Suppose you typed a long command, e.g., scp -r server-65:/Users/lex/opt/ethtool/ ~/opt/ethtool/ and you want to run it again and you are too lazy to type it again and too lazy to hit the up arrow to find it in your command history.

If you run the history command you'll see the last commands you entered:

$ history
 1157  # some command...
 1158  # some command...
 1159  # some command...
 1160  # some command...
 1161  # some command...
 1162  scp -r server-65:/Users/lex/opt/ethtool/ ~/opt/ethtool/
 1163  # some command...
 1164  # some command...
 1165  # some command...
 1166  # some command...
 1167  # some command...
 1168  # some command...
 1169  # some command...
 1170  # some command...
 1171  # some command...
 1172  # some command...
 1173  # some command...
 1174  # some command...
 1175  # some command...
 1176  # some command...
 1177  # some command...
$ 


Press CTRL + R and type yak Your prompt will change from what it is currrently, e.g, "$" to "(reverse-i-search)" .. and it will show you what you've matched in your command history by typing "yak".



(reverse-i-search)`yak': scp -r server-65:/Users/lex/opt/ethtool/ ~/opt/ethtool/



When you find the command you want to run just hit enter or esc to abort.



Share this article



This work is licensed under the Creative Commons Attribution 3.0 Unported License.

No comments:

Post a Comment