Brief explanation how to read compressed log files in Centos or Redhat Linux.

Today we will look at an interesting topic “how to read compressed i.e. gzip files” in Linux.

Lets see below example:

1. Reading .tar.gz compressed zip file in Linux.

[root@nglinux testdir]# echo "Hello World" > file1.txt
[root@nglinux testdir]# tar -cvf file1.tar file1.txt 
file1.txt

[root@nglinux testdir]# ls -ltr
total 16
-rw-r--r--. 1 root root    12 Feb 22 11:44 file1.txt
-rw-r--r--. 1 root root 10240 Feb 22 11:45 file1.tar

[root@nglinux testdir]# zcat file1.tar 
gzip: file1.tar: not in gzip format

[root@nglinux testdir]# gzip file1.tar 
    
[root@nglinux testdir]# zcat file1.tar.gz 
file1.txt0000644000000000000000000000001413434050463011312 0ustar  rootrootHello World
[root@nglinux testdir]# 

 

2. Reading .gz file.

[root@nglinux testdir]# ls -ltr
total 8
-rw-r--r--. 1 root root  12 Feb 22 11:44 file1.txt
-rw-r--r--. 1 root root 145 Feb 22 11:45 file1.tar.gz
[root@nglinux testdir]# 

[root@nglinux testdir]# gzip file1.txt 
[root@nglinux testdir]# ls -ltr
total 8
-rw-r--r--. 1 root root  42 Feb 22 11:44 file1.txt.gz
-rw-r--r--. 1 root root 145 Feb 22 11:45 file1.tar.gz

[root@nglinux testdir]# zcat file1.txt.gz 
Hello World
[root@nglinux testdir]# 

 


The commands to read zip or compressed files starts with “Z” and are also called Z commands.
zcat : to cat/view the compressed file
zless and zmore : are for less and more, to view the file page by page
zgrep & zegrep : to grep and search inside the compressed file
zcmp & zdiff : helps in finding out difference between two compressed files

 

3. Usage of zgrep, zegrep, zmore, and zless command.

[root@nglinux testdir]# zcat file1.txt.gz 
Hello World

[root@nglinux testdir]# zmore file1.txt.gz 
------> file1.txt.gz <------
Hello World

[root@nglinux testdir]# zless file1.txt.gz 
Hello World
file1.txt.gz (END) 

[root@nglinux testdir]# zgrep -o  "Hello" file1.txt.gz 
Hello

[root@nglinux testdir]# zegrep -o  "Hello|rld" file1.txt.gz 
Hello
rld
[root@nglinux testdir]# 

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments