Solved

UNTAR FILES USING WILDCARD

Posted on 2006-07-13
17
886 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now