この記事はLinuxの秘訣とヒントシリーズの一部です。この記事では、猫の基本的な使用法について説明します。 コマンド(Linuxで最も頻繁に使用されるコマンド)および tac (catコマンドの逆–ファイルを逆の順序で印刷します)いくつかの実用的な例を示します。
関連項目: 13Linuxでの便利な「cat」コマンドの例
LinuxでのCatコマンドの基本的な使用法
猫 コマンド、連結の頭字語 は、*nixシステムで最もよく使用されるコマンドの1つです。このコマンドの最も基本的な使用法は、ファイルを読み取り、それらを stdoutに表示することです。 、端末にファイルの内容を表示することを意味します。
# cat file.txt


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


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


アペンドリダイレクタを使用すると、新しいファイルのコンテンツをfile-all.txt
の下部に追加できます。 次の構文で。
# cat file4.txt >> file-all.txt


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


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


猫の使用量を減らす コマンドは、以下の構文で新しいファイルを作成することです。ファイルの編集が終了したら、CTRL+D
を押します。 新しいファイルを保存して終了します。
# cat > new_file.txt


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


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


Linux catコマンドについてもっと知りたいですか?次に、Linuxで役立つ13の「cat」コマンドの例に関する記事を読んでください。
LinuxでTacコマンドを使用する方法を学ぶ
一方、* nixシステムであまり知られていない、あまり使用されていないコマンドは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
サンプル出力
[email protected] ~ $ 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 ....
[email protected] ~ $ 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
テキストファイルの操作には優れていますが、他の種類のファイル、特にバイナリファイルや、最初の行がそれを実行するプログラムを示しているファイルでは避ける必要があります。