How to Get Time Since File Was Last Modified in Seconds with Bash

Best Way to Get File Modified Time in Seconds

Since it seems like there might not be a "correct" solution I figured I'd post my current one for comparison:

if stat -c %Y . >/dev/null 2>&1; then
get_modified_time() { stat -c %Y "$1" 2>/dev/null; }
elif stat -f %m . >/dev/null 2>&1; then
get_modified_time() { stat -f %m "$1" 2>/dev/null; }
elif date -r . +%s >/dev/null 2>&1; then
get_modified_time() { date -r "$1" +%s 2>/dev/null; }
else
echo 'get_modified_time() is unsupported' >&2
get_modified_time() { printf '%s' 0; }
fi

[edit]
I'm updating this to reflect the more up to date version of the code I use, basically it tests the two main stat methods and a somewhat common date method in any attempt to get the modified time for the current working directory, and if one of the methods succeeds it creates a function encapsulating it for use later in the script.

This method differs from the previous one I posted since it always does some processing, even if get_modified_time is never called, but it's more efficiently overall if you do need to call it most of the time. It also lets you catch an unsupported platform earlier on.

If you prefer the function that only tests functions when it is called, then here's the other form:

get_modified_time() {
modified_time=$(stat -c %Y "$1" 2> /dev/null)
if [ "$?" -ne 0 ]; then
modified_time=$(stat -f %m "$1" 2> /dev/null)
if [ "$?" -ne 0 ]; then
modified_time=$(date -r "$1" +%s 2> /dev/null)
[ "$?" -ne 0 ] && modified_time=0
fi
fi
echo "$modified_time"
}

Check last modified date is within n seconds

Assuming GNU find (a fair assumption, given other GNU tools used in the question):

if [[ $(find . -maxdepth 1 -type f -newermt '-200 seconds' -print -quit) ]]; then
echo "The newest file is less than 200 seconds old"
else
echo "The newest file is more than 200 seconds old"
fi

On a system with BSD tools (such as MacOS), this might instead be:

if [[ $(find . -maxdepth 1 -type f -mtime -200s -print -quit) ]]; then
echo "The newest file is less than 200 seconds old"
else
echo "The newest file is more than 200 seconds old"
fi

Either of the above (as appropriate for the current platform) will have find scan the directory until they find a single file less than 200 seconds old; and will stop at that point and print the name of that file. This makes the search considerably more efficient for large directories than having ls sort the entire list (or to continue to scan for more files after one has already been found).

Note also the use of [[ ]], which suppresses string-splitting -- with [ ], quotes around $( ) would be needed to ensure correct behavior with filenames having spaces in their names, or files whose names could be expanded as glob expressions.

How to get the last time a file was modified in Unix

Though people might tell you, you should not parse the output of ls, simply that can easily break if your file name contains tabs, spaces, line breaks, your user decides to simply specify a different set of ls options, the ls version you find is not behaving like you expected...

Use stat instead:

stat -c '%Y'

will give you the seconds since epoch.

Try

date -d "@$(stat -c '%Y' $myfile)" "+%m/%d/%Y"

to get the date, and read through man date to get the time in the format you want to, replacing '%F' in the command line above:

date -d "@$(stat -c '%Y' $myfile)" "+%H:%M"

EDIT: used your formats.

EDIT2: I really don't think your date format is wise, because it's just so ambiguous for anyone not from the US, and also it's not easily sortable. But it's a cultural thing, so this is more of a hint: If you want to make your usable for people from abroad, either use Year-month-day as format, or get the current locale's setting to format dates.

Print a file's last modified date in Bash

You can use the
stat
command

stat -c %y "$entry"

More info


%y time of last modification, human-readable

Linux Script to get time in seconds when folder was last modified

If you want the number of seconds ago:

echo $[$(date +%s)-$(stat --printf "%Y" /tmp/log)]

Time since last update of a file in human readable format

You can write a function:

time_since() {
file=$1
now=$(date +%s)
file_time=$(stat -c %Y "$file")
seconds=$((now - file_time))
hh=$(( $seconds / 3600 ))
mm=$(( ($seconds - $hh * 3600) / 60 ))
ss=$(( $seconds - $hh * 3600 - $mm * 60 ))
printf '%d:%02d:%02d\n' $hh $mm $ss
}

time_since /var/cache/apt
# output => 332:17:07

how to use shell script checking last changed time of a file

You can get the last modification time of a file with stat, and the current date with date. You can use format strings for both to get them in "seconds since the epoch":

current=`date +%s`
last_modified=`stat -c "%Y" $file`

Then, it's pretty easy to put that in a condition. For example:

if [ $(($current-$last_modified)) -gt 180 ]; then 
echo "old";
else
echo "new";
fi


Related Topics



Leave a reply



Submit