[英]How do I exclude absolute paths for tar?
I am running a PHP script that gets me the absolute paths of files I want to tar up.我正在运行一个 PHP 脚本,它为我提供了我想要压缩的文件的绝对路径。 This is the syntax I have:这是我的语法:
tar -cf tarname.tar -C /www/path/path/file1.txt /www/path/path2/path3/file2.xls
When I untar it, it creates the absolute path to the files.当我解压它时,它会创建文件的绝对路径。 How do I get just /path
with everything under it to show?我如何只显示/path
下的所有内容?
You are incorrectly using the -C
switch, which is used for changing directories.您错误地使用了-C
开关,该开关用于更改目录。 So what you need to do is:所以你需要做的是:
tar -cf tarname.tar -C /www/path path/file1.txt path2/path3/file2.xls
or if you want to package everything under /www/path
do:或者,如果您想将所有内容打包在/www/path
请执行以下操作:
tar -cf tarname.tar -C /www/path .
You can use -C
switch multiple times.您可以多次使用-C
开关。
If you want to remove the first n leading components of the file name, you need strip-components
.如果要删除文件名的前 n 个前导组件,则需要strip-components
。 So in your case, on extraction, do所以在你的情况下,在提取时,做
tar xvf tarname.tar --strip-components=2
The man page has a list of tar
's many options, including this one.手册页列出了tar
的许多选项,包括这个选项。 Some earlier versions of tar
use --strip-path
for this operation instead.一些早期版本的tar
使用--strip-path
来代替此操作。
For me the following works the best:对我来说,以下方法效果最好:
tar xvf some.tar --transform 's?.*/??g'
--transform
argument is a replacement regex for sed, to which every extracted filepath is fed. --transform
参数是 sed 的替代正则表达式,每个提取的文件路径都被提供给它。 Unlike --strip-components
, it will remove all path information, not just fixed number of components.与--strip-components
不同,它将删除所有路径信息,而不仅仅是固定数量的组件。
If you don't know how many components are in the path, you could try this:如果你不知道路径中有多少个组件,你可以试试这个:
DIR_TO_PACK=/www/path/
cd $DIR_TO_PACK/..
tar -cf tarname.tar $(basename $DIR_TO_PACK)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.