kind of human readable for file-age

This commit is contained in:
ville rantanen
2018-12-20 16:23:50 +02:00
parent 2ce7b86b1b
commit bc9dd12a40

View File

@@ -1,16 +1,17 @@
#!/bin/bash #!/bin/bash
_help() { function _help() {
echo ' echo '
Usage: file-age {file} [units] 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 [units] is one of S, M, H, d, w, m, y, 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
' '
exit exit
} }
@@ -18,6 +19,17 @@ for (( i=1; i<=$#; i++ ))
do [[ "${!i}" = "-h" ]] && _help do [[ "${!i}" = "-h" ]] && _help
done 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" STATFILE="$1"
[[ -z "$STATFILE" ]] && _help [[ -z "$STATFILE" ]] && _help
test -e "$STATFILE" || { echo File "$STATFILE" does not exist >&2; exit 1; } test -e "$STATFILE" || { echo File "$STATFILE" does not exist >&2; exit 1; }
@@ -45,6 +57,8 @@ case $UNITS in
echo $(( $age / 2592000 ));; echo $(( $age / 2592000 ));;
y) y)
echo $(( $age / 31536000 ));; echo $(( $age / 31536000 ));;
human)
_human "$age";;
*) *)
echo Unit $UNITS not recognized. echo Unit $UNITS not recognized.
_help;; _help;;