[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【超絶入門3分】できた!Linux系OSコマンド実行履歴を表示する

こんにちは!
株式会社ビヨンド四国オフィスのペルシャ猫、いのうえです。

かっこよく言えばMSPの私が日々障害対応していくなかでよく使うコマンド「history
コマンドの実行履歴一覧を表示するためのコマンド。
「実行したコマンドを振り返りたいなあ~」って時に使えます。

historyコマンド

コマンド実行履歴一覧を表示する。

[root@test-aws-harukainoue ~]# history
   5  ls -l
    6  cd dir1
    7  ls -l
    8  touch haruka{1..10}
  ・
  ・
  ・
 995  netstat
  996  netstat -l
  997  netstat -t
  998  netstat -p
  999  netstat -nltp
 1000  history

直近に実行したコマンド5個を表示する

[root@test-aws-harukainoue ~]# history 5
  999  netstat -nltp
 1000  history
 1001  history 5
 1002  history
 1003  history 5

履歴の番号でコマンドを実行する

[root@test-aws-harukainoue kadai2]# !855
df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        475M     0  475M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  400K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  2.8G  5.3G  35% /
tmpfs            99M     0   99M   0% /run/user/1000

855番のコマンドを実行したい場合
わざわざコマンドを入力しなくても、「」+「番号(855)」だけで
指定した番号のコマンドを実行することができます!便利!

mkdirを含むコマンド履歴だけを表示する

[root@test-aws-harukainoue ~]# history | grep mkdir
 194  mkdir haruka
  197  mkdir dir3
  291  mkdir test1
  298  mkdir txt.1 txt.2
  385  mkdir haruka
  820  mkdir haruka
  836  mkdir sntax
  842  mkdir syntax
  965  mkdir test
 1003  history | grep mkdir

grepmkdirコマンドの実行履歴だけを絞り込んで表示することもできます。
history」の後に「|(パイプ)」+「grep」+「絞りたいコマンド」で可能です。

historyコマンド以外に使える実行履歴の検索方法があった?!

Ctrl」+「r」をターミナル上で同時に押すとコマンド履歴の検索モードになる機能があったんです!発見!
例えば、「w」が含まれるコマンド履歴を検索します。

(reverse-i-search)`w': pwd

Ctrl」+「r」で検索モードになるので、検索したい英数字を入力します。
Enter」を押すとコマンドが実行されます。
なんてことでしょう~簡単~♪
直近で叩いたコマンド履歴から表示されるので、「Ctrl」+「r」でひとつずつさかのぼることができます。

まとめ

history」だけだとずらずらとコマンド履歴が表示されますが
grep」で絞り込むことで対象のコマンドを含む実行履歴のみが表示されるので時短になりそうです。
これは使えそう!」と頻繁に使っちゃってます。
日々新しく覚えることだらけの私にとって
「あぁ~あの時どんなコマンド叩いたっけなあ~?」と振り返られる「history」コマンドは重宝してます。

今後もトライアンドエラーとやらを繰り返し、
お気に入りのコマンドや、個人的にもっと理解を深めたいコマンドに関してブログを書いていきます。

日々成長、日々前進。
毎日、私自身をアップデートしていかなければ!!!
最後まで読んでくださって、ありがとうございます。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
2,176
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Akika Inoue

Belongs to the System Solutions Department.
He joined Beyond as a founding member of the Shikoku office.
I jumped into the IT industry with no experience. As an education team, we create curriculum and conduct training for new graduates, mid-career, and existing members.
The main business is server operation and maintenance.
Either way, we value your content.
Also belongs to the Web Content Division and YouTube Team.