Often you may want to use Bash to calculate the number of seconds that have elapsed since a file was last modified.
You can use the following syntax to do so:
echo $(($(date +%s) - $(date -r /c/users/bobbi/data2/teams.txt +%s)))
This particular example will return the number of seconds that have elapsed since the file named teams.txt was last modified.
This syntax works by first calculating the number of seconds that have elapsed since the epoch (January 1, 1970) and then subtracting the number of seconds that have elapsed since the particular file was last modified compared to the epoch.
The following example shows how to use this syntax in practice.
Example: How to Get Time Since File was Last Modified (in Seconds)
Suppose we use the ls -l statement to first view the last modification time for a file with the following path:
- c:/users/bobbi/data2/teams.txt

We can see that the file was las modified on August 22 at 11:28.
We can use the following syntax to return the number of seconds that have elapsed since the file was last modified:
find -newermt 'Aug 15 2024 00:00:00'
The following screenshot shows how to use this syntax in practice:

We can see that it has been 599 seconds since the file named teams.txt has last been modified.
Feel free to replace the path specified after date -r with the path to your own file.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
How to Compare Dates in Bash
How to Calculate Date Difference in Bash
How to Convert Timestamp to Date in Bash
