Let’s say I have two tar archives generated in slightly different way:

$ tar tvf archive1.tar 
-rw-r--r-- root/root 567 2016-09-18 14:28 member1
-rw-r--r-- root/root 1696 2016-09-18 14:28 member2
$ tar tvf archive2.tar 
-rw-r--r-- root/root 567 2016-09-18 14:28 ./member1
-rw-r--r-- root/root 1696 2016-09-18 14:28 ./member2

How to extract member1 reliably from either of two archives? I’m receiving the tar over a pipe, the generator isn’t under my control, and while I can run the pipeline multiple times, I’d really like to avoid doing so without necessity.


$ tar xOvf archive1.tar member1 > /dev/null
$ tar xOvf archive2.tar member1 > /dev/null
tar: member1: Not found in archive
tar: Exiting with failure status due to previous errors
  • BSD tar, apparently, doesn't have the problem: it worked for me both ways. When I needed a GNU tar format, I ended up mentioning both member1 and ./member1. It exists with an error code, but does the extraction; so I have no guarantee extraction worked. Did you find a solution? – Victor Sergienko Dec 11 '18 at 21:20
  • No, I did not; in fact, I haven’t touched that code since that day :D – andrewsh Dec 22 '18 at 14:14
  • What I was trying to do was write a script which would dump the contents of .deb and .ipk of all existing subformats without requiring dpkg-deb. There is such a script part of MC, but it only supports standard-compliant .deb files and requires dpkg. Many .ipk files do not follow the standard in multiple ways (e.g. using tar instead of ar, packing members in a different order, not providing debian-binary), which makes that script fail. – andrewsh Dec 22 '18 at 14:15
  • Unfortunately, it turned out, the root of the archive is another thing lots of packages (and also ar and tar) cannot agree on: many on them come with ./, while others don’t have this prefix. – andrewsh Dec 22 '18 at 14:18
  • Probably writing such things in shell is a bad idea anyway. – andrewsh Dec 22 '18 at 14:19

It looks like the command line switch --no-anchored may do what you want. From the tar(1) man page (they really are very useful to read or a least scan)

patterns match after any '/' (default for exclusion)

tar -tvf test
-rw-rw-r-- iain/iain         0 2016-09-18 16:14 ./member1
-rw-rw-r-- iain/iain         0 2016-09-18 16:14 ./member2


tar -xvf test  member1
tar: member1: Not found in archive
tar: Exiting with failure status due to previous errors

And then

tar -xvf test --no-anchored member1
  • 1
    I thought about this, but this will work only when the archive is guaranteed to not have member1 anywhere else in the hierarchy, which covers only one of the two cases I have. And sure, obviously I have studied the manpage before posting the question :) – andrewsh Sep 18 '16 at 15:31
  • 1
    You did not state that member1 could appear anywhere in the path or multiple times. – Iain Sep 18 '16 at 15:48
  • It's at least worth mentioning that a solution will work for MCVE, but not in a general case. – Victor Sergienko Dec 11 '18 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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