Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Vultour
Created September 27, 2016 16:50
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 Vultour/75809ddafd796d023a933197f59ce0ab to your computer and use it in GitHub Desktop.
Save Vultour/75809ddafd796d023a933197f59ce0ab to your computer and use it in GitHub Desktop.
Time-specific find
#!/bin/bash
USAGE="Usage: $0 <FOLDER> <ORIGIN-FILE> <TIME-LIMIT> [RECURSIVE?]"
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
echo $USAGE
exit 1
fi
DIR=$1
ORG="$(date --date "$(stat $2 | grep -E 'Modify: (.*)$' | grep -oE '[0-9]+.*')" +"%s")"
LIM=$3
REC=""
if [ -z $4 ]; then
REC="-maxdepth 1"
fi
if [ ! -d $DIR ]; then
echo "Error: directory '$DIR' doesn't exist"
fi
echo "Origin: $ORG"
for item in $(find $DIR $REC); do
FILE=$item
TIMESTAMP="$(date --date "$(stat $FILE | grep -E 'Modify: (.*)$' | grep -oE '[0-9]+.*')" +"%s")"
if [ $LIM -gt 0 ]; then
CHECK=$(($ORG + $LIM))
if [ $TIMESTAMP -lt $CHECK ] && [ $TIMESTAMP -gt $ORG ]; then
echo $FILE
fi
fi
if [ $LIM -lt 0 ]; then
CHECK=$(($ORG $LIM))
if [ $TIMESTAMP -gt $CHECK ] && [ $TIMESTAMP -lt $ORG ]; then
echo $FILE
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment