-2

Inside a Bash script I need to save the result of executing this in a variable:

expr $(date +%s) - $(date +%s -r $videoName)

The result of executing this command should return the difference (in seconds) between the actual date and the date when the video was edited.

My first thought was something like this, but is not working:

differenceInSeconds=$(expr $(date +%s) - $(date +%s -r $videoName))

Any help?

Thanks,

user3139207
  • 121
  • 13

1 Answers1

0

I have edited above to include the source. This is very well addressed in another question. If you used this question, you should cite it in your question.


In Linux, date -r file prints the date when the file was last modified:

date +%s -r file.txt

To print the seconds elapsed since the last modification, you can use expr with the current date in seconds minus the date of the last modification:

expr $(date +%s) - $(date +%s -r file.txt)

In Mac OS X (BSD flavor of date) this won't work, you'd need to use stat as other answers pointed out.

echo $(( $(date +%s) - $(stat -f%c myfile.txt) ))

And as a function to be called with the file name:

lastmod(){
     echo "Last modified" $(( $(date +%s) - $(stat -f%c "$1") )) "seconds ago"
}

The only thing that could be messed up is either your variable $videoName or how to save it as a variable. This is how I did it on a mac with no problem (01_movie.flv is a random video file of mine):

#!/usr/bin/env bash

videoName=01_movie.flv
time=`echo $(( $(date +%s) - $(stat -f%c $videoName) ))`
echo $time

Printed to the terminal:

956680
Community
  • 1
  • 1
PhysicalChemist
  • 540
  • 4
  • 14