Shell script to get the time difference

by on May 2, 2006 · 31 comments· LAST UPDATED September 19, 2007

in , ,

Q. I am able to write PHP or Perl script where I can find out time difference between script executions. Now I have .shtml file that is nothing but a shell script outputting some data to browser. What I want is time difference or time it took to execute a script. How do I write a shell script?

A. Your Logic should be as follows:

* Get start time and store to a variable START

* Execute a shell script

* Grab output and send to web browser

* Get time again and store to a variable END

* Calculate difference using expression END - START

Shell script o get the time difference

Here is small script that does the same thing (please note that script teated on GNU/Linux and with GNU date command only):
$ vi timediff.bash
Append text as follows:

#!/bin/bash
START=$(date +%s)
# do something
# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here
END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

Save and execute the script as follows:
$ chmod +x timediff.bash
Execute the script:
$ ./timediff.bash
Output:

It took 4 seconds
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 31 comments… read them below or add one }

1 Anonymous May 8, 2006 at 10:46 pm

What ifmy script takes mili seconds to execute and I want to be able to measure the time difference even if it is few micro seconds or mili seconds.

Thanks,

Ananda

Reply

2 Anonymous August 22, 2006 at 7:35 pm

instead of
$(date +%s)
use
$(date +%s%N)

and you will get nanoseconds precision


Mitsuhashi Da! Oboeteoke!

Reply

3 satish asnani March 25, 2007 at 8:22 am

i need to find the no.of days between system date and the date of file creation. how do i achieve this?

Reply

4 Amit Pankaj January 23, 2013 at 10:52 am

echo $((( $END – $START ))/86400)

will give you output in days

Reply

5 Amit Pankaj January 23, 2013 at 10:54 am

small correction

echo $((( $END – $START )/86400))

will give you output in days

Reply

6 pavana August 16, 2007 at 9:11 am

i need to find the no.of days and time between system date and the date of file creation. how do i achieve this?

Reply

7 Arturs Bebris February 6, 2013 at 6:24 pm

This will remove all files modified 30 or more days ago.

for file in “$( /usr/bin/find /path/to/your/files -type f -mtime +30 )”
do
/bin/rm -v -f $file
done

Reply

8 Raj September 12, 2007 at 12:14 pm

Hi, you time diff script doesn’t work. date +%s returns only %s as value. And thus it does not work forward.

Reply

9 Ratna September 14, 2007 at 12:45 pm

The above mentioned script will not work because “%s” is given in small letter.
Replace “%s” with “%S” and the script will work.

Reply

10 seema October 23, 2007 at 8:59 am

can i have the similar script in k-shell

Reply

11 Johannes May 8, 2008 at 1:24 pm

Cool script, thanks!

Reply

12 ESWAR September 29, 2008 at 5:33 am

write a shell script pow(a,b)
eg a=2
b=3
how to to write this program.

Reply

13 Nathan December 16, 2008 at 1:59 am

Wow! its really cool :)

Reply

14 Achala February 21, 2009 at 6:05 pm

How do i find time difference between two file .
Example: One file created at Wed Feb 18 10:08:34 IST 2009.And Anothe file created at
Wed Feb 18 23:32:36 IST 2009. How do i find difference between Wed Feb 18 23:32:36 IST 2009 – Wed Feb 18 10:08:34 IST 2009 ?
Please Help

Reply

15 shiva April 30, 2009 at 1:34 pm

nice script…. good
thaqs

Reply

16 Yogesh June 2, 2009 at 7:07 am

Good one !!!

Reply

17 Jax April 11, 2010 at 1:23 am

Thanks for the script mate. It was of great help to me.

Reply

18 Mary Monahan October 12, 2010 at 1:23 pm

Just a comment:
%s does not work on Solaris. %S does, but it only returns the seconds part of the date-time, not the number of seconds since 00:00:00, Jan 1, 1970 so you would have to deal with the math of changes in minutes, hours and the turn over from 59 to 0.

Reply

19 jules March 5, 2011 at 10:27 pm

@Achala
difference between 2 files in seconds:
use date -r for the modification date of the file and +%s for date in seconds:
Example 2 files bobo.png and foobar.ppm
echo `date -r bobo.png +%s`-`date -r foobar.ppm +%s` | bc
8411376
echo “8411376/(24*3600)”|bc -l
97.35388888888888888888
So 97 days between these files.

Reply

20 Vinod September 27, 2011 at 4:13 pm

It was of great help to me…………….Thanx.

Reply

21 xofer April 5, 2011 at 9:57 pm

Stumbled upon this example. The script in article is all very fine as a script exercise, but it does not follow bash logic. If you want to get the running time of – lets say – a_long_process
you just execute:
time a_long_process
simple, isn’t it? For more options check: man time

Reply

22 solo October 10, 2011 at 12:21 pm

Is it possible to do this with the use of the time() command?

Reply

23 Aravindhan Devarajan August 22, 2012 at 5:54 pm

I want to get system time alone. How do I get it from shell script? Anyone please suggest scripts

Reply

24 shivendra August 23, 2012 at 3:50 am

Hey
Instead of using second can we use DD:MM:YYYY format
and then calculate deference.

Reply

25 Bhaskar March 21, 2013 at 1:35 pm

write a script, ” How much time it will take to boot up the device”?, can anyone help me?

Reply

26 Bhaskar March 21, 2013 at 1:55 pm

Which is in shell or perl script or python?

Reply

27 betagama September 10, 2013 at 6:04 pm

echo ” Time taken to process $SECONDS”

Reply

28 betagama September 10, 2013 at 6:07 pm

just put the above one line at the end of the script and you will get the time till that statement..

Reply

29 betagama September 10, 2013 at 6:09 pm

by the way that will be in shell… as most of your answers and since i googled for bash and this came so i thought might be usefull for people who come via google to this page by mistake ;)

Reply

30 Marco December 18, 2013 at 11:54 am

If you want to have the time difference based on entries in a logfile, you first have to convert the found dates to seconds.

>  date -s 'Dec 17 14:38:01'
Tue Dec 17 14:38:01 CET 2013
>  date -s 'Dec 17 14:38:01' +%s
1387287481

Reply

31 Kushal Gangan February 21, 2014 at 6:02 am

Thanks, it was helpful :)

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: