3

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?

2 Answers2

6

Try this (omit the initial slash):

tar -tzf backup_2010-09-27.tar.gz etc/passwd
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
1

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 ....

user237419
  • 1,653
  • 8
  • 8