Get the number of days since the last file change

I want to get the number of days from the date the file was last modified to today.

I use this $ ls -l uname.txt | awk '{print $6, "", $7}' $ ls -l uname.txt | awk '{print $6, "", $7}' $ ls -l uname.txt | awk '{print $6, "", $7}' $ 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 the last modified date to today.

Any way to do this?

+6
source share
3 answers

Instead of using ls you can use date -r to indicate the date the file was modified. In addition to this, the date %s specifier, which formats the date in seconds from the era, is useful for calculations. The combination of these two results leads to the desired number of days:

 mod=$(date -r uname.txt +%s) now=$(date +%s) days=$(expr \( $now - $mod \) / 86400) echo $days 
+5
source

Try creating a script:

 #!/bin/bash ftime=`stat -c %Y uname.txt` ctime=`date +%s` diff=$(( (ctime - ftime) / 86400 )) echo $diff 
+1
source

You can wrap the differences in GNU and BSD status with some mathematical and basic readable BASH API:

 since_last_modified() { local modified local now=$(date +%s) local period=$2 stat -f %m $1 > /dev/null 2>&1 && modified=$(stat -f %m $1) # BSD stat stat -c %Y $1 > /dev/null 2>&1 && modified=$(stat -c %Y $1) # GNU stat case $period in day|days) period=86400 ;; # 1 day in seconds hour|hours) period=1440 ;; # 1 hour in seconds minute|minutes) period=60 ;; # 1 minute in seconds *) period= ;; # default to seconds esac if [[ $period > 0 ]]; then echo "$(( (now - modified) / period ))" else echo "$(( now - modified ))" fi } 

The main use of seconds since the last modification:

 since_last_modified uname.txt 

or minutes stored in a variable

 minutes_since=$(since_last_modified uname.txt minutes) 
0
source

Source: https://habr.com/ru/post/955164/


All Articles