bash - batch script for extracting one file from multiple tar files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash - batch script for extracting one file from multiple tar files
# 1  
Old 11-13-2007
Question bash - batch script for extracting one file from multiple tar files

so i have hundreds of files named history.20071112.tar
(history.YYYYMMDD.tar)

and im looking to extract one file out of each archive called status_YYYYMMDDHH:MM.lis

here is what i have so far:

for FILE in `cat dirlist`
do
tar xvf $FILE ./status_*
done

dirlist is a text containg the full path to the tar files i want to extract the single file from:
/export/home/psxfer/history/archive/history.20070101.tar
/export/home/psxfer/history/archive/history.20070102.tar
/export/home/psxfer/history/archive/history.20070103.tar
/export/home/psxfer/history/archive/history.20070104.tar
/export/home/psxfer/history/archive/history.20070105.tar
etc...

the problem is that it doesnt look like tar is liking the wildcard and needs the exact file name of the single file to extract so it's not working.

anyone have any ideas? or would i need to run tar -tf history.YYYYMMDD.tar for each file to get the exact filename for the .lis file?

thanks in advance!!

i forgot to mention that this is solaris 8 if that makes a difference.

Last edited by kuliksco; 11-13-2007 at 01:11 AM..
# 2  
Old 11-13-2007
Well, this *should* work. There are caveats, tho. First, the pattern should be quoted, since it will be expanded by the shell if it matches any files in the current directory. Second, the filename must match precisely, path and all. Are you sure the file is stored in the archive as "./foo.bar" ?
# 3  
Old 11-13-2007
Quote:
Originally Posted by gus2000
Well, this *should* work. There are caveats, tho. First, the pattern should be quoted, since it will be expanded by the shell if it matches any files in the current directory. Second, the filename must match precisely, path and all. Are you sure the file is stored in the archive as "./foo.bar" ?
hmm...i tried with the quotes but the ./status_* still isnt finding a match. here is the contents of one of the tar files:

tar -tf /export/home/psxfer/history/archive/history.20070101.tar
./status2_2007010107:00.lis.gz
./status3_2007010107:00.lis.gz
./status_2007010107:00.lis.gz
./RSCAR084_482134.pdf.gz
./rscem001_482111.lis.gz
./rscem002_482114.lis.gz
./rscem006_482110.lis.gz
./rscem006_482113.lis.gz
./rscbi068_482140.lis.gz
./RSCPT002_482144.pdf.gz
./rscpt003_482143.txt.gz
./rscpt004_476773.txt.gz
# 4  
Old 11-13-2007
Curious, I didn't have a problem extracting from tar via wildcards on OSX (it was the nearest Unix machine). I guess you'll need to extract the name first, as you already suggested, although it means scanning each archive twice (arrrrrg).
# 5  
Old 11-13-2007
Quote:
Originally Posted by gus2000
Curious, I didn't have a problem extracting from tar via wildcards on OSX (it was the nearest Unix machine). I guess you'll need to extract the name first, as you already suggested, although it means scanning each archive twice (arrrrrg).
ok got it working. i ended up using:

for FILE in `cat $1`
do
LIS=`tar -tf $FILE | grep ./status_`
tar -xvf $FILE $LIS
done

easier than i thought. not sure if there was a better way other than using grep.

thanks again for the help.

Last edited by kuliksco; 11-13-2007 at 11:37 AM..
This User Gave Thanks to kuliksco For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux read specific content file from tar.gz files without extracting

hello i wish to write the result of these below conditions in a file: 1. in a specific folder, i have many tar.gz files. 2. each tar.gz file contains ".dat" file in sub folders. 3. i wish to get the full path of these .dat files, if i find in it a specific word ("ERROR24"). 4. all this... (6 Replies)
Discussion started by: jimmyjames9
6 Replies

2. Shell Programming and Scripting

Bash not removing all .tar.bz2 files after extracting

In the bash below each .tar.bz2 (usually 2) are extracted and then the original .tar.bz2 is removed. However, only one (presumably the first extracted) is being removed, however both are extracted. I am not sure why this is? Thank you :). tar.bz2 folders in /home/cmccabe/Desktop/NGS/API ... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Extracting .tar.gz files

I have a directory tree containing archive .tar.gz files that I want to extract at the location where they recide. How can I achieve such an operation? (7 Replies)
Discussion started by: kristinu
7 Replies

4. Shell Programming and Scripting

Need help in batch renaming files with bash shell script.

I have some 50+ files in the following format : abcd_vish_running_ZEBRA_20140818.dat_08-14-2014_23:08:23 abcd_vish_running_ZEB-RA_20140818.dat_08-14-2014_23:08:35 abcd_vish_running_ZEB_RA_20140818.dat_08-14-2014_23:08:37 abcd_vish_running_RI-NG_20140818.dat_08-14-2014_23:08:42... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. Shell Programming and Scripting

Extracting non multiple files via script

Hi, Can somebody help me? I am testing a demo with the given function PATH525="/uscms/home/emily/READme/extra/data/" TEMP=temp FileName=DataFileName CopyFiles() { # PATHNAME="$paths" ... (9 Replies)
Discussion started by: emily
9 Replies

6. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

7. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

8. UNIX for Dummies Questions & Answers

Extracting specific files from a tar file in HP-UX

I have tried: tar -xfv mytarfile.tar archive/tabv/* tar -xfv mytarfile.tar --wildcards 'archive/tabv/*' tar -xf mytarfile.tar -v --wildcards 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards --no-anchored 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards `archive/tabv/*` and none... (5 Replies)
Discussion started by: zapper222
5 Replies

9. Shell Programming and Scripting

Extracting .tar files.

Hey guys complete n00b here so I'll try my best at explaining. I'm creating a backup and restore utility and decided to use tar. I create a backup folder in each user's account and when backing up (say word processing files), I use the following: tar cvf /home/user/backup/wpbackup.tar... (2 Replies)
Discussion started by: EwanD
2 Replies

10. UNIX for Advanced & Expert Users

How to create a Tar of multiple Files in Unix and FTP the tar to Windows.

Hi, On my Unix Server in my directory, I have 70 files distributed in the following directories (which have several other files too). These files include C Source Files, Shell Script Source Files, Binary Files, Object Files. a) /usr/users/oracle/bin b) /usr/users/oracle... (1 Reply)
Discussion started by: marconi
1 Replies
Login or Register to Ask a Question