Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 793
forum.test17 Avatar asked Mar 24 '15 17:03

forum.test17


1 Answers

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
like image 134
Brian Avatar answered Sep 30 '22 05:09

Brian