4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

history | grep XXX と!履歴番号を駆使して過去に実行したコマンドを再実行する

Posted at

#はじめに
ターミナル上で過去実行したコマンドの検索はどの様に行なっていますか?
ctrl+r 派でしょうか。それともhistory | grep XXX 派でしょうか。

筆者の個人的な意見ですが、
ctrl+rは結果が一行しか出ないので見辛い、かつ、ctrl+rで目的の履歴に一致するまで遡るのがめんどくさい。かといってhistory | grep XXXXだと出力結果からコピペするのがメンドくさいです。

そこでhistory | grep XXX と !(感嘆符)履歴番号を使う

筆者はsshしたホスト名を結構な頻度で忘れます。今回もsshrc1で過去のコマンド検索して実行してみます。

$ history | grep sshrc
  194  vim .sshrc
  197  . .sshrc
  200  vim .bashrc[[ -f ~/.sshrc ]]
  201  [[ -f ~/.sshrc ]]
  202  [ -f ~/.sshrc ]
  203  test -f ~/.sshrc
  204  test  ~/.sshrc
  209  [ -f ~/.sshrc ]; echo $?
  210  [[ -f ~/.sshrc ]]; echo $?
  214  . .sshrc
  242  sshrc uni-dev
  243  sshrc uni-pra
  361  history | sshrc
  362  history | grep sshrc
  364  sshrc bot-event
  372  vim .sshrc
  373  cd  .sshrc
  378  sshrc
  379  history | sshrc
  380  history | grep sshr
  383  sshrc bot-event
  394  vim .sshrc
$!383
!508
sshrc bot-event

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
/etc/update-motd.d//30-banner:


       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
Last login: Wed Jan 1 13:35:36 2020 from kd027095113068.p

無事にsshできました。簡単ですね。

おわりに

history | grep sshrcしたけど、このホスト名はどのサーバだっけ...?となりそうで筆者は怖いです。ホスト名はわかりやすくしようね!

  1. sshrcの概要:https://qiita.com/ikuwow/items/ba4ca57fd67c06fd1b19

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?