eyeonチームの新人(Linuxも触ったことないレベル)くんに、テキストファイルの操作を教えたときメモを共有いたします。
IT業界の最新動向は、全く踏まえておりません!
30年以上前からの基礎知識です。
1.いきなりまとめ
Linux上でテキストファイルの中身を見るときのコマンドは、以下のように使い分けしましょう。
| コマンド(基本の構文) | 使いみち | 実行例 |
| cat <ファイル名> | ファイル全体を見たいときに使いましょう。 なお、でっかいファイルを単純にcatで開くのは止めましょう。ファイルの内容がすべて表示されるまで、画面にファイルの内容が流れ続けます。 | $ cat /etc/hosts |
| tail <ファイル名> | ファイルの末尾だけ見たいときに使いましょう(ログファイルの最新のログだけ見たいときとか)。 デフォルトだと、ファイルの末尾の10行だけ表示されます。 | $ tail /var/log/messages |
| less <ファイル名> | ファイルを1ページずつめくっていきたいときに使いましょう(ログファイルのログを、上から全部チェックしたいときとか)。 開いたら、[PageDonw]キーとかで次のページに進みましょう。 | $ less /var/log/messages |
| head <ファイル名> | ファイルの上のほうだけ見たいときに使いましょう。 とりあえずどんなことが書かれているファイル?ってときとかに良いかも。 デフォルトだと、ファイルの最初の10行だけ表示されます。 | $ head /var/log/messages |
| vi <ファイル名> | テキストファイルを編集したいときに使いましょう。 viじゃなくてvimだとちょっとだけ高機能です。 | $ vi /etc/hosts |
| grep <文字列> <ファイル名> | 指定した文字列が含まれる行だけ表示したいときに使いましょう。 | $ grep TEST /etc/hosts |
2.各コマンドによる表示内容のご紹介
(1)cat
実行すると、対象ファイル全体が表示(標準出力)されます。
でっかいファイルだと、表示しきるまでターミナルソフトが占有されちゃうのでご注意ください。
(ターミナルソフトの画面上にずっとファイル内容が流れ続ける)
[username@hostname ~]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost6 localhost6.localdomain6
10.10.10.1 eyeon1
10.10.10.2 eyeon2
[username@hostname ~]$
(2)tail
実行すると、ファイルの末尾10行(デフォルト)が表示されます。
まぁ、末尾=シッポ=tailってことです。
なお、以下の例では、末尾5行を指定しています。
加え、最新ログが勝手に自動表示されるよう、-f を指定しています。
[username@hostname ~]# tail -n5 -f /var/log/messages
Jun 9 17:30:01 hostname systemd: Created slice User Slice of root.
Jun 9 17:30:01 hostname systemd: Started Session 71 of user root.
Jun 9 17:30:01 hostname systemd: Removed slice User Slice of root.
Jun 9 17:30:27 hostname dhclient[2230]: XMT: Solicit on eth0, interval 108030ms.
Jun 9 17:30:32 hostname su: (to root) username on pts/1
(3)less
実行すると、ファイルの最初の1ページ目が表示されます。
スペースキーを押下すると、次のページに進みます。
[ENTER]を押下すると、次の行に進みます。
[Shift] + G を押下すると、最終行が表示されます。
[username@hostname ~]# less /var/log/messages
Jun 5 08:12:02 hostname systemd: Removed slice User Slice of root.
Jun 5 08:12:26 hostname dhclient[2216]: XMT: Solicit on eth0, interval 109700ms.
Jun 5 08:14:16 hostname dhclient[2216]: XMT: Solicit on eth0, interval 130070ms.
Jun 5 08:15:41 hostname systemd: Starting Cleanup of Temporary Directories...
Jun 5 08:15:41 hostname systemd: Started Cleanup of Temporary Directories.
:
(4)head
実行すると、ファイルの先頭の10行(デフォルト)が表示されます。
まぁ、先頭=頭=ヘッドってことです。
なお、以下の例では、先頭の5行を指定しています。
[root@hostname ~]# head -n5 /var/log/messages
Jun 5 08:12:02 hostname systemd: Removed slice User Slice of root.
Jun 5 08:12:26 hostname dhclient[2216]: XMT: Solicit on eth0, interval 109700ms.
Jun 5 08:14:16 hostname dhclient[2216]: XMT: Solicit on eth0, interval 130070ms.
Jun 5 08:15:41 hostname systemd: Starting Cleanup of Temporary Directories...
Jun 5 08:15:41 hostname systemd: Started Cleanup of Temporary Directories.
(5)vi
実行すると、ファイルを編集できるようになります。
i をクリックと編集モード(Insertモード)になります。
[username@hostname ~]$ vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost6 localhost6.localdomain6
10.10.10.1 eyeon1
10.10.10.2 eyeon2
(6)grep
指定した文字列を含む行だけを表示できます。
以下では、/var/log/messages ってログファイルの”Jun 5 08:20:”という文字列を含む行のみを表示しています(要するに、6月5日8時20分のログ)。
[username@hostname ~]# grep "Jun 5 08:20:" /var/log/messages
Jun 5 08:20:01 hostname systemd: Created slice User Slice of root.
Jun 5 08:20:01 hostname systemd: Started Session 3 of user root.
Jun 5 08:20:01 hostname systemd: Removed slice User Slice of root.
Jun 5 08:20:40 hostname dhclient[2216]: XMT: Solicit on eth0, interval 131430ms.
[username@hostname ~]#
X.関連ページ
eyeonでは、IT業界の基礎知識として、以下のブログを公開しております。