Skip to content

Instantly share code, notes, and snippets.

@jtickle
Last active August 29, 2015 13:59
Show Gist options
  • Save jtickle/10730730 to your computer and use it in GitHub Desktop.
Save jtickle/10730730 to your computer and use it in GitHub Desktop.
BASH "Older Than" Script
#!/bin/bash
# This script will return 0 if the specified file is older, 1 if newer than the specified date.
# Intended for use with GNU Find, like so:
#
# find filez/ -exec ./older.sh {} "2 days ago 00:00" + -delete
#
# If you want to find files that are newer than 2 days ago, you'd do something like:
#
# find filez/ -not -exec ./older.sh {} "2 days ago 00:00" + -delete
#
# Or you could change the COMPARATOR below.
COMPARATOR="-le"
function usage {
echo "Usage: $0 <date> <file>"
exit
}
if [ $# -ne 2 ]; then
usage
fi
if [ -e "$1" ]; then
FILE="$1"
DATE="$2"
else
if [ -e "$2" ]; then
FILE="$2"
DATE="$1"
else
usage
fi
fi
T1=$(date --date="$(stat --printf %y "$FILE")" +%s)
T2=$(date --date="$DATE" +%s)
let "dT=$T2-$T1"
if [ $dT $COMPARATOR 0 ]; then
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment