Shell script to get the time difference

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?

ADVERTISEMENTS

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
33 comments… add one
  • Anonymous May 8, 2006 @ 22:46

    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

  • Anonymous Aug 22, 2006 @ 19:35

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

    and you will get nanoseconds precision


    Mitsuhashi Da! Oboeteoke!

  • satish asnani Mar 25, 2007 @ 8:22

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

    • Amit Pankaj Jan 23, 2013 @ 10:52

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

      will give you output in days

      • Amit Pankaj Jan 23, 2013 @ 10:54

        small correction

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

        will give you output in days

  • pavana Aug 16, 2007 @ 9:11

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

    • Arturs Bebris Feb 6, 2013 @ 18:24

      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

  • Raj Sep 12, 2007 @ 12:14

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

  • Ratna Sep 14, 2007 @ 12:45

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

  • seema Oct 23, 2007 @ 8:59

    can i have the similar script in k-shell

  • Johannes May 8, 2008 @ 13:24

    Cool script, thanks!

  • ESWAR Sep 29, 2008 @ 5:33

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

  • Nathan Dec 16, 2008 @ 1:59

    Wow! its really cool :)

  • Achala Feb 21, 2009 @ 18:05

    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

  • shiva Apr 30, 2009 @ 13:34

    nice script…. good
    thaqs

  • Yogesh Jun 2, 2009 @ 7:07

    Good one !!!

  • Jax Apr 11, 2010 @ 1:23

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

  • Mary Monahan Oct 12, 2010 @ 13:23

    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.

  • jules Mar 5, 2011 @ 22:27

    @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.

    • Vinod Sep 27, 2011 @ 16:13

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

  • xofer Apr 5, 2011 @ 21:57

    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

  • solo Oct 10, 2011 @ 12:21

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

  • Aravindhan Devarajan Aug 22, 2012 @ 17:54

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

  • shivendra Aug 23, 2012 @ 3:50

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

  • Bhaskar Mar 21, 2013 @ 13:35

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

  • Bhaskar Mar 21, 2013 @ 13:55

    Which is in shell or perl script or python?

  • betagama Sep 10, 2013 @ 18:04

    echo ” Time taken to process $SECONDS”

  • betagama Sep 10, 2013 @ 18:07

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

  • betagama Sep 10, 2013 @ 18:09

    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 ;)

  • Marco Dec 18, 2013 @ 11:54

    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
  • Kushal Gangan Feb 21, 2014 @ 6:02

    Thanks, it was helpful :)

  • Samuel Oct 9, 2015 @ 18:48

    Do you know about the ‘time’ command.

    Simply put ‘time’ before whatever script you want to run, and afterwards you will get stats how much time it took.

    For exemple:

    time sleep 5

    real 0m5.001s
    user 0m0.000s
    sys 0m0.000s

  • Amit Yadav Jun 20, 2016 @ 10:18

    Hello,

    In my ksh file the below two lines of code written for start/end time of some process.
    echo launch TIMESTAMP START: $(date +”%Y.%m.%d %H:%M:%S”)
    echo launch TIMESTAMP END: $(date +”%Y.%m.%d %H:%M:%S”)

    which showing ouput as:
    showing in log as : launch TIMESTAMP START: 2016.05.24 06:30:35
    showing in log as : launch TIMESTAMP END: 2016.05.24 14:55:05

    Now please let us know how i take total time taken for this process. just to inform you that (END-START will not help in this and showing incorrect output while considering all difficult combinations)

    Please help!
    Thanks in Advance.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.