使用TAR归档时如何剥离路径 [英] How to strip path while archiving with TAR
问题描述
我有一个文件,其中包含要使用tar存档的文件列表.
我们称之为mylist.txt
I have a file that contain list of files I want to archive with tar.
Let's call it mylist.txt
它包含:
/path1/path2/file1.txt
/path1/path2/file3.txt
...
/path1/path2/file10.txt
我要做的是将此文件归档到tarball中,但不包括/path1/path2/
.
目前正在执行此操作:
What I want to do is to archive this file into a tarball but excluding /path1/path2/
.
Currently by doing this:
tar -cvf allfiles.tar -T mylist.txt
取消存档后保留路径.
我尝试了此操作,但也无法正常工作
I tried this but won't work too:
tar -cvf -C /path1/path2 allfiles.tar -T mylist.txt
它会将/path1/path2
中的所有文件存档,即使那些不在mylist.txt中的文件也是如此
It archives all the files in /path1/path2
even those which are not in mylist.txt
有办法吗?
推荐答案
在提取阶段"中,您可以使用strip-components
标志,例如
In your "Extraction phase" you can use the strip-components
flag like
tar xvf tarname.tar --strip-components=n
这将删除文件名的前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.
如果您想在归档时执行此操作,则只会想到一件事,我将与您分享
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)每行,将路径从文件名中分离出来
1) for each line, split the path out of the filename
2)执行cd到该路径,并在该文件名上执行tar
2) execute cd to that path and tar on that filename
3)对每一行重复
这篇关于使用TAR归档时如何剥离路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!