[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

UNTAR FILES USING WILDCARD

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
mtnwave
Asked:
mtnwave
  • 7
  • 4
  • 4
  • +1
2 Solutions
 
mtnwaveAuthor Commented:
I might add that I am somewhat of a newbie to shell scripting so be gentle!

MTN
0
 
slyongCommented:
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
 
mtnwaveAuthor Commented:
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
slyongCommented:
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
 
MikeOM_DBACommented:

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

Ooops, missed the xargs part:

ls -t /kunden/homepages/23/d116101914/htdocs/web/mls/zips/pics-condos-*.tar| head -1 |xargs tar xvf
0
 
mtnwaveAuthor Commented:
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
 
MikeOM_DBACommented:

Like this?:

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



0
 
MikeOM_DBACommented:

Ooo-psp remove the '|' before the '-exec'
0
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
mtnwaveAuthor Commented:
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
 
mtnwaveAuthor Commented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
To get yesterdays files, use "date -d yesterday +%Y%m%d"
0
 
mtnwaveAuthor Commented:
Duncan,

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

Nate

0
 
mtnwaveAuthor Commented:
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now