PriteshP23 Posted January 8, 2014 Share Posted January 8, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/ Share on other sites More sharing options...
trq Posted January 9, 2014 Share Posted January 9, 2014 Its really not that clear what your trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464522 Share on other sites More sharing options...
jazzman1 Posted January 9, 2014 Share Posted January 9, 2014 Can I see the whole output of: echo ilename_"dateQ".tar.Z Why "Z" at the end? Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464532 Share on other sites More sharing options...
trq Posted January 9, 2014 Share Posted January 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464534 Share on other sites More sharing options...
PriteshP23 Posted January 9, 2014 Author Share Posted January 9, 2014 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 " . Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464555 Share on other sites More sharing options...
jazzman1 Posted January 9, 2014 Share Posted January 9, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464624 Share on other sites More sharing options...
PriteshP23 Posted January 9, 2014 Author Share Posted January 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464646 Share on other sites More sharing options...
jazzman1 Posted January 10, 2014 Share Posted January 10, 2014 (edited) 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 January 10, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1464650 Share on other sites More sharing options...
PriteshP23 Posted January 13, 2014 Author Share Posted January 13, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1465049 Share on other sites More sharing options...
jazzman1 Posted January 13, 2014 Share Posted January 13, 2014 (edited) 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 January 13, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1465076 Share on other sites More sharing options...
Solution PriteshP23 Posted January 14, 2014 Author Solution Share Posted January 14, 2014 Finally, it works 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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1465179 Share on other sites More sharing options...
jazzman1 Posted January 14, 2014 Share Posted January 14, 2014 (edited) 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 January 14, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1465203 Share on other sites More sharing options...
PriteshP23 Posted January 15, 2014 Author Share Posted January 15, 2014 @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. Quote Link to comment https://forums.phpfreaks.com/topic/285207-unzip-tarz-file-and-check-fianlename-date-by-shell-script/#findComment-1465335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.