Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 want to extract all .htaccess files from a tarball, how can I do so? I see only one mention of "include" in the manual page of tar which is not related with an option for including only certain filenames at all.

Is there an alternative other than mentioning all paths as argument?

share|improve this question
up vote 9 down vote accepted

GNU tar supports "--wildcards" option.

tar tvfz file.tar.gz --wildcards '*/.htaccess'
share|improve this answer
Wow, I haven't even considered using wildcards for tar. Thanks! – Lekensteyn Feb 22 '12 at 17:12

Not a single command, but this works:

$ tar -ztvf tarball.tar.gz | grep .htaccess | awk '{print $9}'>files
$ cat files
$ tar -zxvT files -f tarball.tar.gz 
x 1/.htaccess
x 2/.htaccess
share|improve this answer
your solution is far more portable than mine. – andcoz Feb 22 '12 at 18:03
This can be simplified to tar tzf tarball.tar.gz | grep .htaccess > files. Before andcoz' answer, I've succesfully used tar xf tarball.tar.gz -T <(tar tf tarball.gz | grep -F .htaccess). I still like andcoz' answer more because it's shorter to type – Lekensteyn Feb 22 '12 at 18:08
Also, don't forget to rm files when done, or you may be quite confused in a month or two.. ;) – Izkata Feb 22 '12 at 21:03
tar tf tarball.tar.gz | grep '/\.htaccess$' | tar xfT tarball.tar.gz -
share|improve this answer
Smart use of the word for standard input (-) – Lekensteyn Feb 22 '12 at 22:16

Just expanding on andcoz's accepted answer to point out the differences between gnutar and bsdtar ...

# tar (GNU tar) 1.26
# bsdtar 2.8.4 - libarchive 2.8.4
# tested in Bash shell on Mac OS X

# only list .htaccess files from archive
bsdtar -T <(echo '*/.htaccess') -tvzf archive.tar.gz
bsdtar --include='*/.htaccess' -tvzf archive.tar.gz
gnutar --wildcards '*/.htaccess' -tvzf archive.tar.gz

# only extract .htaccess files from archive
bsdtar -T <(echo '*/.htaccess') -xvzf archive.tar.gz
bsdtar --include='*/.htaccess' -xvzf archive.tar.gz
gnutar --wildcards '*/.htaccess' -xvzf archive.tar.gz
share|improve this answer

Using pax, the POSIX standard archive utility:

pax -r -s ':/\.htaccess:/.htaccess:' -s ':^\.htaccess:.htaccess:' -s ':.::'

(i.e. extract non-root .htaccess files and root .htaccess files as is; suppress all other files)

share|improve this answer

Your Answer


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

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