If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Compressing files older than 30 days, but timestamp is part of filename

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-04, 16:46
Mercurial Mercurial is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
Compressing files older than 30 days, but timestamp is part of filename

I need to write a script that will compress files inside several directories that are older than 30 days.

The problem is the time stamp is wrong on the directories and the files and the correct date is actually in the name of the directory/file e.g. file070504, so the script will have to extract this, convert this to date format, add 30 days to it and if it was to equal todays date then it would execute a compress command on it.

Can anyone help? quite lost
Reply With Quote
  #2 (permalink)  
Old 06-08-04, 17:32
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Lightbulb

Try something like this:
Code:
#!/bin/ksh

cMM=$(date +%m)
cDD=$(date +%d)
cYY=$(date +%y)
echo "Today is: $cMM/$cDD/$cYY"

for f in $(ls -1 file*)
do
  dt=$(echo $f|cut -c5-)
  m=$(echo $dt|cut -c1-2)
  d=$(echo $dt|cut -c3-4)
  y=$(echo $dt|cut -c5-6)
  #echo "File date is: ${m}/${d}/${y}"
  cmpss="N"
  if [ y -eq cYY ]
  then
    if [ m -lt cMM ]
    then
      cmpss="Y"
    else
      if [ m -eq cMM ]
      then
        (( d0 = cDD - d ))
        if [ d0 -gt 29 ]
        then
          cmpss="Y"
        fi
      fi
    fi
  else
    if [ y -lt cYY ]
    then
      if [ m -lt 12 ]
      then
        cmpss="Y"
      else
        (( d0 = 31 - d + cDD ))
        if [ d0 -gt 29 ]
        then
          cmpss="Y"
        fi
      fi
    fi
  fi
  if [[ "$cmpss" == "Y" ]]
  then
    compress $f
    echo "File $f compressed."
  fi
done
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 06-08-04, 17:42
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
PS: Replace the following line of code from:

for f in $(ls -1 file*)

to:

for f in $(ls -1 file*|grep -v '.Z')
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 06-08-04, 22:34
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Exclamation Corrections to script

The script is still NOT perfect, but this version is a bit more accurate:
Code:
#!/bin/ksh

cMM=$(date +%m)
cDD=$(date +%d)
cYY=$(date +%y)
echo "Today is: $cMM/$cDD/$cYY"

for f in $(ls -1 file*|grep -v '.Z')
do
  dt=$(echo $f|cut -c5-)           # Location of the date in file name
  m=$(echo $dt|cut -c1-2)        # Month
  d=$(echo $dt|cut -c3-4)        # Day
  y=$(echo $dt|cut -c5-6)        # Year
  #echo "File date is: ${m}/${d}/${y}"
  cmpss="N"
  if [ y -eq cYY ]
  then
    (( diff = cMM - m ))
    if [ diff -gt 1 ]
    then
      cmpss="Y"
    else
      if [ diff -eq 1 ]
      then
        (( d0 = 31 - cDD + d ))
        if [ d0 -ge 30 ]
        then
          cmpss="Y"
        fi
      else
        if [ d -gt 29 ]
        then
          cmpss="Y"
        fi
      fi
    fi
  else
    if [ y -lt cYY ]
    then
      if [ m -lt 12 ]
      then
        cmpss="Y"
      else
        (( d0 = 31 - d + cDD ))
        if [ d0 -ge 30 ]
        then
          cmpss="Y"
        fi
      fi
    fi
  fi
  if [[ "$cmpss" == "Y" ]]
  then
    compress $f
    echo "File $f compressed."
  fi
done
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #5 (permalink)  
Old 06-10-04, 04:40
Mercurial Mercurial is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
Thanks for the help, will work from there
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On