0

sample setup: folder structure

/folder1/folder2/folder3

folder3 has 2 files:

  • sample.backups.tar
  • sample

My objective to run a script that bzips sample and appends it to sample.backups.tar, remove the compressed file outside bzip.

I wrote the following lines

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

The result was that inside sample.backups.tar I have the file structure /folder1/folder2/folder3/sample.tar.bz2.

I don't want the folder structure there; I want only sample.tar.bz2 to be in that file, without the folders. How would I accomplish that?

Things tried:

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

Attempted to change the folder to compressed sample file, but it still gives me the folder structure

Note:

For the requirement I have, I need to use absolute and full paths for all name references. I am using append mode for tar as I need to use the same tar to store multiple bzips.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Schu
  • 1,124
  • 3
  • 11
  • 23

2 Answers2

1

did you try:

`tar -jcf $compressedPath sample`
filename=$(basename $compressedSamplePath)
`tar -r --file=$sampleBackupsPath $filename --remove-files`
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • your suggest would work as long as I run the script from the location of filename, which is not gaurenteed in my case, thats the reason behind using absolute paths – Schu Sep 30 '11 at 19:35
  • Why are you trying to execute the output of the tar commands? Is it a problem with Markdown backticks vs "I really want them in the shell" backticks? – Jonathan Leffler Sep 30 '11 at 19:37
  • i am a newbie in shell scripting, I dont really know the effects of using it with ` `. I saw some other ways, this one worked, thats all I cared. But I would love to know more – Schu Sep 30 '11 at 19:41
  • It doesn't matter where you execute the script from. Whatever absolute path name appears in $compressedSamplePath, base name will return only the filename which is from what I understand you want. – ennuikiller Sep 30 '11 at 21:04
0
`tar -C $folder3Path -jcf $compressedSamplePath sample`
`tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName`
`rm $compressedSamplePath`

I ended up solving the problem in the above fashion, but I dont understand why the following would not work.

`tar -C $folder3Path -r --file=$sampleBackupsPath $compressedSampleName --remove-files`
Schu
  • 1,124
  • 3
  • 11
  • 23