Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to check whether a file is in a gzip'ed tar archive. For example I want to check whether the /etc/passwd is present in the archive backup_2010-09-27.tar.gz

A dumb solution can be:

tar -tzf backup_2010-09-27.tar.gz | grep '/etc/passwd'

But that takes time. Is there a better solution?

share|improve this question
up vote 6 down vote accepted

Try this (omit the initial slash):

tar -tzf backup_2010-09-27.tar.gz etc/passwd
share|improve this answer
that worked! Thanks. – Daniele Sep 27 '10 at 11:15

its gunzipping process that eats time and there's no way to reach the tar archive members before gunzipping. I don't know how much will help to explicitly specify a member offset when listing the tar but if the member lookup process is faster than fully listing then:

tar -K etc/passwd -tzf backup_2010-09-27.tar.gz| head -1

i doubt it tho.

you can also try to extract the file and check the return code so you will get rid of fully listing the archive and grepping afterwise.

echo etc/passwd | tar -T - -K etc/passwd -Oxzf backup_2010-09-27.tar.gz >/dev/null

$? should be zero

still, gunzipping ....

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.