【问题标题】:tar/bzip files without including the folder structuretar/bzip 文件,不包括文件夹结构
【发布时间】:2011-09-30 19:23:29
【问题描述】:

示例设置:文件夹结构

/folder1/folder2/folder3

文件夹 3 有 2 个文件:

  • sample.backups.tar
  • sample

我的目标是运行一个 bzips 示例并将其附加到 sample.backups.tar 的脚本,删除 bzip 之外的压缩文件。

我写了以下几行

folder3Path=#full path for folder3
samplePath=#full path with file name for sample file
compressedSamplePath=#full path with file name for bzipped sample file to be created
sampleBackupsPath=#full path with file name for sample.backups.tar

tar -jcf $compressedPath sample
tar -r --file=$sampleBackupsPath $compressedSamplePath --remove-files

结果是在 sample.backups.tar 我有文件结构 /folder1/folder2/folder3/sample.tar.bz2。

我不想要那里的文件夹结构;我只希望 sample.tar.bz2 出现在该文件中,而没有文件夹。我将如何做到这一点?

尝试过的事情:

tar -jcf $compressedPath sample
tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSamplePath --remove-file

尝试将文件夹更改为压缩示例文件,但它仍然给了我文件夹结构

注意:

对于我的要求,我需要对所有名称引用使用绝对路径和完整路径。 我正在使用 tar 的附加模式,因为我需要使用同一个 tar 来存储多个 bzip。

【问题讨论】:

    标签: bash compression tar


    【解决方案1】:

    你试过了吗:

    `tar -jcf $compressedPath sample`
    filename=$(basename $compressedSamplePath)
    `tar -r --file=$sampleBackupsPath $filename --remove-files`
    

    【讨论】:

    • 只要我从文件名的位置运行脚本,你的建议就可以工作,在我的情况下,这不是 garenteed,这就是使用绝对路径的原因
    • 您为什么要尝试执行 tar 命令的输出? Markdown 反引号与“我真的希望它们在 shell 中”反引号有问题吗?
    • 我是shell脚本的新手,我真的不知道将它与` `一起使用的效果。我看到了其他一些方法,这个有效,这就是我所关心的。但我很想知道更多
    • 从哪里执行脚本并不重要。无论 $compressedSamplePath 中出现什么绝对路径名,基本名称都将仅返回我理解您想要的文件名。
    【解决方案2】:
    `tar -C $folder3Path -jcf $compressedSamplePath sample`
    `tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName`
    `rm $compressedSamplePath`
    

    我最终以上述方式解决了问题,但我不明白为什么以下方法不起作用。

    `tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName --remove-files`
    

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      相关资源
      最近更新 更多