Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Posts
    2

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

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713
    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

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  5. #5
    Join Date
    Jun 2004
    Posts
    2
    Thanks for the help, will work from there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •