bash 如何防止符号链接被 tar 文件提取命令覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29239081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:37:21  来源:igfitidea点击:

how to prevent symbolic links overwritten from tar file extract command

linuxbashunixtarcentos7

提问by forum.test17

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

注意:从 tar 版本 1.27 或更高版本开始覆盖符号链接

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

下面我试图说明问题到底是什么。

contents of the dirtmp1

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

现在创建包含名称为 symlink1 的 tar 文件

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"

提取文件夹(symlnk1)中的 tar 文件会覆盖符号链接。我想要的只是保留符号链接并复制“iNeedThisfile.txt”

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

运行此命令后 tar -xvf symlink1.tar.gz

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

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.

我很抱歉无法用更少的文本行传达我的信息。

回答by Brian

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).

我有同样的问题。就我而言,tar 1.23 具有正确的行为(保留符号链接),而 1.26 具有“新”行为(删除符号链接并改为创建目录)。

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.

我发现将 -h 标志添加到 EXTRACT 上的 tar 可以完成这项工作。符号链接被保留,文件被添加到它指向的目录中。

E.g., I had to go from tar zxf foo.tar.gz to tar -h -zxf foo.tar.gz

例如,我不得不从 tar zxf foo.tar.gz 到 tar -h -zxf foo.tar.gz