much more accurate age
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
function _help() {
|
function _help() {
|
||||||
echo '
|
echo '
|
||||||
Usage: file-age {file} [units]
|
Usage: file-age {file} [units]
|
||||||
@@ -7,27 +7,67 @@ Usage: file-age {file} [units]
|
|||||||
Checks file modification date, and the current time. Prints the age of
|
Checks file modification date, and the current time. Prints the age of
|
||||||
the file in [units].
|
the file in [units].
|
||||||
|
|
||||||
[units] is one of S, M, H, d, w, m, y, human
|
[units] is one of S, M, H, d, w, m, y, full, human
|
||||||
seconds is the default. All values floored.
|
seconds is the default. All values floored.
|
||||||
month = 30 days
|
month = 30 days
|
||||||
year = 365 days
|
year = 365 days
|
||||||
human = YY-MM-DD hh:mm:ss
|
full = YY-MM-DD hh:mm:ss
|
||||||
'
|
human = 3 months 2 days
|
||||||
exit
|
'
|
||||||
|
exit
|
||||||
}
|
}
|
||||||
for (( i=1; i<=$#; i++ ))
|
for (( i=1; i<=$#; i++ ))
|
||||||
do [[ "${!i}" = "-h" ]] && _help
|
do [[ "${!i}" = "-h" ]] && _help
|
||||||
done
|
done
|
||||||
|
|
||||||
function _human {
|
function _human {
|
||||||
local T=$1
|
file_date=( $( date -r "$STATFILE" +"%Y %m %d %H %M %S" ) )
|
||||||
local Y=$((T/60/60/24/365))
|
current_date=( $( date +"%Y %m %d %H %M %S" ) )
|
||||||
local m=$((T/60/60/24/30%12))
|
local values=()
|
||||||
local D=$((T/60/60/24%30))
|
for (( i=0; $i<${#file_date[@]}; i++ )); do
|
||||||
local H=$((T/60/60%24))
|
values+=( $(( ${current_date[$i]#0} - ${file_date[$i]#0})) )
|
||||||
local M=$((T/60%60))
|
done
|
||||||
local S=$((T%60))
|
|
||||||
printf '%02d-%02d-%02d %02d:%02d:%02d\n' $Y $m $D $H $M $S
|
if [[ ${values[5]} -lt 0 ]]; then
|
||||||
|
values[5]=$(( 60 + ${values[5]} )); values[4]=$(( ${values[4]} -1 ))
|
||||||
|
fi
|
||||||
|
if [[ ${values[4]} -lt 0 ]]; then
|
||||||
|
values[4]=$(( 60 + ${values[4]} )); values[3]=$(( ${values[3]} -1 ))
|
||||||
|
fi
|
||||||
|
if [[ ${values[3]} -lt 0 ]]; then
|
||||||
|
values[3]=$(( 24 + ${values[3]} )); values[2]=$(( ${values[2]} -1 ))
|
||||||
|
fi
|
||||||
|
if [[ ${values[2]} -lt 0 ]]; then
|
||||||
|
case ${file_date[1]#0} in
|
||||||
|
1|3|5|7|8|10|12) mlen=31;;
|
||||||
|
2) mlen=28;;
|
||||||
|
*) mlen=30;;
|
||||||
|
esac
|
||||||
|
values[2]=$(( $mlen + ${values[2]} ));
|
||||||
|
values[1]=$(( ${values[1]} -1 ))
|
||||||
|
fi
|
||||||
|
if [[ ${values[1]} -lt 0 ]]; then
|
||||||
|
values[1]=$(( 12 + ${values[1]} )); values[0]=$(( ${values[0]} -1 ))
|
||||||
|
fi
|
||||||
|
if [[ "$1" = "full" ]]; then
|
||||||
|
printf '%02d-%02d-%02d %02d:%02d:%02d\n' ${values[@]}
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local units=( yr mo day hr min sec )
|
||||||
|
if [[ "${file_date[@]}" = "${current_date[@]}" ]]; then
|
||||||
|
printf " 0 sec\n"; return
|
||||||
|
fi
|
||||||
|
for (( i=0; $i<${#values[@]}; i++ )); do
|
||||||
|
if [[ ${values[$i]} -gt 0 ]]; then
|
||||||
|
j=$(( i + 1 ))
|
||||||
|
printf "%2s %-3s" ${values[$i]} ${units[$i]}
|
||||||
|
if [[ $j -lt ${#values[@]} ]]; then
|
||||||
|
printf " %2s %-3s" ${values[$j]} ${units[$j]}
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
STATFILE="$1"
|
STATFILE="$1"
|
||||||
@@ -44,22 +84,24 @@ age=$(( $now - $created ))
|
|||||||
|
|
||||||
case $UNITS in
|
case $UNITS in
|
||||||
S)
|
S)
|
||||||
echo $age;;
|
echo $age ;;
|
||||||
M)
|
M)
|
||||||
echo $(( $age / 60 ));;
|
echo $(( $age / 60 )) ;;
|
||||||
H)
|
H)
|
||||||
echo $(( $age / 3600 ));;
|
echo $(( $age / 3600 )) ;;
|
||||||
d)
|
d)
|
||||||
echo $(( $age / 86400 ));;
|
echo $(( $age / 86400 )) ;;
|
||||||
w)
|
w)
|
||||||
echo $(( $age / 604800 ));;
|
echo $(( $age / 604800 )) ;;
|
||||||
m)
|
m)
|
||||||
echo $(( $age / 2592000 ));;
|
echo $(( $age / 2592000 )) ;;
|
||||||
y)
|
y)
|
||||||
echo $(( $age / 31536000 ));;
|
echo $(( $age / 31536000 )) ;;
|
||||||
|
full)
|
||||||
|
_human full;;
|
||||||
human)
|
human)
|
||||||
_human "$age";;
|
_human;;
|
||||||
*)
|
*)
|
||||||
echo Unit $UNITS not recognized.
|
echo Unit $UNITS not recognized.
|
||||||
_help;;
|
_help ;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
Reference in New Issue
Block a user