3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux初心者向け:!! と !文字列 の応用ワザ

Posted at

Linux初心者向け:!! と !文字列 の応用ワザ

こんにちは、みおです。
前に紹介した history コマンド、めちゃ便利なんですが…
その中でも特に使えるのが !!!文字列 系です。
今回はこれらの応用ワザをまとめます!


1. !! で直前コマンドを再実行

$ ls
Desktop  Documents
$ !!
ls
Desktop  Documents

👉 単純に「直前コマンドをもう一回」です。
タイプミスしたときや、権限不足でやり直すときに大活躍!


2. sudo !! で「権限不足」を即修正

$ apt-get update
Permission denied
$ sudo !!
sudo apt-get update

👉 Linuxあるある「sudoつけ忘れた〜!」を一発で解決できます。
これは覚えておくと本当に助かります。


3. !番号 で履歴から実行

$ history | tail -n 3
  201  ls -la
  202  cd projects
  203  git status
$ !201
ls -la

👉 番号を指定して再実行。確実に狙ったコマンドを呼び出せます。


4. !文字列 で直近のコマンドを再実行

$ !git
git status
On branch main
nothing to commit, working tree clean

👉 「直近で打った git コマンド」を一瞬で呼び出してくれます。


5. !?文字列? で部分一致検索

$ !?status?
git status
On branch main

👉 コマンドの一部しか覚えてなくても呼び出せます。地味に便利!


まとめ

  • !! → 直前コマンドをもう一度
  • sudo !! → sudo忘れを即救済
  • !番号 / !文字列 → 指定して再実行
  • !?文字列? → 部分一致で検索

初心者さんはまず sudo !! を覚えるだけでも世界が変わります。
ぜひ今日からターミナルに取り入れてみてくださいね✨


💡 ちなみに…
ほかにも「Linux初心者におすすめの学習リソース」をまとめた記事を書いてます。

👉 Linux初心者におすすめの学習リソースまとめ

リソース選びで迷ったときの参考になると思うので、良かったらのぞいてみてくださいね〜。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?