Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can search through a number of files for a particular string by pipelining 'grep' and 'find' commands.

Now, I have some 10 zip files each of which has archived a lot of text files. Is there any way I could search for a string in all text files archived in all those zip files without extracting the zip files?

share|improve this question
up vote 1 down vote accepted
unzip -c \*.zip | grep yourtext

You could use some regexes in the grep command to finetune the string matching

Based on the comments below, zgrep would be a better option.

share|improve this answer
    
Thanks Raj. But your solution is useful when just one zip file is used at a time. Nothing appears if *.zip is used but just a CAUTION: filename not matched appears. – usajbalt Nov 24 '10 at 6:57
    
you have to use the backslash according to the manual page. Am just printing this out for your benefit - it should be unzip -c <backslash>*.zip, not just *.zip. – Rajesh Krishnamoorthy Nov 24 '10 at 7:11
    
Ahaan..okay got it worked but there is still one problem. I am doing all this to find which file contains a particular string. The command you mentioned will not print the name of file in which the string is found. – usajbalt Nov 24 '10 at 8:09
    
ah..then you could use zgrep - zgrep <options> <pattern> *.zip – Rajesh Krishnamoorthy Nov 25 '10 at 0:24

You can also use zipgrep command that is included with unzip package. For example:

for z in *.zip ; do zipgrep PATTERN $z |sed "s/^/$z: /" ; done

This command gives you matched file names inside the archives as well.

share|improve this answer

Mount the archives as directories using AVFS or fuse-zip. Both are FUSE filesystems.

AVFS provides a view of the filesystem in ~/.avfs where every archive file has an associated directory with the same name plus a # at the end.

mountavfs
grep -r PATTERN ~/.avfs/$PWD/*.zip\#/
…
unmountavfs

Fuse-zip mounts a zip onto a directory.

for z in *.zip; do mkdir "$z.d" && fuse-zip "$z" "$z.d"; done
grep -r PATTERN *.zip.z/
…
fusermount -u *.zip.d
rmdir *.zip.d
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .