untar .tar.gz file to a specific file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting untar .tar.gz file to a specific file
# 1  
Old 07-21-2011
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
Code:
/local/server/source/path/remote_file.txt.tar.gz

Extracted file
Code:
/local/server/destination/path/un-tar/stage_file.txt

Command I'm using
Code:
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
# 2  
Old 07-21-2011
If your tar implementation supports the --transform option:

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

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

# 3  
Old 07-21-2011
I don't have --xform option with my tar Smilie

I'm on
Code:
 
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
# 4  
Old 07-21-2011
Why don't you just rename the file after extracting it?
# 5  
Old 07-21-2011
I am confused. if the tarball contained the file "remote_file.txt" then
Code:
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:
Code:
tar -zxvf /local/server/source/path/remote_file.txt.tar.gz -O > /local/server/destination/path/un-tar/stage_file.txt

These 2 Users Gave Thanks to DoxieLvr For This Post:
# 6  
Old 07-22-2011
Hi DoxieLvr,
Quote:
Originally Posted by DoxieLvr
I am confused. if the tarball contained the file "remote_file.txt" then
Code:
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.
[/CODE]
That's true!! I might have tried many options Smilie but pasted here something wrong!! Smilie

Quote:
Originally Posted by DoxieLvr
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:
Code:
tar -zxvf /local/server/source/path/remote_file.txt.tar.gz -O > /local/server/destination/path/un-tar/stage_file.txt

Works perfect!! Smilie. Thanks so much!!

Hi radoulov,
Quote:
Originally Posted by radoulov
Why don't you just rename the file after extracting it?
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

2. Shell Programming and Scripting

Script to Tar file in a specific Directory

I'm trying to write a Unix script that will go to a specific directory (/tmp/Sanbox/logs) and tar.gz all the log files in that directory and delete the original files that are older than 2 days. So far I have this but it doesn't work. Any help would be appreciated. #!/bin/bash ... (7 Replies)
Discussion started by: Loc
7 Replies

3. Solaris

HOW TO extract.tar file to specific directory..?

Hi all, In Solaris howto extract tar file to specific folder. This is what we do in Linux, but how to do the same thing in Solaris ? -tar -xzvf /tmp/etc.tar.bz -C /tmp (Will extract in /tmp dir) 3.gzip COMPRESSION AND EXTRACTION -tar -czvf /tmp/etc.tar.bz /etc -du ... (5 Replies)
Discussion started by: manalisharmabe
5 Replies

4. Shell Programming and Scripting

scp/untar .tar file in parallel issue

Hi Guys, I am facing a strange issue while doing parallel (using & for background) scp/untar operation from my unix box to multiple unix boxes... I am getting tar : unexpected EOF in archive error the code is as follows.,,, for i in 10 do sh -c "scp <command> ; ssh tar -xf <tar> -C... (4 Replies)
Discussion started by: mihirvora16
4 Replies

5. UNIX for Dummies Questions & Answers

untar a tar file

how can I untar a file without extracting it? sample: file.tar to file thanks, lara (1 Reply)
Discussion started by: lhareigh890
1 Replies

6. Shell Programming and Scripting

Script to untar latest tar file

I am trying to put together a script that will check for the latest file in a directory then extract it. The extraction and the scheduling I can do, but am not sure how to get it to check for the latest file. These files are uploaded every evening by an external party and the previous days files... (3 Replies)
Discussion started by: stheologo
3 Replies

7. UNIX for Advanced & Expert Users

Tar utility (untar a .tar file) on VxWorks

Hi All Can someone pls guide me if there any utility to compress file on windows & uncompress on vxworks I tried as - - compressed some folders on windows ... i created .tar ( to maintain directory structure ) and compressed to .gz format. - on VxWorks i have uncompressed it to .tar... (1 Reply)
Discussion started by: uday_01
1 Replies

8. UNIX for Dummies Questions & Answers

Untar a TAR file at different location

Hi, I want to UNTAR a TAR file at different location. Is it possible? My TAR file contains the files with absolute path. Malay (5 Replies)
Discussion started by: malaymaru
5 Replies

9. UNIX for Dummies Questions & Answers

tar - restore only file of specific dates

hi there, anybody know if there is any efficient way of restoring only files of specific dates from a tape (with tar command)? :rolleyes: coz the tapes containing few weeks' files, but i need only files of a few days..... any kind feedback is appreciated. Thanks in advanced. (0 Replies)
Discussion started by: newbie168
0 Replies

10. Shell Programming and Scripting

unTar a specific file from remote

During tar, the command used is tar cvf - * | remsh system_name dd of=/dev/rmt/0m bs=10k To untar all, we used remsh system_name "dd if=/dev/rmt/0m ibs=10k" | tar xvf - Question? How to untar a specific file from remote? Thanks alot... (2 Replies)
Discussion started by: gelbvonn
2 Replies
Login or Register to Ask a Question