Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created July 24, 2015 21:26
Show Gist options
  • Save ilyaevseev/7442859c58abab3e3558 to your computer and use it in GitHub Desktop.
Save ilyaevseev/7442859c58abab3e3558 to your computer and use it in GitHub Desktop.
Find files in specific datetime interval, then copy to selected folder.
#!/bin/sh
test $# = 2 || test $# = 3 || { echo "Usage: $0 start-time end-time [dest-folder]"; exit; }
Fail() { echo "ERROR: $@" 1>&2; exit 1; }
NOW="$(date +%s)"
START="$(date +%s -d "$1" 2>/dev/null)"; test -z "$START" && Fail "wrong start-time: $1"
FINISH="$(date +%s -d "$2" 2>/dev/null)"; test -z "$FINISH" && Fail "wrong end-time: $2"
DESTDIR="$3"
#est "$FINISH" -gt "$NOW" && Fail "end-time cannot be in future."
test "$FINISH" -lt "$START" && Fail "end-time cannot be before start-time."
test -z "$DESTDIR" || test -d "$DESTDIR/" || Fail "wrong target directory $DESTDIR"
MAXDELTA="$(expr '(' $NOW - $START + 59 ')' / 60)"
MINDELTA="$(expr '(' $NOW - $FINISH ')' / 60)"
DoFind() { find . -type "f" -mmin "-$MAXDELTA" -mmin "+$MINDELTA" "$@"; }
if test -d "$DESTDIR"
then DoFind -exec cp -a '{}' $DESTDIR/ ';'
else DoFind
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment