Need a script to check for a file with yesterdays date in the name

Soldato
Joined
9 May 2005
Posts
7,400
Location
Berkshire
Can anyone help me with this, I'm not quite sure how to get a script to check for a file that's named something like "file-dategoeshere.xls". The report is genereated daily, and I want a script to check if the file has run for that day and then send out an e-mail if it hasn't. The e-mail part I can do but I'm stumped on the checking of the name.

Anyone got any ideas?
 
Something like this? Should be fine to check for file existence

Code:
#!/bin/bash
DATE=`date +%e-%m-%y`
FILE=/home/jesus/Heaven/file-$DATE.xls

if [ -f $FILE ]
then
echo "file exists!"
fi
 
If the files were made with the date format: "+%Y%m%d", you would:

Code:
DAY = `date +%d`
MONTH = `date +%m`
YEAR = `date +%Y`

if [ DAY -gt 1 ]; then
  DAY = $(($DAY-1))
else
  if [ MONTH -gt 1 ]; then
    MONTH = $(($MONTH-1))
    case $MONTH in
    4 || 6 || 9 || 11)
      DAY = 30
    1 || 3 || 5 || 7 || 8 || 10 || 12)
      DAY = 31
    2)
      if [ ( ( $(($YEAR%4)) -eq 0 ) && ( $(($YEAR%100)) -eq 0 ) || ( $(($YEAR%400)) -eq 0 ) ]; then
        DAY = 29
      else
        DAY = 28
      fi
    esac
  else
    MONTH = 12
    DAY = 31
    YEAR = $(($YEAR-1))
  fi
fi
YESTERDAY = ${YEAR}${MONTH}${DAY}

Completely untested mind, and I'm sure someone is about to do it with a one liner.
 
ls file-dategoeshere.xls | while read x; do date +%s -r $x | perl -n -e 'if($_>=time()-24*60*60){print "yes"}else{print "no"} '; done

That'll print "yes" if the file has been modified in the last 24 hours and no otherwise

Lol I didn't read the question properly but clearly
ls file-*.xls | while read x; do date +%s -r $x | perl -n -e 'if($_>=time()-24*60*60){print "yes"}else{print "no"} '; done

wrapped in a grep 'yes' would work

Can I haz linux cookies plz

:D
 
Last edited:
I ended up using a variation of this:

Code:
#!/bin/bash
DATE=`date +%e-%m-%y`
FILE=/home/jesus/Heaven/file-$DATE.xls

if [ -f $FILE ]
then
echo "file exists!"
fi

With some minor changes

Code:
#!/bin/bash
cd /path/to/report/folder
DATE=`date [U]--date=yesterday[/U] +%e-%m-%Y`
FILE=[U]*[/U]$DATE.xls
if [ -f $FILE ]
then
echo "file exists!"
[U]else
echo "file does not exist!"
cd /path/to/mail/script
php -q mailer.php[/U]
fi

The report names contains a load of spaces which was buggering up line 5, so it was easier to *$DATE.xls.
 
Back
Top Bottom