Register
It is currently Wed Apr 23, 2014 8:49 pm

Script to search through .zip .tar and .tar.gz archives


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Oct 13, 2008 5:11 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
Hi all I'm attempting to create a script that searches through archive files (.zip, .tzr, .tzr.gz) to find a particular file for me. I found this online:

Code:
grab () {
find /directory_path -iname "*.tar.gz" -exec tar tz -f '{}' \; | grep $1
find /directory_path -iname "*.tar" -exec tar t '{}' \; | grep $1
find /directory_path -iname "*.zip" -exec unzip -l '{}' \; | grep $1
}
grab *foo*.bar


I have been attempting to use it like this:
./script *foo*.bar

But it doesn't want to work and I have no idea why. It doesn't ouput an error or access the HDD, so I don't think it's actually doing anything. Basically I'm looking for anything of filetype ".bar" with "foo" as part of the filename (e.g. *foo*.bar). Also I would assume I put "*foo*.bar" in as argument 1 ($1) but I would like some clarification on it.

I also need it to search through the uncompressed directories and subdirectories as well, so I would probably just add a standard "find" or "grep" funtion to the start of it as well. Any advice?


Top
 Profile  
 PostPosted: Wed Oct 22, 2008 5:44 pm   

Joined: Fri Oct 17, 2008 10:45 am
Posts: 3
Location: Canada
The 3 entries with find /directory_path need to be changed to the path you want searched. For example, to search the entire directory tree, replace /directory_path with /. Or to search you're home directory, change it to ~ etc... Also, the script will not work as it is made as a function to be put into .bashrc like an alias. Put #!/bin/bash at the beginning of the file and grab $1 at the end of the file so that it executes.

Here is a improved copy that allows you to put in the search path as argument 1 and the file to find as argument 2:

#!/bin/bash
grab () {
find $1 -iname "*.tar.gz" -exec tar tz -f '{}' \; | grep $2
find $1 -iname "*.tar" -exec tar t '{}' \; | grep $2
find $1 -iname "*.zip" -exec unzip -l '{}' \; | grep $2
}
grab $1 $2
exit 0

examples: grab /usr Makefile
grab / *.txt


Top
 Profile WWW  
 PostPosted: Wed Oct 29, 2008 4:05 am   

Joined: Fri Sep 26, 2008 7:58 am
Posts: 10
I actually sorted it out anyway. I realised that. When I wrote "directory_path I actually meant that my directory path would be inserted there, not that I would perform the find in "directory_path".

Turns out it just took a while to run to completion as it was nearly 10 gigs of info.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP