Skip to content

Instantly share code, notes, and snippets.

@cherdt
Created May 19, 2017 03:10
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 cherdt/fea863773d106e958abecaec51637822 to your computer and use it in GitHub Desktop.
Save cherdt/fea863773d106e958abecaec51637822 to your computer and use it in GitHub Desktop.
A bash script that returns the age of a file (since last change) in days
#!/bin/bash
# confirm we received an argument
if [ $# -lt 1 ]
then
echo "USAGE: bash daysold.sh <filename>"
exit 1
fi
# confirm the argument is a file
if [ ! -f $1 ]
then
echo "$1 does not exist"
exit 1
fi
# calculate file age in days
NOWSECONDS=$(date +%s)
FILESECONDS=$(stat -c %Z $1)
SECONDSDIFFERENCE=$(expr $NOWSECONDS - $FILESECONDS)
DAYSDIFFERENCE=$(expr $SECONDSDIFFERENCE / 84600)
# display result
echo $DAYSDIFFERENCE
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment