Monday, April 4, 2022

[SOLVED] Search for multiple paths in a tar archive

Issue

I know I can search for multiple strings like below in a tar

tar -tf $file |  egrep -i 'str1|str2|str3'

that gives me many paths I am not interested like e.g. the string str1 is bfoa

/data/misc/user/0/info.bfoa.com
/data/app/info.bfoa.com
/data/data/info.bfoa.com

and if str2 is ubercab

/data/misc/user/0/com.ubercab
    /data/app/com.ubercab
    /data/data/com.ubercab

are matches from tar listing. From these I only need ones that are /data/data/*string* pattern like e.g. /data/data/com.ubercab

I tried

 tar -tf $file |  egrep -i '/data/data/*str1*|/data/data/*str2*|/data/data/*str3*'

It says

egrep: repetition-operator operand invalid

so how do I get these things in a tar search in just that 1 grep attempt:
-List of searches search1|search2|search3
-Searches should be regexes -search string should search through paths for a match
-want just the parent directory /data/data/com.ubercab & not its content displayed


Solution

I think the regex pattern you want for egrep is:

tar tf bundle.tar | egrep -E "/data/data/.*(str1|str2|str3).*"


Answered By - Andrew Philips
Answer Checked By - Dawn Plyler (WPSolving Volunteer)