On MacOS 10.11.6, /usr/bin/tar is "bsdtar 2.8.3 - libarchive 2.8.3". The -P option allows me to extract a tar file through a symlink. That is, if foo1 is a symlink to foo2, and the tar file stores foo1 as a regular directory, I can extract the tar file, and whatever was in foo1 in the tar file will end up in foo2. This is what I want.

However, if there are hard linked files under foo1 they won't extract into foo2. This is strange, since without -P, tar correctly preserves hardlink relationships between file names.

The man page says this about -P:

 -P      Preserve pathnames.  By default ... tar will refuse to extract archive entries
whose pathnames contain .. or whose target directory would be altered by a symlink.  
This option suppresses these behaviors.

Here is a minimal example illustrating the problem:

# Make example directory and files:

~$ mkdir foo1
~$ touch foo1/bar
~$ ln foo1/bar foo1/baz
~$ ls -l foo1
total 0
-rw-------  2 marshall  staff  0 Aug 29 09:19 bar
-rw-------  2 marshall  staff  0 Aug 29 09:19 baz

# Now we have two hardlinked files under foo1.  Let's tar it up:

~$ tar cvf foosball.tar foo1
a foo1
a foo1/bar
a foo1/baz

# Now turn foo1 into a symlink to foo2:

~$ /bin/rm -Rf foo1
~$ mkdir foo2
~$ ln -s foo2 foo1

# Let's try untarring:

~$ tar xPvf foosball.tar
x foo1/
x foo1/bar
x foo1/baz: Cannot extract through symlink foo1/barCan't create 'foo1/baz'
tar: Error exit delayed from previous errors.
~$ ls -l foo?
lrwx------  1 marshall  staff  4 Aug 29 09:19 foo1 -> foo2

total 0
-rw-------  1 marshall  staff  0 Aug 29 09:19 bar

# bar was created, but the other filename hardlinked to it was not.

Is there a set of commandline options that will allow me to extract hardlinked files through a symlinked directory? Is this just a bug or limitation of BSD tar?


Your Answer

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

Browse other questions tagged or ask your own question.