66 lines
1.3 KiB
Bash
Executable File
66 lines
1.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
function _help() {
|
|
echo '
|
|
Usage: file-age {file} [units]
|
|
|
|
Checks file modification date, and the current time. Prints the age of
|
|
the file in [units].
|
|
|
|
[units] is one of S, M, H, d, w, m, y, human
|
|
seconds is the default. All values floored.
|
|
month = 30 days
|
|
year = 365 days
|
|
human = YY-MM-DD hh:mm:ss
|
|
'
|
|
exit
|
|
}
|
|
for (( i=1; i<=$#; i++ ))
|
|
do [[ "${!i}" = "-h" ]] && _help
|
|
done
|
|
|
|
function _human {
|
|
local T=$1
|
|
local Y=$((T/60/60/24/365))
|
|
local m=$((T/60/60/24/30%12))
|
|
local D=$((T/60/60/24%30))
|
|
local H=$((T/60/60%24))
|
|
local M=$((T/60%60))
|
|
local S=$((T%60))
|
|
printf '%02d-%02d-%02d %02d:%02d:%02d\n' $Y $m $D $H $M $S
|
|
}
|
|
|
|
STATFILE="$1"
|
|
[[ -z "$STATFILE" ]] && _help
|
|
test -e "$STATFILE" || { echo File "$STATFILE" does not exist >&2; exit 1; }
|
|
|
|
UNITS=${2:-S}
|
|
case $OSTYPE in
|
|
darwin*) created=$( stat -f %c -t %s "$STATFILE" ) ;;
|
|
*) created=$( stat -c %Y "$STATFILE" ) ;;
|
|
esac
|
|
now=$( date +%s )
|
|
age=$(( $now - $created ))
|
|
|
|
case $UNITS in
|
|
S)
|
|
echo $age;;
|
|
M)
|
|
echo $(( $age / 60 ));;
|
|
H)
|
|
echo $(( $age / 3600 ));;
|
|
d)
|
|
echo $(( $age / 86400 ));;
|
|
w)
|
|
echo $(( $age / 604800 ));;
|
|
m)
|
|
echo $(( $age / 2592000 ));;
|
|
y)
|
|
echo $(( $age / 31536000 ));;
|
|
human)
|
|
_human "$age";;
|
|
*)
|
|
echo Unit $UNITS not recognized.
|
|
_help;;
|
|
esac
|