この記事は、Linuxのトリックとテクニックシリーズの一部であり、この記事ではcatコマンド(Linuxで最も頻繁に使用されるコマンド)の基本的な使用方法とtac(catコマンドの逆 – ファイルを逆順に表示)をいくつかの実践的な例とともに説明します。
関連記事: Linuxでの有用な「cat」コマンドの例13
LinuxでのCatコマンドの基本的な使用方法
Catコマンドは、Concatenateの頭字語で、*nixシステムで最も使用されるコマンドの1つです。このコマンドの最も基本的な使用法は、ファイルを読み取り、それらをstdoutに表示することで、つまりファイルの内容を端末に表示することです。
# cat file.txt

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

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

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

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

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

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

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

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

Linuxのcatコマンドについてもっと学びたいですか?それなら、Linuxにおける13 Useful ‘cat’ Command Examples in Linuxという当社の記事を読んでください。
LinuxでTacコマンドの使用方法を学びます
一方、*nixシステムではあまり知られておらず、あまり使用されていないコマンドはtac
コマンドです。Tacは、実質的にcat
コマンドの逆バージョンです(また、逆に綴られています)。これはファイルの各行を、最下部の行から最上部の行に向かって、マシンの標準出力に出力します。
# tac file-all.txt

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

次に、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/