How to prevent tar extraction from overwriting symbolic links directories
Asked Answered
A

4

8

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.

Achernar answered 24/3, 2015 at 17:24 Comment(10)
Are you sure this isn't working? This worked for me on a CentOS 5 machine.Luedtke
you want extact one file from tar file ? tar -xvf {symlink1.tar.gz} {path/to/file}Condition
@Etan Reisner no it is is not working on centos 7 , tar version 1.26Achernar
@Hidd3n I do not want to extract single file, but while extracting do not want to override the symbolic link to the file.Achernar
Why u extract tar to another path that not exit symbolic link file?Condition
@Hidd3n I did not get thatAchernar
I means extract tar file to another place has not exit symbolic link when you extract no replace happenedCondition
@Hidd3n That can be the case , but I want to extract on the same place and this worked earlier in tar version 1.20 no longer from tar version 1.27Achernar
Does using the -k flag to tar change anything? (I can try this on a CentOS 7 machine later.)Luedtke
@EtanReisner using the k flag will not overwrite the existing files and reports an error back. I want to overwrite the existing files in my caseAchernar
E
15

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
Emigration answered 1/4, 2015 at 20:47 Comment(4)
@Brain Thanks so much for replying, I read the documentation but missed this -h flag. I will try this :)Achernar
Added -h flag did not work for me in tar version 1.26. Centos 7. Extracting the contents removes the symlink :(Achernar
Works for REHL 7. That piece of sh*t threw 2 days of frustration on me. +1Jongjongleur
I don't know which OS it works, but I'm with @Achernar -- it is not working in my Debian, tar 1.30+dfsg-6 as we speak. Extracting the contents using tar -h -zxf removes the symlink for me too.Rugby
R
1

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.

Rugby answered 15/11, 2020 at 18:38 Comment(0)
L
0

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.

Lifeboat answered 3/3, 2023 at 16:15 Comment(0)
D
0

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

Drily answered 19/9, 2024 at 15:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.