dtdt0454
2012-09-21 14:35
浏览 156
已采纳

tar文件保留完整路径。 怎么阻止它?

I am trying to create a tar archive on my server through PHP as follows:

exec('tar -cvf myfile.tar tmp_folder/innerfolder/');

It works fine, but the saved file preserves the full path including tmp_folder/innerfolder/ I am creating those on the fly for users, so it's a bit unusable for users to have this path while extracting. I have reviewed this topic - How to strip path while archiving with TAR , but in the explanation the guy doesn't give an example, and I don't quite understand what to do.

Please, tell me with an example, how to add files to tar in a way that it does not preserve the 'tmp_folder/innerfolder/' part in archive?

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

我试图通过PHP在我的服务器上创建一个tar存档,如下所示:

exec('tar -cvf myfile.tar tmp_folder / innerfolder /');

它工作正常,但保存的文件保留了完整路径,包括tmp_folder / innerfolder / 我 为用户动态创建那些,因此用户在提取时使用此路径有点无法使用。 我已经回顾了这个主题 - 如何在使用TAR归档时去除路径,但在解释中,这家伙没有给出一个例子,我不太明白该怎么做。

请告诉我一个例子,如何以不保存档案中'tmp_folder / innerfolder /'部分的方式将文件添加到tar?

提前致谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • douluo5937 2012-09-21 14:39
    已采纳

    you can cheat..

    exec('cd /path/to/tmp_folder/ && tar -cvf /path/to/myfile.tar innerfolder/');
    

    This would would give your users just the innerfolder when they extracted the tarball

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshi1966 2012-09-21 14:39

    Use the -C option to tar:

    tar -C tmp_folder/innerfolder -cvf myfile.tar .
    
    评论
    解决 无用
    打赏 举报
  • duanmao2650 2012-09-21 14:45
    tar czf ~/backup.tgz --directory=/path filetotar
    
    评论
    解决 无用
    打赏 举报
  • doulangpeng3933 2013-07-12 19:08

    You can use --transform

    tar -cf files.tar --transform='s,/your/path/,,' /your/path/file1 /your/path/file2
    tar -tf files.tar
    file1
    file2
    

    More info: http://www.gnu.org/software/tar/manual/html_section/transform.html

    评论
    解决 无用
    打赏 举报
  • doumeng1089 2013-11-01 16:59

    If you want to preserve the current directory name but not the full path to it, try something like this (executed from within the directory that you want to tar; assumes bash/zsh):

    ORIGDIR=${PWD##*/}
    tar -C `dirname $PWD` -cvf ../archive.tar $ORIGDIR
    

    Here's some detail; first:

    ORIGDIR=${PWD##*/}
    

    .. stores the current directory name (i.e. the name of the directory you're in). Then, in the tar command:

    -C `dirname $PWD`
    

    .. switches tar's "working directory" from the standard root ("/") to the parent of the folder you want to archive. Strangely the -C switch only affects the path for building the archive, but not the location the archive itself will be stored in. Hence you'll still have to prefix the archive name with "../", or else tar will place it within the folder you started the command in. Finally, $ORIGDIR is relative to the parent directory, and so it and its contents are archived recursively into the tar (but without the path leading to it).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题