Best examples of zgrep and zcat commands in Unix/Linux
- Admin
- Dec 31, 2023
- Linux-unix
This post covers the following examples
- Find the given input string in
tar.gzfiles - Search string in a nested folder, that contains gzip files
- search
tarfiles for a givenstring - how to use
grepcommand ongzfiles. - find the string pattern gzipped files in a folder
- zgrep multiple gz files in a directory
In Linux and Unix, a grep command used to search for a given string in log and text files. In production Environments, These files are compressed as gz or tar,gzip files.
Suppose you have uncompressed files like gzip, tar, or gz files, How do you search for a given word in those search files?
Tomcat server stores the log files in tar.gz format and these files are generated daily. If you want to search for errors or strings in those files, We can use zgrep for search and zcat to view the files.
grep command does not work on tar or gz files, It is an equivalent command called zgrep with compressed files
Here is the syntax of the Zgrep command
zgrep searchpattern inputgzdirectory options
searchpattern is any plain string or combination of *,? and plainstring
inputgzdirectory is the location of gz files
options are optional like -R or -M , -a
How to search tar.gz files for a particularly given string without extracting it?
In a Current directory, there are many tar.gz files, how do we search for a particular word in those files? First way, unzip the .gz file and use the grep command.
Here are the steps
gunzip filename.gz
grep "searchword" file
Another way, is using the zgrep command in Unix.
zgrep -a "search word" filename.gz
grep command works on text files and zgrep works on compressed logs.
How to Search gzip files in a nested folder
zgrep search in the current directory gz files.
If you want to search nested and recursive folders/directories, use the -R option.
zgrep -R --include=*.gz -H "*Exception\*" .
Another way using zipgrep also used for zgrep files
zgrep 'string' \*.zip
Zcat command example in Linux
Cat command in Linux is used to view the log and text files,
Zcat is similar to the cat command but it only works with tar/gz files.
The Zcat command is used to view compressed files in Linux or Unix.
syntax
zcat options gzfile
It is used to view compressed files without uncompressing them.
zcat filename.tar.gz
you can also use the pipe symbol to move content to other files or the console.
What is zgrep command?
Zgrep command is a grep command for compressed files in Unix/Linux. It searches the string or regular expression pattern of a string in an uncompressed file, without extracting the content. There are a lot of options -e,-w, and recursive folder search.
What is the difference between grep and zgrep?
grep command used to search string, regex pattern strings in text, and normal files. zgrep command search strings in compressed files such as gz, tar,gzip formats.
Both commands have lot of options to search case-sensitive, recursive search.
What is the Zcat command used for?
zcat command is used to view the compressed files without extracting the content. It is similar and equivalent to cat command for compressed files.
