Solved

UNTAR FILES USING WILDCARD

Posted on 2006-07-13
17
899 Views
Last Modified: 2008-01-09
Hello All;

I need to figure out a way to untar a series of files, updated daily, whose names vary each day based upon the date.

Here is the code I am using to untar one file

#!/bin/bash

cd /kunden/homepages/23/d116101914/htdocs/web/mls/images

tar -xvf /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-20060705.tar

The problem is that the name of the .tar file(s) change daily and I cannot be around to unzip them each day. Therefore I need a script that will enable a .tar wilcard.

Any suggestions? I am quite eager to resolve this.

MTN
0
Comment
Question by:mtnwave
  • 7
  • 4
  • 4
  • +1
17 Comments
 

Author Comment

by:mtnwave
ID: 17105430
I might add that I am somewhat of a newbie to shell scripting so be gentle!

MTN
0
 
LVL 24

Expert Comment

by:slyong
ID: 17105715
you can put in the date string like this:

#!/bin/bash
cd /kunden/homepages/23/d116101914/htdocs/web/mls/images
tar -xvf /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-`date '+%Y%m%d'`.tar
                                                                                                                                                ^^^^^^^^^^^^^^^^^

you might noticed that +%Y%m%d will display the date format in 20060714 format.  You might be interested to unzip 1 day before, so instead of [ date '+%Y%m%d' ] you can use [ date -d tomorrow '+%Y%m%d' ]
0
 

Author Comment

by:mtnwave
ID: 17106719
OK
 I had a look a this and not exactly what I am looking for in terms of flexibility. What I would really like is a wildcard that can recognize a text string, say "pics" and uzip this instead of using just the date string that you have supplied.

When I call the {gunzip} or {unzip} functions it work brilliantly. THis would help avoid future problems if the naming convention was alster. I could invoke the same functions as:

unzip *.zip -d /kunden/homepages/23/d116101914/htdocs/web/mls/images

The above code essentiall says inzip any .zip file.  I just feel as though thtis would be a better way to insure against a change to the naming convension.

Nate Flint
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 24

Expert Comment

by:slyong
ID: 17106739
I am not really sure what kind of naming convention that you want to use.  If you could provide examples that would be great.  The naming must be of certain format.  Say now the files are in pics-condos-YYYYMMDD.tar format and you change it to pics-condos-DDMMYYYY.tar format, the script will have to be changed otherwise if the name only changes from pics-condos-YYYYMMDD.tar format to pics-condos-ver2-YYYYMMDD.tar format, you can just replace the front part to * like:

tar -xvf /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*`date '+%Y%m%d'`.tar
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17109411

Or supposing you only want to un-tar the last one:

ls -t /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-*.tar| head -1 |tar xvf -


0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17109424

Ooops, missed the xargs part:

ls -t /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-*.tar| head -1 |xargs tar xvf
0
 

Author Comment

by:mtnwave
ID: 17109628
Thanks Slylong,

tar -xvf /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*`date '+%Y%m%d'`.tar  

does not appear to work. tar does not recognize this "*" as a wildcard.

I am thinking of something that addresses  the lowest common denominator: it will untar  ANY tar files.

In the the previous post, I gave this example for a .zip file:

unzip *.zip -d /kunden/homepages/23/d116101914/htdocs/web/mls/images

This line works well in that it unzips any thing with a .zip extension. Since the tar files on the server all need to be extracted I thought I would do the same for the .tar files.  Since tar does not seem to support using the "*" as a wildcard, I am looking for a way to run a command to extract ALL .tar files in a directly

I am sorry if I threw you with the naming convention thing. I was thinking that if I gave the format of the file name we could play with it in a number of different ways.

So in closing, the directory contains ten files of images most updated daily that need to be extracted every night. To hedge against a change in the naming convention i thought it would be best to write a line or lines that extract all tar files in a directory.

Nate
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 300 total points
ID: 17110150

Like this?:

find /kunden/homepages/23/d116101914/htdocs/web/mls/zips/ -name '*.tar'| -exec tar xvf {} \;



0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 17110158

Ooo-psp remove the '|' before the '-exec'
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 17112835
You could just use a loop:

for i in /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*`date '+%Y%m%d'`.tar; do tar -xvf  $i;done

You can only give tar one file to untar at a time - all other args are treated as filenames to restore from the archive. Hence the use of a shell for loop.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 17112844
More simply (and with extra spaces for clarity):

for i in /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*.tar ; do tar -xvf  $i ; done
0
 

Author Comment

by:mtnwave
ID: 17122011
MikeOM;

I have tried this script
ls -t /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-*.tar| head -1 |xargs tar xvf
and it did not work.

Same with
find /kunden/homepages/23/d116101914/htdocs/web/mls/zips/ -name '*.tar' -exec tar xvf {} \
this error was returned:
"find: missing argument to `-exec'"

duncan_roe
for i in /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*`date '+%Y%m%d'`.tar; do tar -xvf  $i;done

Returned this error:
tar: /kunden/homepages/23/d116101914/htdocs/web/mls/zips/*20060717.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
0
 

Author Comment

by:mtnwave
ID: 17122037
Slylong your answer is the one that seems to work, however it does not trtuly use a wildcard.

The other options supplied (thank you) don't seem to fly on the system I am working with.

Any other thoughts?


Thanks
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 200 total points
ID: 17122513
There were no files that matched the wildcard *20060717.tar. The shell command "shopt -s nullglob" would prevent this error, but no files would be untarred. Perhaps the path is wrong? - it was only an example.

find /kunden/homepages/23/d116101914/htdocs/web/mls/zips/ -name '*.tar' -exec tar xvf {} \ is missing the semicolon after the backslash.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 17122533
To get yesterdays files, use "date -d yesterday +%Y%m%d"
0
 

Author Comment

by:mtnwave
ID: 17122654
Duncan,

Thanks, that seems to be working, I forgot the semicolon. Let me check the results....

Nate

0
 

Author Comment

by:mtnwave
ID: 17123253
Mike and Duncan

I gave mike a few more points as he was initially on track, Duncan came with the assist.

Nate
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question