# Problem creating a tar ball in different directories

**URL:** <https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907>\
**Category:** Shell Programming and Scripting\
**Created:** [February 6, 2017, 7:14am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907 "2017-02-06T07:14:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [February 6, 2017, 7:14am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907/1 "2017-02-06T07:14:55Z")

</div>

Hi all. I'm hitting a problem creating a tar archive in one directory from files located in a different directory. It fails when I replace the absolute paths with variables in the script but works if I just run tar on the cmdln. E.g.

```nohighlight
#!/bin/ksh

BASE=$PWD
STAGE=$BASE/stage
LOG=$BASE/log
FILENAME=test.tar.gz

$(tar -zcvf $STAGE/${FILENAME} ${LOG}/*)

```

This gives the following error

```nohighlight
tar: Removing leading `/' from member names

```

I saw some posts about this saying to use -C but that didn't help. Also, tried to change dir in to the log dir and run e.g.

```nohighlight
$(cd $LOG && tar -zcvf $STAGE/${FILENAME} *)

```

but that gives an error, although it appear to actually create the gz file correctly.

```nohighlight
 "./a.ksh[11]: a.b: not found [No such file or directory]"

```

where a.b is the file in the log sub dir that's being tarred.

---

<div class="post-metadata">

**Author:** ![apmcd47](https://community.unix.com/letter_avatar/apmcd47/32/6_22734026db12803ebb3e059622ac3534.png) [@apmcd47](https://community.unix.com/u/apmcd47)\
**Post date:** [February 6, 2017, 7:50am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907/2 "2017-02-06T07:50:13Z")

</div>

I don't understand why you have your tar command embedded in a process substitution:

```nohighlight
$(tar -zcvf $STAGE/${FILENAME} ${LOG}/*)

```

This would cause the output of `tar` (and you have it in verbose mode) being collected into a shell-variable:

```nohighlight
tar_log=$(tar -zcvf $STAGE/${FILENAME} ${LOG}/*)

```

As you are not doing this I can only imagine the output of tar is being interpreted by your shell process.

As for the leading "/" error, tar is attempting to prevent absolute file paths to be entered into the archive so you don't overwrite anything when extracting the archive. I don't know the -C switch but imagine it should be used thus:

```nohighlight
tar -zcv -C ${LOG} -f ${STAGE}/${FILENAME} . 

```

Andrew

---

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [February 6, 2017, 8:58am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907/3 "2017-02-06T08:58:25Z")

</div>

Good points Andrew. Thanks.

Yeah the 'tar: Removing leading `/' from member names' warning is there for our own benefit so I guess it's simpler to just do everything in 3 steps and get around any issues ...

```nohighlight
cd ${LOG} && tar -zcf ${FILENAME} * && cp ${FILENAME} ${STAGE}

```

---

<div class="post-metadata">

**Author:** ![Peasant](https://community.unix.com/user_avatar/community.unix.com/peasant/32/2251_2.png) [@Peasant](https://community.unix.com/u/Peasant)\
**Post date:** [February 6, 2017, 10:31am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907/4 "2017-02-06T10:31:55Z")

</div>

How about `pax`  
The compression is not POSIX, but it should work almost everywhere.  
Notice you cannot append to compressed archive with pax, if you require such feature.

```nohighlight
pax -wzf ${STAGE}/mybackup.tar.gz ${LOG}

```

Will create compressed archive from ${LOG} directory (everything in it) into ${STAGE}/mybackup.tar.gz

It also supports to substitute paths inside archive in one go, and a lot more, in one line.  
Check it out online or on these forums!

Hope that helps  
Regards  
Peasant.

---

<div class="post-metadata">

**Author:** ![apmcd47](https://community.unix.com/letter_avatar/apmcd47/32/6_22734026db12803ebb3e059622ac3534.png) [@apmcd47](https://community.unix.com/u/apmcd47)\
**Post date:** [February 7, 2017, 4:14am UTC](https://community.unix.com/t/problem-creating-a-tar-ball-in-different-directories/364907/5 "2017-02-07T04:14:54Z")

</div>

> [@general\_franco](#):
>
> Good points Andrew. Thanks.
> 
> Yeah the 'tar: Removing leading `/' from member names' warning is there for our own benefit so I guess it's simpler to just do everything in 3 steps and get around any issues ...
> 
> ```plaintext
> cd ${LOG} && tar -zcf ${FILENAME} * && cp ${FILENAME} ${STAGE}
> 
> ```

No, use the absolute path for the target tarball (as in my example).

Andrew
