Skip to content

Instantly share code, notes, and snippets.

@gnanet
Forked from ashrithr/compare_file_age.sh
Created July 19, 2017 18:08
Show Gist options
  • Save gnanet/e26356c616854c6e7f29c0ca14838a27 to your computer and use it in GitHub Desktop.
Save gnanet/e26356c616854c6e7f29c0ca14838a27 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