I need to get the time in seconds since a file was last modified. ls -l
doesn't show it.
The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.
Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.
Using getlastmod() Function: The getlastmod() function is used to get the last modification time of the current page.
Using the stat command, we can also control the output by the -c FORMAT option. There are two formats to display the mtime: %y – displays time of last data modification in a human-readable format. %Y – displays time of last data modification in number of seconds since Epoch.
Here we are going to see how to get the last modified date of the file in Linux, sometimes we may require timestamps of the file and apart from this it also ensures that we have the latest version of that file. Using Stat command. Using date command. Using ls -l command. Example 1: Using Stat command.
Unfortunately the BSD implementation of date (for example in Mac OS X) doesn't support the -r flag. To get the last modification seconds, you can use the stat command instead, as other answers suggested. Once you have that, the rest of the procedure is the same to compute the elapsed seconds.
To get the last modification seconds, you can use the stat command instead, as other answers suggested. Once you have that, the rest of the procedure is the same to compute the elapsed seconds. Show activity on this post.
-mtime 90 Means you are looking for a file modified exactly 90 days. For example, to search for txt files in the /home/james/data directory that were modified less than 90 days ago use the following command: Additionally, you can use numerical parameters as shown: For example, the following command displays text files modified in the last 12 hours
The GNU implementation of date
has an -r
option to print the last modification date of a file instead of the current date.
And we can use the format specifier %s
to get the time in seconds,
which is convenient to compute time differences.
lastModificationSeconds=$(date +%s -r file.txt)
currentSeconds=$(date +%s)
And then you can use arithmetic context to compute the difference, for example:
((elapsedSeconds = currentSeconds - lastModificationSeconds))
# or
elapsedSeconds=$((currentSeconds - lastModificationSeconds))
You could also compute and print the elapsed seconds directly without temporary variables:
echo $(($(date +%s) - $(date +%s -r file.txt)))
Unfortunately the BSD implementation of date
(for example in Mac OS X) doesn't support the -r
flag. To get the last modification seconds, you can use the stat
command instead, as other answers suggested.
Once you have that, the rest of the procedure is the same to compute the elapsed seconds.
In BASH, use this for seconds since last modified:
expr `date +%s` - `stat -c %Y /home/user/my_file`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With