I have list of files which contain particular patterns. But , those file have been tarred. Now I want to search the pattern in the tar file . And to know which are file contain pattern. without extracting it.
Any idea....?
I have list of files which contain particular patterns. But , those file have been tarred. Now I want to search the pattern in the tar file . And to know which are file contain pattern. without extracting it.
Any idea....?
Python's tarfile
module along with Tarfile.extractfile()
will allow you to inspect the tarball's contents without extracting it to disk.
The easiest way is probably to use avfs. I've used this before for such tasks.
Basically, the syntax is:
avfsd ~/.avfs # Sets up a avfs virtual filesystem
rgrep pattern ~/.avfs/path/to/file.tar#/
/path/to/file.tar
is the path to the actual tar file.
Pre-pending ~/.avfs/
(the mount point) and appending # lets avfs expose the tar file as a directory.
the tar
command has a -O
switch to extract your files to standard output. So you can pipe those output to grep/awk
tar xvf test.tar -O | awk '/pattern/{print}'
tar xvf test.tar -O | grep "pattern"
eg to return file name one pattern found
tar tf myarchive.tar | while read -r FILE
do
if tar xf test.tar $FILE -O | grep "pattern" ;then
echo "found pattern in : $FILE"
fi
done