I have directory tree with many tar files. Each tar file contains many other files. I want to search through all the tar files for a given pattern, and print the full path of any files within a tar that are found.

I got this far: find . -type f -name '*.tar' -exec tar tf {} \; | egrep '<pattern>'

Now my problem is that the above command only prints the name of the file within the tar that it finds. Ie, the output of the above is:
pattern.jpg foundMe.txt

I would like the output to contain also the full path and the tar name where the file was found. How would I do this?

Thank you!

  • What is your pattern -- it is capturing only the filiename? For me find returns the full path relative to whence I'm executing the command by default.
    – eebbesen
    Nov 10, 2014 at 12:27
  • Yes, find does do that, but then -exec tar tf only returns the name of the file within the tar archive Nov 10, 2014 at 12:28
  • Have you tried execdir? See this question.
    – eebbesen
    Nov 10, 2014 at 12:32
  • that doesn't help, tar tf still only outputs the name of the file, I need to combine the tar file path from <find> command with result of <tar tf | egrep 'pattern'> Nov 10, 2014 at 13:31

1 Answer 1


Of course, lost the plot somewhere along the line :$

I had a similar problem with jar files, looking for java classes. Used to do something like this: capture the output of each tar tf|grep <pattern> then look see if the output contains a value - if so, print name ($f) and value ($p):

find . -name '*.tar' -type f|while read f; do p="$(tar tf $f|egrep <pattern>)"; [ -n "$p" ] && echo -e "$f\n$p" ; p=""; done

Switched the order of find arguments - cheaper to look at file name before file type.

  • I am searching for files within tar archives, this doesn't help Nov 10, 2014 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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