Linuxでの「cat」と「tac」コマンドの使用方法と例

この記事は、Linuxのトリックとテクニックシリーズの一部であり、この記事ではcatコマンド(Linuxで最も頻繁に使用されるコマンド)の基本的な使用方法とtac(catコマンドの逆 – ファイルを逆順に表示)をいくつかの実践的な例とともに説明します。

関連記事: Linuxでの有用な「cat」コマンドの例13

LinuxでのCatコマンドの基本的な使用方法

Catコマンドは、Concatenateの頭字語で、*nixシステムで最も使用されるコマンドの1つです。このコマンドの最も基本的な使用法は、ファイルを読み取り、それらをstdoutに表示することで、つまりファイルの内容を端末に表示することです。

# cat file.txt
View Content of File in Linux

catコマンドの別の使用法は、複数のファイルを読み取るか結合して出力をモニターに送信することです。以下の例で示します。

# cat file1.txt file2.txt file3.txt
View Content of Multiple Files

このコマンドは、“>” Linuxリダイレクト演算子を使用して複数のファイルを1つの単一のファイルに連結(結合)するためにも使用できます。

# cat file1.txt file2.txt file3.txt > file-all.txt
Join Multiple Files in Linux

追記リダイレクタを使用すると、次の構文で新しいファイルの内容をfile-all.txtの末尾に追加できます。

# cat file4.txt >> file-all.txt
Append Content File to New File

catコマンドを使用してファイルの内容を新しいファイルにコピーすることもできます。新しいファイルの名前は任意に変更できます。たとえば、現在の場所からファイルを/tmp/ディレクトリにコピーします。

# cat file1.txt > /tmp/file1.txt 
Copy Content of File to New File

現在の場所からファイルを/tmp/ディレクトリにコピーして、その名前を変更します。

# cat file1.txt > /tmp/newfile.cfg
Copy File to /tmp Location

A less usage of the cat command is to create a new file with the below syntax. When finished editing the file hit CTRL+D to save and exit the new file.

# cat > new_file.txt
Create New File using Cat Command

ファイルのすべての出力行に番号を付けるには、-nスイッチを使用します。

# cat -n file-all.txt
Add Numbers to Lines in File

非空行の番号のみを表示するには、-bスイッチを使用します。

# cat -b file-all.txt
Print Line Numbers in File

Linuxのcatコマンドについてもっと学びたいですか?それなら、Linuxにおける13 Useful ‘cat’ Command Examples in Linuxという当社の記事を読んでください。

LinuxでTacコマンドの使用方法を学びます

一方、*nixシステムではあまり知られておらず、あまり使用されていないコマンドはtacコマンドです。Tacは、実質的にcatコマンドの逆バージョンです(また、逆に綴られています)。これはファイルの各行を、最下部の行から最上部の行に向かって、マシンの標準出力に出力します。

# tac file-all.txt
Print Content File in Reverse Order

コマンドの最も重要なオプションの1つは、ファイルの内容を文字列またはキーワードに基づいて分割する-sスイッチで表されます。

# tac file-all.txt --separator "two"
Remove Matching String in File

次に、tacコマンドの最も重要な使用法は、ログファイルの逆順のログ内容をデバッグするのに大いに役立つことです。

$ tac /var/log/auth.log

Or to display the last lines

$ tail /var/log/auth.log | tac
サンプル出力
tecmint@tecmint ~ $ tac /var/log/auth.log
pr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
....
tecmint@tecmint ~ $ tail /var/log/auth.log | tac
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 15:55:02 tecmint CRON[17194]: pam_unix(cron:session): session closed for user root
Apr  6 15:55:01 tecmint CRON[17195]: pam_unix(cron:session): session closed for user root
...

同じくcatコマンドと同様に、tacテキストファイルを操作する際に優れた仕事をしますが、特にバイナリファイルや最初の行がそれを実行するプログラムを示すファイルでは避けるべきです。

Source:
https://www.tecmint.com/learn-linux-cat-command-and-tac-command/