March 15, 2007 6:27pm
My aim was to find a bunch of zip files, unzip with the -t flag to get their contents, and look for a specific file in the archive. The output I wanted was the name of the zip, followed by the path to the file. Like this:
ZIP: archive.zip
testing: path/to/the/filename OK
find . -name \*.zip -exec unzip -t {} \; grep filename gives me the path to the file in the archive, but not the name of the file (aside: I always forget that you need space, backslash, semicolon at the end of the find exec flag). I ended up writing a script that prints the zip name first, then unzips the archive and greps the output.#!/bin/bashTrouble is, the zip files have spaces and brackets in their names (eww!), so unzip can't find them. For example, if the archive is called
echo "ZIP: $1"
unzip -t `echo $1` | grep filenamemy zip.zip, unzip tries to unzip my.zip. Now, the solution at this point should have been obvious, but as usual my brain went on holiday for an hour or two and I went down a wacky sed path. The solution is much simpler; use quotes.#!/bin/bashIs there a way to do away with the script and make it a one liner?
echo "ZIP: $1"
unzip -t "`echo $1`" | grep filename