tar / bzip文件,不包括文件夹结构

时间:2011-09-30 19:23:29

标签: bash compression tar

示例设置:文件夹结构

/folder1/folder2/folder3

folder3有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来存储多个bzips。

2 个答案:

答案 0 :(得分:1)

你试过了吗?

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

答案 1 :(得分:0)

`tar -C $folder3Path -jcf $compressedSamplePath sample`
`tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName`
`rm $compressedSamplePath`

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

`tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName --remove-files`
相关问题