LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2011, 07:33 PM   #1
novloski
LQ Newbie
 
Registered: May 2011
Posts: 2

Rep: Reputation: 0
Help making specific script loop over multiple files in directory


I am trying - to no avail - to take an old script I found in the forum help-needed-in-grep-for-tar-gz-658730/ The code:
Code:
#!/bin/bash
if zgrep $1 $2 > /dev/null 2>&1
then
   while read file
   do
      if tar -zxOf $2 "$file" | grep -q $1
      then
         echo file "$file" matches
      fi 
   done < <(tar ztf $2)
fi
works great for a filename passed in as a parameter. But I have a few thousand files that I need to search through and I was hoping to make the script loop through all of the tar.gz files in the directory. I have browsed around some other threads and tried hacking some code together, but it just isn't panning out for me. This is what I am currently trying:
Code:
#!/bin/bash

cd /cygdrive/c/TEST/tars_4_testing

for files in `dir -d test.tar.gz` ; do
## original: if zgrep $1 $2 > /dev/null 2>&1
echo searching in "$files" for $1...
if zgrep $1 "$files" > /dev/null 2>&1
then
	while read file
	do
        echo "$file"
		if tar -zixOf $2 "$file" | grep -q $1
		then
			echo file "$file" matches
			tar -zixOf $2 "$file" >> gotcha.log
		fi 
	done < <(tar ztf $2)
fi
done
but it is not working. It loops through files, but when it starts the
Code:
while read file
loop, it starts looping through the wrong file. For example, as you see above I have hardcoded "test.tar.gz" to only search in that file. But when I run it it ends up searching through a file called x.tar.gz (which is apparent by the echo statements once in the while loop). Sorry for the verbose explanation for the simple problem. I probably could have gotten by with "please help turn the script on thread x to run on all files in a directory"

Anyways, thanks for you help.
-Novloski
 
Old 05-10-2011, 08:08 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Try this,

Code:
#!/bin/bash

cd /cygdrive/c/TEST/tars_4_testing
for files in `find . -exec basename {} \;` ; do
echo searching in "$files" for $1...
if zgrep $1 "$files" > /dev/null 2>&1
then
	while read file
	do
        echo "$file"
		if tar -zixOf $2 "$file" | grep -q $1
		then
			echo file "$file" matches
			tar -zixOf $2 "$file" >> gotcha.log
		fi 
	done < <(tar ztf $2)
fi
done
 
1 members found this post helpful.
Old 05-10-2011, 11:21 PM   #3
novloski
LQ Newbie
 
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Smile thanks

Thanks for the help. Sorry for posting the sloppy hacked up script. I should have taken out references to the variable $2 since I didn't want to use it anymore. Your code worked in the sense that it iterated through the files just like I was asking. The parts of the code that didn't work, were due to my posting of the sloppy script in the first place.

I'm not sure this will be my best solution due to the time it is taking to extract every file in the archive when a hit is found. But it is a start.

I really appreciate your quick assistance. Not sure this would be useful to anyone, but here is what I ended up using:
Code:
#!/bin/bash

cd /cygdrive/c/TEST/tars_4_testing/search
for files in `find . -exec basename {} \;` ; do
echo searching in "$files" for $1...
if zgrep $1 "$files" > /dev/null 2>&1
then
	echo $1 found in "$files"
	while read file
	do
        echo "$file"
		if tar -zixOf "$files" "$file" | grep -q $1
		then
			echo file "$file" matches
			tar -zixOf "$files" "$file" >> gotcha.log
		fi 
	done < <(tar ztf "$files")
fi
done

Kind Regards,
Novloski
 
Old 05-11-2011, 06:13 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Glad it worked for you and thanks for posting your working code, might be a help for others.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script - Use variable in a for loop with directory path Tech109 Linux - General 2 01-19-2011 11:22 AM
Loop through all files in a directory. Bash/Perl script? Nzo Linux - Newbie 9 12-09-2009 08:09 PM
loop move files up one directory level Melsync Programming 8 12-19-2005 11:51 PM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 05:08 PM
How to perform an action (a command) on multiple files with the FOR loop? chun-mee Programming 4 08-23-2005 04:11 AM


All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration