grep tar.gz file without unzipping
This post will show you how to grep tar.gz or .gz files without unzipping it.
grep gz file without unzipping(not tar.gz)
You can use command zgrep
to do this
zgrep -a string file.gz
- -a means process a binary file as if it were text
Zgrep invokes grep on compressed or gzipped files. All options specified are passed directly to grep.
For example:
j@ubuntu:~/tmp$ zgrep -a test file1.gz
test123
grep tar.gz file without unzipping
Actually zgrep
also can deal with tar.gz file but the output is not so friendly.
j@ubuntu:~/tmp$ zgrep -a hello file.tar.gz
file20000664000175000017500000000000714101525007007240 0ustar jjhello2
So we can use tar
instead.
tar -xzOf file.tar.gz | grep STRING
- -x : Extract files from an archive
- -z : Filter the archive through gzip
- -o : Extract files to standard output
- -f : Use archive file
Below is one real example:
j@ubuntu:~/tmp$ tar -xzOf file.tar.gz | grep hello
hello2
grep tar.gz and print which actual files match
You know ,a tar.gz file may contains many files , if you need to know which sub files contain the strings you want to grep.
tar xzf file.tar.gz --to-command 'grep --label="$TAR_FILENAME" -H stringtosearch ; true'
- –to-command : Pipe extracted files to command
- –label : Display input actually coming from standard input as input coming from file TAR_FILENAME
- TAR_FILENAME : Internal environment variable of –to-command , means the name of the files which been extracted by tar
- -H : Print the file name for each match
Below is a real example:
j@ubuntu:~/tmp$ tar tzf file.tar.gz
file1
file2
j@ubuntu:~/tmp$ tar xzf file.tar.gz --to-command 'grep --label="$TAR_FILENAME" -H hello ; true'
file2:hello2
We can see file.tar.gz
contains 2 files, file1 and file1 , and only file2 match