How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory.

However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that.

I used stat command to check for file information:

function checkStat

{

    # make sure stat command is installed

    which stat > /dev/null 2>&1

    if [ $? -eq 1 ]; then
        echo "stat command not found!"
        exit 2
    fi

}



function processFile

{

    if [ -s ${1} ]; then
        VAR="$(stat -c %y ${1})"
        VAR2="${VAR:0:10}"

        FILE_TIME=$(ls -l $1| cut -d" " -f6,7,8,9)
        FILE_SIZE=$(du -sh $1 | cut -f1)
        if [ $NOW == $VAR2 ]; then
            echo "Current File Found: ${1} DATED $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${CURRENT_FILE}
        else
            erroremail=1
            echo "Outdated File Found: ${1} DATED $FILE_TIME - SIZE $FILE_SIZE" | tee -a ${OLD_FILE}
        fi

    else
        erroremail=1
        echo "File Not Found OR 0KB in SIZE: ${1}" | tee -a ${FILE_NOT_FOUND}
    fi



}



#############################

# Main

#############################



# Verify the stat command exists

checkStat



# Create log file

touch  ${LOG_FILE}



# Process the files

for d in ${DIR[@]}; do
    for f in ${FILE[@]}; do
        processFile "${d}/${f}"
    done
done

touch filetimes to two dummy files, use find to gety a list of your files
example:

days_before()
{
   perl -e ' ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - (86400 * $ARGV[0]));
           printf "%d%02d%02d%s\n", $year+1900, $mon+1, $mday, "2359.59"; '  $1
}

touch -t $(days_before 1) dummy
touch -t $(days_before 2) ol_dummy
find . \( ! -newer dummy -a -newer ol_dummy \) -exec ls -l {} \;
1 Like

Thanks. I am trying to find a way to accomplish this in unix shell script.

Unless you have GNU date there is no simple code to do this. CFA Johnson, a forum member, has a website with a shell script library to do date arithmetic. The code is good, but doing dates in shell in an involved.

Rolling your own date routines is not best practice. Finding yesterday when the date today is March 1 is a classic example. You have to know whether it is a leap year or not, then roll back to Feb 28 or 29.

The standard way to get yesterday:

calculate the current Julian day, subtract one, then convert Julian -> Gregorian date.

get current time in UNIX epoch seconds and subtract 86400, then convert epoch seconds to a Gregorian date.

For production systems always use known good libraries for date calculations.

eerily similar thread and solution...restricted to the shell as well: http://www.unix.com/shell-programming-scripting/136257-date-within-timeframe-2-days-ago-2.html

1 Like

Just a note - when you get working respsonses it makes folks here feel like it is homework, when it cannot be used. We have a special forum for homework.

awk, perl, python, grep are all external to shell and require special knowledge of syntax separate from shell. awk is everywhere, perl is close to everywhere; python and ruby are somewhat less ubiquitous.

Straight shell may be possible but for a lot of things; it will require a special tested shell library to run. Generally experienced programmers resort to simpler methods: another environment like awk.

If you have to have shell - get GNU date. That allows date arithmetic operations. Although using date is not straight shell either.

I don't know where folks draw the line between shell and other things, but in reality about 80% of the commonly used "shell" commands are not shell builtins, they are separate code - find, head, tail...etc. try the which command or the type builtin to see what a command really is.

bash will let you create custom builtins, you have to code it in C however.

Thank you guys.