Created
May 19, 2017 03:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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