Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

use find to search through compressed files?

Hi all,

Is it possible? I don't see any option in the man page for this.

I'm looking for an old file on a windows machine with cygwin installed. I tend to compress (and not consistently either, sometimes zip, bzip2, or gzip) old file/directories and lump them all together. I'm sure I have the file somewhere but it is likely compressed in my mess of archives somewhere and I don't want to manually have to search through them all.

Any suggestions?

Thanks,

-Pat
0
pat5star
Asked:
pat5star
  • 3
  • 3
  • 3
  • +2
3 Solutions
 
sunnycoderCommented:
Hi pat5star,

find / -name "*.gz" | while read name
do
     temp=`gzip -l $name | awk ' { print $4 } ' | grep <filename>`
     if [ -n $temp ]
            echo $name has <filename>
     fi
done

Sunnycoder
0
 
sunnycoderCommented:
pat5star,

A similar script can be written for bzip2 or any other archiving program that will let you list the files in the archive

Sunnycoder
0
 
MysidiaCommented:
Yes, it's possible it may just take a long time.
for example...

mkdir /tmp/lists
cd /tmp/lists

basepath=/path/to/start/from
gpcmd='grep -i filename_you_are_looking_for'
find $basepath -name \*.zip -print0 > zips
find $basepath -name \*.tar -print0 > tarballs
find $basepath -name \*.tar.bz2 -print0 > bzip_tarballs
find $basepath -name \*.tar.gz -print0 > gzip_tarballs

cat zips |xargs -0 -n 1 unzip -v |$gpcmd
cat tarballs |xargs -0 -n 1 tar tvf |$gpcmd
cat bzip_tarballs |xargs -0 -n 1 tar jtvf |$gpcmd
cat gzip |xargs -0 -n 1 tar jtvf |$gpcmd
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
MysidiaCommented:
Err, actually, my above commands would find if it's around but not tell you exactly which archive
the file was found in (fairly useless)

Instead, an approach more like this:

cat zips |xargs --verbose --replace -0 sh -c 'echo "Archive: {}" ; unzip -v "{}" |grep name_search_text'
cat tarballs |xargs --verbose --replace -0 sh -c 'echo "Archive: {}" ; tar tvf "{}" |grep name_search_text'
cat bzip_tarballs |xargs --verbose --replace -0 sh -c 'echo "Archive: {}" ; tar jtvf "{}" |grep name_search_text'
cat gzip |xargs --verbose --replace -0 sh -c 'echo "Archive: {}" ; tar ztvf "{}" |grep name_search_text'
0
 
owensleftfootCommented:
For a change maybe windows has an easier solution :) Have a look at http://www.worldssp.net/software/Zipsearch.htm
0
 
GnsCommented:
Um what's easier with a shareware install Foot? Extending Sunnys script (sorry Mysidia, I've not looked over your scriptlets for sanity... yet:-) to "work" with any number of tools/archivers for finding the archive containing a specific file is trivial.

-- Glenn
0
 
owensleftfootCommented:
" Um what's easier with a shareware install Foot?"

Hi Glen. he's looking for "an old file". With the emphasis on "an". As in singular. Why bother typing in some of the above code (which may well not work or may be buggy) when  he can download a shareware windows program which will  do what he wants "out of the box"? A lazy programmer is a good programmer.. You cant get any lazier than not having to program at all. I prefer linux but Im not political about it - if there is an easier windows solution I will use it.
0
 
pat5starAuthor Commented:
I awarded a grade of 'A' to this answer as all solutions were very helpful. I awarded 250 points to owensleftfoot because in this particular case I needed to find the file on a windows machine and his suggestion is really the easiest to use and enabled me to find what I needed. I awarded 125 points both to sunnycoder and Mysidia as those scripts should work well for me on my linux machines or even the windows machines that I have cygwin installed on.

Normally I like writing or tweaking scripts to do what I want but this case is different. I needed that file ASAP and really didn't want to spend the time tweaking and testing a script to make sure it's checking all directories, files, and compressed files properly. Later when I have time to spare I will as these are handy scripts that I know I'll need again in the future...at least until I learn how to archive things in a more orderly fashion :P

I hope everyone is happy with the way I split the points and I appreciate the advice. Thanks again,

-Pat
0
 
GnsCommented:
Thank you Pat for the nice feedback. (And now Foot can say the mandatory "nananananana, I wa-as ri-ight":-):-)

-- Glenn
0
 
sunnycoderCommented:
lol ... I cant wait to hear that :o)
0
 
owensleftfootCommented:
" Thank you Pat for the nice feedback. (And now Foot can say the mandatory "nananananana, I wa-as ri-ight":-):-)"

Im too much of a gentleman Glen :)
0
 
GnsCommented:
:-)

-- Glenn
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now