I am working on SunOS 5.10. I have a folder that contains about 200 zip files. Each zip file contains only one text file in it.
I would like to search for a specific string in all the text files in all the zip files.
I tried this (which searches for any text file in the zip file that contains the string “ORA-“) but it didn’t work.
zipgrep ORA-1680 *.zip
What is the correct of doing it without uncompressing the zip files?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It is in general not possible to search for content within a compressed file without uncompressing it one way or another.
Since zipgrep is only a shellscript, wrapping unzip and egrep itself, you might just as well do it manually:
for file in *.zip; do unzip -c "$file" | grep "ORA-1680"; done
If you need just the list of matching zip files, you can use something like:
for file in *.zip; do
if ( unzip -c "$file" | grep -q "ORA-1680"); then
echo "$file"
fi
done
This way you are only decompressing to stdout (ie. to memory) instead of decompressing the files to disk. You can of course try to just grep -a the zip files but depending on the content of the file and your pattern, you might get false positives and/or false negatives.
Method 2
zipgrep takes a single file. To make it work across multiple files put it in a loop:
for i in *.zip do zipgrep ORA-1680 "$i" done
Method 3
The AVFS filesystem presents a view of the filesystem where every archive file /path/to/foo.zip is accessible as a directory ~/.avfs/path/to/foo.zip#. It’s a FUSE filesystem, which you can install on Solaris. AVFS provides read-only access to most common archive file formats.
mountavfs
for z in ~/.avfs$PWD/*.zip; do
find "$z#" -exec grep ORA-1680 {} +
done
fusermount -u ~/.avfs # optional
Method 4
ugrep recursively searches zip, gz, tar, tgz, bz2, lz4, zstd, pax, cpio, and other types of compressed files and archives with option -z.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0