0

When reading the manual of tar command I encountered something wrong with the option --strip-components. The example in the manual is that there is a tar file containing a directory with 3 files. When listed with the tar command it looks like this:

$tar -tf music.tar
practice/
practice/blues
practice/folk
practice/jazz

The manual says the --strip-components option

allow you to strip away a certain number of leading directory components

and

tar --extract --file=music.tar --strip-components=1 folk

will extract the file ‘folk’ into the current working directory.

But when I run this command on Ubuntu, I got this error

tar: folk: Not found in archive
tar: Exiting with failure status due to previous errors

And my tar version is 1.34. Besides, I got file folk extracted successfully when I executed the command below

tar --extract --file=music.tar --strip-components=1 practice/folk

Did I have some misunderstanding about the manual and option --strip-components? Or is there something wrong with the manual?

1 Answer 1

1

That particular example is wrong - you still need to specify the full path of the file inside the archive. Otherwise, given a tar file like:

$ tar -tf foo.tar
a/
a/folk
b/folk
b/blues

There would be no way for tar to tell which file you meant with tar --extract --file=foo.tar --strip-components=1 folk.

The section documenting --strip-components (6.7 Modifying File and Member Names) has a correct example:

For example, suppose you have archived whole ‘/usr’ hierarchy to a tar archive named ‘usr.tar’. Among other files, this archive contains ‘usr/include/stdlib.h’, which you wish to extract to the current working directory. To do so, you type:

$ tar -xf usr.tar --strip=2 usr/include/stdlib.h

The option ‘--strip=2’ instructs tar to strip the two leading components (‘usr/’ and ‘include/’) off the file name.

If you add the ‘--verbose’ (‘-v’) option to the invocation above, you will note that the verbose listing still contains the full file name, with the two removed components still in place.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .