Jump to content


Photo

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

tar.z shell linux unix date

Best Answer PriteshP23, 14 January 2014 - 08:48 AM

Finally, it works :happy-04:

cd $file/path

        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 -

Thank you all for your time and input. :thumb-up:

Go to the full post


  • Please log in to reply
12 replies to this topic

#1 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 08 January 2014 - 09:44 AM

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.


" I will Win not immediately, but Definitely. "

 


#2 trq

trq

    Advanced Member

  • Administrators
  • 30,996 posts
  • LocationSydney, Australia.

Posted 08 January 2014 - 11:39 PM

Its really not that clear what your trying to do.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,631 posts
  • LocationMississauga, Canada

Posted 09 January 2014 - 12:55 AM

Can I see the whole output of:

 

echo ilename_"dateQ".tar.Z

 

Why "Z" at the end?



#4 trq

trq

    Advanced Member

  • Administrators
  • 30,996 posts
  • LocationSydney, Australia.

Posted 09 January 2014 - 01:08 AM

Can I see the whole output of:
 
echo ilename_"dateQ".tar.Z


ilename_dateQ.tar.Z

Why "Z" at the end?


That is commonly used with ncompress packages.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 09 January 2014 - 05:40 AM

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 " .


" I will Win not immediately, but Definitely. "

 


#6 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,631 posts
  • LocationMississauga, Canada

Posted 09 January 2014 - 01:46 PM

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?



#7 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 09 January 2014 - 06:00 PM

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.

" I will Win not immediately, but Definitely. "

 


#8 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,631 posts
  • LocationMississauga, Canada

Posted 09 January 2014 - 07:05 PM

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, 09 January 2014 - 07:08 PM.


#9 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 13 January 2014 - 07:18 AM

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.

      


" I will Win not immediately, but Definitely. "

 


#10 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,631 posts
  • LocationMississauga, Canada

Posted 13 January 2014 - 12:53 PM

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, 13 January 2014 - 01:04 PM.


#11 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 14 January 2014 - 08:48 AM   Best Answer

Finally, it works :happy-04:

cd $file/path

        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 -

Thank you all for your time and input. :thumb-up:


" I will Win not immediately, but Definitely. "

 


#12 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,631 posts
  • LocationMississauga, Canada

Posted 14 January 2014 - 11:05 AM

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, 14 January 2014 - 11:12 AM.


#13 PriteshP23

PriteshP23

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts
  • Age:24

Posted 15 January 2014 - 08:37 AM

@jazzman1

 

Your logic is right. I agree. But in my file it is was not working and difficult to mange (in terms of variables, whitespace problems).

 

The earlier script worked, so i keep that in my file.

 

I am very thankful to you for your help and time.


" I will Win not immediately, but Definitely. "

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com