untar .tar.gz file to a specific file

Hi,

I'll get a tarred file from a remote location in the format of .tar.gz which my program needs to un-tar it into a specific destination foler and a file

Incoming file

/local/server/source/path/remote_file.txt.tar.gz

Extracted file

/local/server/destination/path/un-tar/stage_file.txt

Command I'm using

tar -C /local/server/destination/path/un-tar -zxvf /local/server/source/path/remote_file.txt.tar.gz stage_file.txt

but that's not what is happening......the un-tarred file though gets created in path /local/server/destination/path/un-tar but with the name remote_file.txt and NOT stage_file.txt!! Is there a way out?

-dips

If your tar implementation supports the --transform option:

% touch a_file b_file
% mkdir target_dir
% ls -l
total 0
-rw-r--r--  1 sysadmin None 0 Jul 21 13:18 a_file
-rw-r--r--  1 sysadmin None 0 Jul 21 13:18 b_file
drwxr-xr-x+ 1 sysadmin None 0 Jul 21 13:18 target_dir
% tar jcvf test.tar.bz2 *_file
a_file
b_file
% tar xvC target_dir --xform 's/a_file/a_newname/' -f test.tar.bz2 a_file --show-transformed-names
a_newname
% ls target_dir 
a_newname

In your case it should be something like:

tar -C /local/server/destination/path/un-tar --xform 's/remote_/stage_/' \
-zxvf /local/server/source/path/remote_file.txt.tar.gz stage_file.txt

I don't have --xform option with my tar :frowning:

I'm on

 
Linux xxxxxxxxxxxxx 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

-dips

Why don't you just rename the file after extracting it?

I am confused. if the tarball contained the file "remote_file.txt" then

tar -C /local/server/destination/path/un-tar -zxvf /local/server/source/path/remote_file.txt.tar.gz stage_file.txt

should not have untarred it as the filename doesn't match.

In any case, you should be able to use the -O option to output to stdout. For example, if you know that the tarball contains exactly one file, try:

tar -zxvf /local/server/source/path/remote_file.txt.tar.gz -O > /local/server/destination/path/un-tar/stage_file.txt
2 Likes

Hi DoxieLvr,

That's true!! I might have tried many options :confused: but pasted here something wrong!! :o

Works perfect!! :D. Thanks so much!!

Hi radoulov,

The problem is there is very high chance that the client will send us a .tar.gz file having the archived file of something totally different name; I would not know which file(name) it had un-tarred!! Hence this option is not viable.

-dips