forum.test17
forum.test17

Reputation: 2209

How to prevent tar extraction from overwriting symbolic links directories

Note:Overwriting of the symlinks occurs from tar version 1.27 or higher

Below I am trying to show what exactly the problem is.

contents of the dirtmp1

file1.txt
file2.txt

code to create the above directory

rm -f -r dirtmp1 && mkdir dirtmp1 && cd dirtmp1 && touch file1.txt && touch file2.txt && ls -al

creating a symbolic link

cd ..
ln -s dirtmp1/ symlink1

now create the tar file which contains the name as symlink1

mkdir dirtmp1
cd dirtmp1
mkdir symlink1 && cd symlink1 && touch iNeedThisfile.txt && cd .. && tar -cvzf symlink1.tar.gz symlink1/

Extract the tar file in folder(symlnk1) is overwriting the symbolic link. All I want is preserve the symbolic link and copy the "iNeedThisfile.txt"

After running this command tar -xvf symlink1.tar.gz

symlink1: total 0 -rw-r--r-- 1 root root 0 Mar 24 18:14 iNeedThisfile.txt

Any flags while extracting which preserves the symbolic links while extracting. and copies the files to the folder pointed by the symbolic link.

I apologise for not able to convey my message in fewer lines of text.

Upvotes: 8

Views: 18739

Answers (4)

textral
textral

Reputation: 1049

Ubuntu 12's old version of tar didn't have the --keep-directory-symlink option, and -h didn't work for me either, so I did this instead:

tar -tf file.tar | grep -v /$ | xargs tar -xf file.tar

this extracts just the files in the archive, bypassing any directory (or symlink to directory) processing

Upvotes: 0

TarAndFeathered
TarAndFeathered

Reputation: 1

What worked for me is --keep-directory-symlink. (I believe that -h only applies to creating tar files.)

This option has been in Gnu tar for almost 10 years, but I think Ubuntu only started exposing it in 20.04/focal (the 18.04 man page does not mention the option).

Aside: What's really weird for me is that I only needed to use --keep-directory-symlink when I ran the command remotely via SSH (e.g. ssh [host] "tar xzvf tarball-with-unsymlinked-directory-paths.tgz"). When I extracted the same tarball directly in a local interactive login session on the same machine I did not need --keep-directory-symlink. No idea why. (This is on an Ubuntu 22.04 system.) It seems Ubuntu's tar is not quite the standard Gnu tar.

Upvotes: 0

xpt
xpt

Reputation: 22994

I'm using Debian and the answer provided by Brian doesn't work for me. Rereading the OP again, I now realized that the OP want to preserve symlink of a directory, whereas I want to preserve the symlink of each individual files. But since this is the only hit I found on this, and since I've already typed out my answer, I'll provide it anyway, for preserving the symlink of each individual files.

So here is the solution that I found out.

First of all, my Debian as we speak:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

and my tar is 1.30+dfsg-6:

$ apt-cache policy tar
tar:
  Installed: 1.30+dfsg-6
  Candidate: 1.30+dfsg-6
  Version table:
 *** 1.30+dfsg-6 500
        500 http://deb.debian.org/debian buster/main amd64 Packages
        100 /var/lib/dpkg/status

What I found is,

  • tar -h -xvf symlink1.tar.gz will not work. Extracting the contents this way will remove the symlinks. tar -h --no-overwrite-dir -xvf symlink1.tar.gz will not work either. But,
  • tar -h --overwrite -xvf symlink1.tar.gz will work! Extracting the contents this way will keep the symlinks of each individual files, But,
  • tar -h --overwrite -xvJf symlink1.tar.xz will not work. I.e., what works for .tar.gz will not work for .tar.xz files.

HTH for anyone wanting to preserve the symlink of each individual files.

Upvotes: 1

Brian
Brian

Reputation: 151

I had the same problem. In my case, tar 1.23 had the proper behavior (preserved the symbolic link) while 1.26 had the "new" behavior (deleted the symbolic link and created a directory instead).

I found adding the -h flag to the tar on the EXTRACT does the job. The symbolic link is preserved and the file(s) are added to the directory it points to.

E.g., I had to go from

tar zxf foo.tar.gz

to

tar -h -zxf foo.tar.gz

Upvotes: 15

Related Questions