Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

5 Answers 5

up vote 8 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
1/.htaccess
2/.htaccess
$ 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

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

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

Your Answer

 
discard

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.