Problem creating a tar ball in different directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem creating a tar ball in different directories
# 1  
Old 02-06-2017
Problem creating a tar ball in different directories

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.

Code:
#!/bin/ksh

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

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

This gives the following error

Code:
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.

Code:
$(cd $LOG && tar -zcvf $STAGE/${FILENAME} *)

but that gives an error, although it appear to actually create the gz file correctly.
Code:
 "./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.
# 2  
Old 02-06-2017
I don't understand why you have your tar command embedded in a process substitution:
Code:
$(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:
Code:
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:
Code:
tar -zcv -C ${LOG} -f ${STAGE}/${FILENAME} .

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 02-06-2017
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 ...

Code:
cd ${LOG} && tar -zcf ${FILENAME} * && cp ${FILENAME} ${STAGE}

# 4  
Old 02-06-2017
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.

Code:
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.
# 5  
Old 02-07-2017
Quote:
Originally Posted by 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 ...

Code:
cd ${LOG} && tar -zcf ${FILENAME} * && cp ${FILENAME} ${STAGE}

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

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create Tar ball

Hi Team, Following unix command is throwing error. Can anyone please help me to fix the issue? tar -cvf /aa/bb/cc/tarball1.tar /x/y/z1/abc.ksh /x/y/z2/pqr.txt /x/y/z3/lmn.tmp Error message thrown: tar: Removing leading `/' from member names OS: uname -a Linux xyz... (1 Reply)
Discussion started by: kmanivan82
1 Replies

2. Shell Programming and Scripting

Untar only folder structure from a tar ball

I have a tar file hello.tar which is 95 GB. hello.tar has many files and folders including some tar files as well. I wish to create a new tar ball which should maintain only the folder structure of hello.tar and the tar ball within the hello.tar So basically the idea is to untar... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

Need Tar Ball command to exclude directories

Hi, uname -a SunOS mymac 5.11 11.2 sun4u sparc SUNW,SPARC-Enterprise I need to tar a folder /tmp/moht but do not want these three folders to be included in the tar file -> savejpg, bmpsave and imgsave I tried --exclude, -path, -not options but it says bad option Can you help me with... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Extract a tar ball into multiple directories

#cat a BAC064DAL BAC063DAL BAC056PHX BAC066DAL BAC062PHX BAC062DAL BAC060DAL BAC058PHX BAC054PHX BAC051PHX # for i in `cat a` > do > tar xvf $a/$a*.tar* > done tar: /*.tar*: Cannot open: No such file or directory tar: Error is not recoverable: exiting now tar: /*.tar*: Cannot... (3 Replies)
Discussion started by: kenshinhimura
3 Replies

5. UNIX for Advanced & Expert Users

Extracting directories only from tar ball

Hi Somebody must have done this before, but I can't seem to find any answer on my problem. On HP-UX 11i v3 I have a relatively large tar ball (~120 GB), and I want to create the directory structure only from the archive. There is no option to make a new archive with only the directory... (3 Replies)
Discussion started by: hpvm_adm
3 Replies

6. Shell Programming and Scripting

Extract files from tar ball without directory structure

Hi, I have tar filw which has multiple directories which contain files. When i extract using tar -xf the directory structure also get extracted. I require only files and not directory structures as there will be overhead of moving the files again. So i searched here and got a solution but... (4 Replies)
Discussion started by: chetan.c
4 Replies

7. Shell Programming and Scripting

Extract contents of tar ball without extracting files

Hi, I'm using a tar command tar -xOvf /home/mytar.tar My intention is to extract data in files which are inside various directories, without extracting files to the disk. Is this the best way to achieve it? Thanks, Chetan (3 Replies)
Discussion started by: chetan.c
3 Replies

8. UNIX for Advanced & Expert Users

Size of a tarball without untarring - Catch parent tar ball has sub tars

hi, I am in a weird situation. I have a parent tarball which contains 2 sub tarballs. The structure is such : Parent.tar.gz ---- > child1.tar.gz and child2.tar.gz I need to get the size of the parent tarball without untaring it I know that the command is gunzip -c parent.tar.gz | wc -c ... (1 Reply)
Discussion started by: mnanavati
1 Replies

9. Shell Programming and Scripting

Problem with array and creating directories

I have an interesting requirement. I have declaried an array like :- arr=`find . ! -name "." | xargs -I {} echo {} | cut -c 2-${#}` Then i will try to access the array elements like :- i=0 for i in ${arr}; do Here comes the confusions, the array elements are basically dir and files stored... (2 Replies)
Discussion started by: Renjesh
2 Replies

10. Solaris

How to extract files from a tar file without creating the directories?

Hello all. I have a tar file that contains a number of files that are stored in different directories. If I extract this tar file with -xvf , the directories get created. Is there a way to extract all of the files into one directory without creating the directories stored in the tar file. (9 Replies)
Discussion started by: gkb
9 Replies
Login or Register to Ask a Question