Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the number of days since file is last modified

I want to get the number of days since file last modified date to today's date.

I use this $ ls -l uname.txt | awk '{print $6 , "", $7}' but it gives me the last modified date. I want to know the number of days from a last modified date to today's date.

Any way to do this?

like image 908
Stunner Avatar asked Oct 03 '13 05:10

Stunner


2 Answers

Instead of using ls, you can use date -r to tell you the modification date of the file. In addition to that, date's %s specifier, which formats the date in seconds since the epoch, is useful for calculations. Combining the two easily results in the desired number of days:

mod=$(date -r uname.txt +%s)
now=$(date +%s)          
days=$(expr \( $now - $mod \) / 86400)
echo $days
like image 99
user4815162342 Avatar answered Sep 23 '22 22:09

user4815162342


Try creating a script:

#!/bin/bash

ftime=`stat -c %Y uname.txt`
ctime=`date +%s`
diff=$(( (ctime - ftime) / 86400 ))
echo $diff
like image 23
Jon Lin Avatar answered Sep 22 '22 22:09

Jon Lin