Extract specific files in a tar archive using a wildcard

18

2

I'm trying to create a script to extract only jpeg pictures from an archive containing many kind of files.

To do so, I tried to use:

   tar -xf MyTar.tar *.jpg

but it failed (*.jpg not found) and suggest me to use "--wildcard". So I tried

tar -xf MyTar.tar --wildcard *.jpg

I did that, but then the same error and a different warning saying to me that the option "--wildcard" is ambiguous.

I've been over the man pages for tar, but didn't find a clue about the problem.

AdrieanKhisbe

Posted 2012-11-12T22:26:51.873

Reputation: 659

Which tar and which version? Paste the results of tar --version – smci – 2017-11-28T06:14:35.117

Answers

32

In the end, I found then answer after a good break. The option is wildcards, plural...

So the command

tar -xf MyTar.tar --wildcards "*.jpg"

did exactly what I needed.

AdrieanKhisbe

Posted 2012-11-12T22:26:51.873

Reputation: 659

3

Put quotes around the wildcard like this "*.jpg" so the shell won't try expanding it and will instead pass it straight through to tar. You want tar to evaluate the wildcard, not the shell and the quotes do that.

Nicole Hamilton

Posted 2012-11-12T22:26:51.873

Reputation: 8 987

0

For some older versions of tar, the following is extracted from the "man" file:

Filename substitution wildcards cannot be used for extracting files from the archive. Rather, use a command of the form:

tar xvf ... /dev/rmt/0 `tar tf ... /dev/rmt/0 | grep 'pattern' `

Ray Peck

Posted 2012-11-12T22:26:51.873

Reputation: 1