如何在使用 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
解压后保留路径.
我试过了,但也不行:
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屋!