Jump to content

Unzip tar.Z file and check fianlename date by Shell Script


PriteshP23
Go to solution Solved by PriteshP23,

Recommended Posts

Hello,

I have one "tar.Z" file. Objective is to unzip it by shell script and check the date.

    File archive:  filename_20140103_1540.tar.Z

 #!/bin/bash
cd $REP_DATAS/path
u=$(date +%u)
if [ ${u} -eq 1 ] ; then        
dateQ=`date --date='-3 day' +'%Y%m%d_%H%m'`
else
dateQ=`date --date='-1 day' +'%Y%m%d_%H%m'`
fi
tar xvf filename_"dateQ".tar.Z

   

error:

    tar: Error is not recoverable: exiting now


THANKS IN ADVANCED.

Link to comment
Share on other sites

Its really not that clear what your trying to do.

 

I got file from Monday to Friday. I need to verify that it should not be delay of more than 3 days.

My file name is difficult to handle in shell script and check the date. Files are like this --> my_file_20140103_1540.tar.Z (For Friday) /  my_file_20140106_1460.tar.Z (For Monday) / my_file_20140107_1120.tar.Z (For Tuesday)

For example, if its monday than it must be the date of Friday (maximum late), not the file with date of thursday. 

In addition, if its Wednesday, i need to check if it is date of Tuesday --> OK, it its date of Monday --> NOK

I hope i am clear.

 

 

echo ilename_"dateQ".tar.Z

 

not working :(

 

We need to correct the dateQ logic. It should be modified in order to print the value of filename. I think here is file of 3rd January 2014 and it need to adjust with current time. May be that's why it is not giving output.

 

Why "Z" at the end?

 

I have already written my filename in the question. Anyway, i repeat the file is in tar.Z format " my_file_20140103_1540.tar.Z " .

Link to comment
Share on other sites

Aha, it's my typo: echo ilename_"dateQ".tar.Z

 

So, use the unix find command to find all files in this particular directory older than 3 days and unzip them.

 

Something like that jumped at the top of my head.

#!/bin/bash

DIR=$(dirname $0)

find ${DIR} -name '*.tar.Z' -type f -mtime +3 | xargs tar -zxvf

if [ $? -eq 0 ];then

echo "Success";

else

echo "Not files found"

exit 0

fi



Note: Make sure that tar -zxvf works for you or try gzip -d

 

 

 

That is commonly used with ncompress packages.

 

Are they provide a good compresson?

Link to comment
Share on other sites

To unzip, zcat is working fine:

zcat $file | tar xvf -

i need to correct the part of date in the script.

dateQ='20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])' 
if [ ${u} -eq 1 ] ; then 
dateQ=`date --date='-3 day' +'%Y%m%d'` 
else 
dateQ=`date --date='-1 day' +'%Y%m%d'`
fi

I believe there are more options possible.  

 

In fact, my goal is to check the date of file.
 
There are 7 files inside the .tar.Z file. With that 7 files i need to do update operation in my database.
 
Normally, everyday (Monday to Friday) i will receive new file with respective date. I need to check the date and display the date of file.
For example, if one day i have not received file in that case, i need to use previous file.
 
Simple example:
Day 1: 09 Jan 2014
I have received the .tar.Z file --> extract --> update operation --> Mesg
"File has received 09 Jan 2014"
Day 2: 10 Jan 2014
I have received the .tar.Z file --> extract --> update operation --> Mesg
"File has received 10 Jan 2014"
Note: DELETE FILE OF 9th Jan
Day 3: 13 Jan 2014
I have NO Files.
 
So, i have to use yesterdays file and repeat the same procedure with ALERT MESG.
 
"File has received 10 Jan 2014" (in RED)
There should be one file ALWAYS available to do update operation, no matter which date.
 
I hope i am clear.
Link to comment
Share on other sites

But ... the time of the file exists and being created via stdin when the file has been created on the file server. You don't have to do this!

 

To find all files created/modified exactly 3 days (72 hours) ago by NOW, you would use:

 

find ${DIR} -name '*.tar.Z' -type f -mtime 3

 

More than 3 days:

 

find ${DIR} -name '*.tar.Z' -type f -mtime +3

 

Less than 3 days

find ${DIR} -name '*.tar.Z' -type f -mtime -3

 

PS: I will provide few examples if you don't understand something.

Edited by jazzman1
Link to comment
Share on other sites

Is it possible to write like this ?

  u=$(date +%u)
        if [ ${u} -eq 1 ] ; then        
        dateQ=`date --date='-3 day' +'%Y%m%d'`
        else
        dateQ=`date --date='-1 day' +'%Y%m%d'`
        fi
       
        zcat my_file_"$dateQ"*.tar.Z | tar xvf -

If possible, i would like to have link which provides shell script example with output.

Thanks in advanced.

      

Link to comment
Share on other sites

Check the possibilities first.

 

Try this:

#!/bin/bash

DIR=$(dirname $0)

YESTARDAY=`/bin/date +%Y%m%d -d "yesterday"`

DAY3AGO=`/bin/date +%Y%m%d -d "3 day ago"`

DAY=`/bin/date +%u`

case ${DAY} in

1) dateQ=${DAY3AGO}
;;

6|7) dateQ='WEEKEND'
;;

*) dateQ=${YESTARDAY}

esac

if [ ${dateQ} -ne 'WEEKEND' ]; then

zcat ${DIR}/my_file_${dateQ}*.tar.Z | tar xvf -

else

echo "It's a weekend time"

exit 0

fi

PS: The code of your bash script was very hard for everyone to understand what do you want to accomplish, so take mine and ask me if you don't understand  something.

 

I think it's clear enough, but.... :)

Edited by jazzman1
Link to comment
Share on other sites

Hey @PriteshP23,

 

in my example I'm checking the current date with DAY=`/bin/date +%u` and returns the current day from 1-7 (Monday-Sunday).

If the current day is 1 (Monday) then I'm checking the string of the file which contains the date 3 days ago in a format (20140103) for instance - my_file_20140103_1540.tar.Z then unzip the file. If the current day is 6 or 7 (Saturday or Sunday) it doesn't do anything with the file of Friday.

However, for others days (Tuesday - Friday) it makes changes to the file with a date of yesterday.

Or... am I missing something in my script? Did you test it?

Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.