Linux 如何在使用 TAR 归档时剥离路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8043579/
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
How to strip path while archiving with TAR
提问by neversaint
I have a file that contain list of files I want to archive with tar.
Let's call it mylist.txt
我有一个文件,其中包含要使用 tar 存档的文件列表。让我们称之为mylist.txt
It contains:
它包含了:
/path1/path2/file1.txt
/path1/path2/file3.txt
...
/path1/path2/file10.txt
What I want to do is to archive this file into a tarball but excluding /path1/path2/
.
Currently by doing this:
我想要做的是将此文件存档到 tarball 中,但不包括/path1/path2/
. 目前通过这样做:
tar -cvf allfiles.tar -T mylist.txt
preserves the path after unarchiving.
解压后保留路径。
I tried this but won't work too:
我试过这个,但也行不通:
tar -cvf -C /path1/path2 allfiles.tar -T mylist.txt
It archives all the files in /path1/path2
even those which are not in mylist.txt
它将所有文件归档,/path1/path2
即使是那些不在 mylist.txt 中的文件
Is there a way to do it?
有没有办法做到这一点?
采纳答案by hovanessyan
In your "Extraction phase" you can use the strip-components
flag like
在您的“提取阶段”中,您可以使用strip-components
类似的标志
tar xvf tarname.tar --strip-components=n
which will remove the first n leading components of the file name. Although if you have different file-path-components this will not work for all cases.
这将删除文件名的前 n 个前导组件。尽管如果您有不同的文件路径组件,这不适用于所有情况。
If you want to do it while archiving, only one thing comes to mind, and I will share
如果你想一边存档一边做,只想到一件事,我会分享
INPUT: list of files + full paths
输入:文件列表+完整路径
1) for each line, split the path out of the filename
1)对于每一行,将路径从文件名中分离出来
2) execute cd to that path and tar on that filename
2) 执行 cd 到该路径并在该文件名上执行 tar
3) repeat for each line
3)对每一行重复