ログローテートされたファイルを解凍することなく、中身を見たいってなったときに便利。
というか、ログローテートされたファイルのログを見るときにこのコマンドしか使っていない。
gunzip -dc ≪ファイル名≫ | less
gunzip -dc ≪ファイル名≫ | grep "≪検索文字≫"
標準出力をfileAに書き込み、fileAを圧縮した後、それを解凍せずに中身が見れることを確認します。
まずは、標準出力をfileAに書き込みます。
[root@www test]# echo "happynewyear 2025" > fileA
[root@www test]# cat fileA
happynewyear 2025
⇒fileAに書き込まれたことをcatで確認できること
次に、fileAを圧縮します。
[root@www test]# gzip fileA
[root@www test]# ll
合計 8
4 -rw-r--r-- 1 root root 44 2024/12/29 09:29:17 fileA.gz
4 -rw-r--r-- 1 root root 22 2024/12/29 09:32:54 fileB
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileC
⇒fileA.gzが作られたこと
最後に、fileA.gzの中身を解凍なしに見ます。
今回はパイプでcatコマンドに標準出力を渡します。
[root@www test]# gunzip -dc fileA.gz | cat
happynewyear 2025
⇒fileA.gzの内容が表示されること
grepも試してみます。
[root@www test]# gunzip -dc fileA.gz | grep "2025"
happynewyear 2025
⇒2025が赤文字となっていること