file age
This commit is contained in:
48
files/file-age
Executable file
48
files/file-age
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
|
||||
_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
|
||||
seconds is the default. All values floored.
|
||||
month = 30 days
|
||||
year = 365 days
|
||||
'
|
||||
exit
|
||||
}
|
||||
for (( i=1; i<=$#; i++ ))
|
||||
do [[ "${!i}" = "-h" ]] && _help
|
||||
done
|
||||
|
||||
STATFILE="$1"
|
||||
[[ -z "$STATFILE" ]] && _help
|
||||
test -e "$STATFILE" || { echo File "$STATFILE" does not exist >&2; exit 1; }
|
||||
|
||||
UNITS=${2:-S}
|
||||
created=$( stat -c %Y "$STATFILE" )
|
||||
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 ));;
|
||||
*)
|
||||
echo Unit $UNITS not recognized.
|
||||
_help;;
|
||||
esac
|
||||
Reference in New Issue
Block a user