2

Is there a more efficient way to do this other than using tar tf and checking the output of every file against the file that I'm looking for?

This is the way I do it right now, but it's very slow (there are about 600-1000 archives that fit the search_patterns):

ARRAY=()
ARRAY[0]=/path/to/archives/*search_pattern1*
ARRAY[1]=/path/to/archives/*search_pattern2*
ARRAY[2]=/path/to/archives/*search_pattern3*

for f in ${ARRAY[@]}
do
    if [[ $f =~ "matching_pattern1" ]]; then
        if tar -tf "$f" | grep "matching_pattern2" ; then
            printf "%s\n" $f;
            exit 0;
        fi
    fi
done

For what it's worth, my search_patterns are 3 consecutive days, and I want first to find the archives that match the matching_pattern1 and then look through all these archives for matching_pattern2 and output the tar file that contains it.

closed as off-topic by terdon Oct 28 '14 at 18:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – terdon

3

Tar files don't have directory (like e.g. a zip file) Thus doing tar tf is the only thing you can do.

If you have to run this multiple times then of course you can list the contents of each tar file in a file and search through that:

tar tvf oneofthe.tar > oneofthe.tar.lst

I would even do so if there is only a slight chance of having to rerun the search, as it is hardly slower to do this first and then search instead of searching (e.g. with grep) on the output of the tar tf

Not the answer you're looking for? Browse other questions tagged or ask your own question.