Skip to content

Instantly share code, notes, and snippets.

@girasquid
Created July 19, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girasquid/15b2c9e049991deac64b677cf02b8692 to your computer and use it in GitHub Desktop.
Save girasquid/15b2c9e049991deac64b677cf02b8692 to your computer and use it in GitHub Desktop.
Bash script that calculates age of each folder in a directory, based on git commits
#!/usr/bin/env bash
# http://www.daveeddy.com/2014/06/29/human-readable-duration-in-bash/
human() {
local seconds=$1
if ((seconds < 0)); then
((seconds *= -1))
fi
local times=(
$((seconds / 60 / 60 / 24 / 365)) # years
$((seconds / 60 / 60 / 24 / 30)) # months
$((seconds / 60 / 60 / 24 / 7)) # weeks
$((seconds / 60 / 60 / 24)) # days
$((seconds / 60 / 60)) # hours
$((seconds / 60)) # minutes
$((seconds)) # seconds
)
local names=(year month week day hour minute second)
local i
for ((i = 0; i < ${#names[@]}; i++)); do
if ((${times[$i]} > 1)); then
echo "${times[$i]} ${names[$i]}s"
return
elif ((${times[$i]} == 1)); then
echo "${times[$i]} ${names[$i]}"
return
fi
done
echo '0 seconds'
}
directory_age() {
local f=$1
echo -n "${f}: "
newest=`git rev-list --parents HEAD --format=%at -- ${f} | (head -n2 | tail -n1)`
oldest=`git rev-list --parents HEAD --format=%at -- ${f} | tail -n1`
if [ -z ${newest} ]
then
echo "skipped (failed to parse commits)"
continue
fi
difference=`expr ${newest} - ${oldest}`
human ${difference}
}
echo "Project Ages:"
echo "================"
for f in *; do
[ -d "${f}" ] || continue
[ ${f} = "archive" ] && continue
directory_age ${f}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment