date/time of a file compare with current date/time

date/time of a file compare with current date/time

Post by mvgor.. » Fri, 10 Mar 2000 04:00:00



Hi,

I hope i can drop my question about Linux shell programming here?

I'm writing a script in bash to find files. I'm using the slocate
programm, which is being runned every hour using a cron job, to create
it's database.

Now i want to know how long it is ago that the database has been
recreated. Zo i need to check the date and time from that file and
compare it with the current date and time. I know there must be easy to
do but i can't find the trick to do it.

Maikel van Gorkom

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

date/time of a file compare with current date/time

Post by Ken Pizzi » Fri, 10 Mar 2000 04:00:00



>I hope i can drop my question about Linux shell programming here?

As long as it's shell programming on unixish systems, it's on-topic.

Quote:>Now i want to know how long it is ago that the database has been
>recreated. Zo i need to check the date and time from that file and
>compare it with the current date and time. I know there must be easy to
>do but i can't find the trick to do it.

Since you're running on Linux the answer is simplified: you'll
be running with GNU date installed and so you can:
  now=$(date +%s)
  then=$(date -r "$file" +%s)
  diff=$((now - then))
  echo "$file was last modified $diff seconds ago"
Or, if you're a one-liner fan:
  diff=$(( $(date +%s) - $(date -r "$file" +%s) ))

                --Ken Pizzini

 
 
 

date/time of a file compare with current date/time

Post by Daniel Frazie » Fri, 10 Mar 2000 04:00:00


To expand on that, this will produce a time in hours, minutes and
seconds.  

now=$(date +%s)
then=$(date -r "$file" +%s)
diff=$((now - then))
echo -n "$file was last modified "
HRS=`expr $diff / 3600`
MIN=`expr $diff % 3600 / 60`
SEC=`expr $diff % 3600 % 60`
if [ $HRS -gt 0 ]
then
        echo -n "$HRS hrs. "
fi
if [ $MIN -gt 0 ]
then
        echo -n "$MIN mins. "
fi
if [ $SEC -gt 0 ]
then
        if [ $MIN -gt 0 ]
        then
                echo "and $SEC secs. ago."
        elif [ $HRS -gt 0 ]  
        then
                echo "and $SEC secs. ago."
        else
                echo "$SEC secs. ago."
        fi  
fi

Hope this helps.
Daniel Frazier



> >I hope i can drop my question about Linux shell programming here?

> As long as it's shell programming on unixish systems, it's on-topic.

> >Now i want to know how long it is ago that the database has been
> >recreated. Zo i need to check the date and time from that file and
> >compare it with the current date and time. I know there must be easy to
> >do but i can't find the trick to do it.

> Since you're running on Linux the answer is simplified: you'll
> be running with GNU date installed and so you can:
>   now=$(date +%s)
>   then=$(date -r "$file" +%s)
>   diff=$((now - then))
>   echo "$file was last modified $diff seconds ago"
> Or, if you're a one-liner fan:
>   diff=$(( $(date +%s) - $(date -r "$file" +%s) ))

>                 --Ken Pizzini

 
 
 

date/time of a file compare with current date/time

Post by Ken Pizzi » Fri, 10 Mar 2000 04:00:00



>To expand on that, this will produce a time in hours, minutes and
>seconds.  

>now=$(date +%s)
>then=$(date -r "$file" +%s)
>diff=$((now - then))
>echo -n "$file was last modified "
>HRS=`expr $diff / 3600`
>MIN=`expr $diff % 3600 / 60`
>SEC=`expr $diff % 3600 % 60`

Better than calling the external "expr" command would be to use
the shell's builtin arithmetic capabilities:
 HRS=$((diff / 3600))
 MIN=$((diff % 3600 / 60))
 SEC=$((diff % 60))

Quote:>if [ $HRS -gt 0 ]
>then
>    echo -n "$HRS hrs. "
>fi
>if [ $MIN -gt 0 ]
>then
>    echo -n "$MIN mins. "
>fi
>if [ $SEC -gt 0 ]
>then
>    if [ $MIN -gt 0 ]
>    then
>            echo "and $SEC secs. ago."
>    elif [ $HRS -gt 0 ]  
>    then
>            echo "and $SEC secs. ago."
>    else
>            echo "$SEC secs. ago."
>    fi  
>fi

Seems a bit verbose; how about:
  ((HRS > 0)) && echo -n "$HRS hrs. "
  ((MIN > 0)) && echo -n "$MIN mins. "
  ((HRS > 0 || MIN > 0)) && echo -n "and "
  echo "$SEC secs. ago."

                --Ken Pizzini

 
 
 

date/time of a file compare with current date/time

Post by Daniel Frazie » Fri, 10 Mar 2000 04:00:00


This was actually a part of a script that was the first script that I
wrote that was longer than a few lines, so I'm sure that there is plenty
that could be cleaned up/optimized.  Thanks for the input, I always
appreciate constructive comments.

Daniel Frazier



> >To expand on that, this will produce a time in hours, minutes and
> >seconds.

> >now=$(date +%s)
> >then=$(date -r "$file" +%s)
> >diff=$((now - then))
> >echo -n "$file was last modified "
> >HRS=`expr $diff / 3600`
> >MIN=`expr $diff % 3600 / 60`
> >SEC=`expr $diff % 3600 % 60`

> Better than calling the external "expr" command would be to use
> the shell's builtin arithmetic capabilities:
>  HRS=$((diff / 3600))
>  MIN=$((diff % 3600 / 60))
>  SEC=$((diff % 60))

> >if [ $HRS -gt 0 ]
> >then
> >       echo -n "$HRS hrs. "
> >fi
> >if [ $MIN -gt 0 ]
> >then
> >       echo -n "$MIN mins. "
> >fi
> >if [ $SEC -gt 0 ]
> >then
> >       if [ $MIN -gt 0 ]
> >       then
> >               echo "and $SEC secs. ago."
> >       elif [ $HRS -gt 0 ]
> >       then
> >               echo "and $SEC secs. ago."
> >       else
> >               echo "$SEC secs. ago."
> >       fi
> >fi

> Seems a bit verbose; how about:
>   ((HRS > 0)) && echo -n "$HRS hrs. "
>   ((MIN > 0)) && echo -n "$MIN mins. "
>   ((HRS > 0 || MIN > 0)) && echo -n "and "
>   echo "$SEC secs. ago."

>                 --Ken Pizzini

 
 
 

date/time of a file compare with current date/time

Post by Michael Wa » Sat, 11 Mar 2000 04:00:00



>  now=$(date +%s)
>  then=$(date -r "$file" +%s)
>  diff=$((now - then))

((diff = now - then)) is better.
--
Michael Wang

http://www.mindspring.com/~mwang    
 
 
 

1. How to Compare File Date to Current Date

How can I compare the date of a file to the current date? Actually, I want
to do something if a particular file is more than an hour old.

A friend gave me a long and ugly script but I was hoping for something
simpler.

Any ideas?

Thanks in advance.

Gail

--

Parallel Products                 | phone: (510) 922-0194


2. rewrite Rules Question

3. run time date and syslog date/time conflict

4. Help with "mv" wildcard command

5. Archiving files in "date folders" based on date and time file generated

6. pthread_create and popen

7. System date and file dates not showing in same time zone

8. ATI Graphics Xpression with linux/XFree86?

9. compare files date time...

10. File modification date/time compare utility

11. how to compare date of file to a fixed date?

12. dates (comparing times)

13. current date & time from Fortran