LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-26-2012, 03:04 PM   #1
scambro
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
Search all tar.gz files in subdirectories for file


I have subfolders of 365 tar.gz files from a webserver across the span of a year. I now need to search all of these tar.gz files for the most recent copy of a few php files. I tried this, but I'm not getting the desired results (the command comes back with Not found in archive, when I know it is):

Code:
www_backups$ tar -ztvf */www*.* | grep ".php"
Code:
tar: 20120324/www-20120324.tar.gz: Not found in archive
But if I cd to 20120324 and do the same command, I get all kinds of matches (as expected). What am I missing in this simple line? Thanks!

Last edited by scambro; 03-26-2012 at 03:26 PM.
 
Old 03-26-2012, 03:34 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
After the shell filename expansion, your tar command results in something like:
Code:
tar -ztvf 20120323/www-20120323.tar.gz 20120325/www-20120325.tar.gz 20120326/www-20120326.tar.gz
The name of the archive to test is that one highlighted in blue and given as argument of the -f option. The other names are arguments of the tar command and they are searched inside the previous archive. If you use -x instead of -t, this mechanism serves to extract only some files from a given archive.

If you want to look for a file inside multiple archives, better to use a loop, e.g.
Code:
for archive in */www-*.tar.gz
do
  tar --wildcards -tvf $archive *.php
done
As you can see, the grep command is not necessary, since we want to look for php files inside the archive, hence we pass them as arguments. The --wildcars option is necessary to enable pattern matching with *. Hope this helps.
 
1 members found this post helpful.
Old 03-26-2012, 04:28 PM   #3
scambro
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
After the shell filename expansion, your tar command results in something like:
Code:
tar -ztvf 20120323/www-20120323.tar.gz 20120325/www-20120325.tar.gz 20120326/www-20120326.tar.gz
The name of the archive to test is that one highlighted in blue and given as argument of the -f option. The other names are arguments of the tar command and they are searched inside the previous archive. If you use -x instead of -t, this mechanism serves to extract only some files from a given archive.

If you want to look for a file inside multiple archives, better to use a loop, e.g.
Code:
for archive in */www-*.tar.gz
do
  tar --wildcards -tvf $archive *.php
done
As you can see, the grep command is not necessary, since we want to look for php files inside the archive, hence we pass them as arguments. The --wildcars option is necessary to enable pattern matching with *. Hope this helps.
aaaaah, ok. I see what you're saying. That's why I was getting a huge list of tar files not found except for the first day, because it was searching in that file, but then also searching in that file for every subdirectory. Makes sense. Thanks for the explanation and assistance!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursive search in a tar.bz2 file vikas027 Programming 10 07-15-2011 03:55 AM
how to search a string in the files in the subdirectories wj123 Programming 4 03-10-2011 08:37 PM
Sort Many Files in Subdirectories into Directories by File Type garyozzy Programming 5 02-15-2011 03:18 PM
I need grep options to search inside a .tar.gz file. ZAMO Linux - General 2 06-24-2008 11:55 PM
tar --exclude for subdirectories mrtwice Linux - Software 2 10-23-2003 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:13 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
Open Source Consulting | Domain Registration