Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Created May 20, 2013 18:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ashrithr/5614283 to your computer and use it in GitHub Desktop.
Save ashrithr/5614283 to your computer and use it in GitHub Desktop.
bash function to compare if a file is older than 28 hours or not
#######BEGIN SCRIPT############
#!/bin/bash
# This checks that the specified file is less than 28 hours old.
# returns 0 if younger than 28 hours.
# returns 1 if older than 28 hours.
#funtion arguments -> filename to comapre against curr time
function comparedate() {
if [ ! -f $1 ]; then
echo "file $1 does not exist"
exit 1
fi
MAXAGE=$(bc <<< '28*60*60') # seconds in 28 hours
# file age in seconds = current_time - file_modification_time.
FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$1")))
test $FILEAGE -lt $MAXAGE && {
echo "$1 is less than 28 hours old."
return 0
}
echo "$1 is older than 28 hours seconds."
return 1
}
#sample usage of function test if file /tmp/test.sh is older than 28 hours or not
comparedate /tmp/test.sh
#######END_SCRIPT#########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment