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):


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

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.


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.