Solved

use find to search through compressed files?

Posted on 2004-09-07
12
315 Views
Last Modified: 2012-05-05
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
Comment
Question by:pat5star
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 125 total points
ID: 12003620
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12003628
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
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 125 total points
ID: 12003679
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
 
LVL 23

Expert Comment

by:Mysidia
ID: 12003792
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
 
LVL 17

Accepted Solution

by:
owensleftfoot earned 250 total points
ID: 12004826
For a change maybe windows has an easier solution :) Have a look at http://www.worldssp.net/software/Zipsearch.htm
0
 
LVL 20

Expert Comment

by:Gns
ID: 12005949
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 17

Expert Comment

by:owensleftfoot
ID: 12011255
" 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
 
LVL 3

Author Comment

by:pat5star
ID: 12012975
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
 
LVL 20

Expert Comment

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

-- Glenn
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12013936
lol ... I cant wait to hear that :o)
0
 
LVL 17

Expert Comment

by:owensleftfoot
ID: 12014393
" 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
 
LVL 20

Expert Comment

by:Gns
ID: 12014401
:-)

-- Glenn
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now