Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get time since file was last modified in seconds with bash?

Tags:

linux

bash

unix

I need to get the time in seconds since a file was last modified. ls -l doesn't show it.

like image 460
mboronin Avatar asked Oct 19 '13 06:10

mboronin


People also ask

How do you check the last time a file was modified in Linux?

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.

How can you tell when the last time was modified?

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.

Which function is used to get the last modification time?

Using getlastmod() Function: The getlastmod() function is used to get the last modification time of the current page.

How do I print the last modified time of a directory?

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.

How to get the last modified date of the file in Linux?

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.

Why can't I get the last modified seconds of a date?

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.

How do I get the last modification 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.

How to search for files modified exactly 90 days ago?

-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


2 Answers

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.

like image 161
janos Avatar answered Oct 17 '22 20:10

janos


In BASH, use this for seconds since last modified:

 expr `date +%s` - `stat -c %Y /home/user/my_file`
like image 11
Jason Enochs Avatar answered Oct 17 '22 21:10

Jason Enochs