Skip to content

Instantly share code, notes, and snippets.

@dpellenwood
Created December 1, 2015 20:35
Show Gist options
  • Save dpellenwood/c971c3a13cfa8eeff463 to your computer and use it in GitHub Desktop.
Save dpellenwood/c971c3a13cfa8eeff463 to your computer and use it in GitHub Desktop.
A simple script to pull & save files from a remote HTTP server
#!/bin/bash
# A simple script to pull & save files from a remote HTTP server
# Created by DPE on 11/30/2015
# david@dpedesign.com
IMAGE_URL='http://northernimages.us/cameras/lsmvc2/lsmvc2-800.jpg'
DIR_NAME='lsmvc2'
# Setup Passed Arguments
while [[ $# > 1 ]]
do
key="$1"
case $key in
-u|--url)
IMAGE_URL="$2"
shift # past argument
;;
-d|--directory)
DIR_NAME="$2"
shift # past argument
;;
--default)
DEFAULT=YES
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
DIR_PATH="/home/dpe/dpedesign/LiftBridgeIs/$DIR_NAME"
#echo "$IMAGE_URL"
#echo "$DIR_NAME"
#echo "$DIR_PATH"
# Get the Last-Modified Header for the current remote image
REMOTE_MODIFIED=$(curl -sI "$IMAGE_URL" | sed -n -e 's/^.*Last-Modified: //p')
REMOTE_MODIFIED=$(date -d "$REMOTE_MODIFIED" +"%s")
#Get the Modified timestamp for the lastest saved local image
LOCAL_MODIFIED=$(find "$DIR_PATH" -type f -exec stat -c "%Y" {} + | sort -r | head -1)
#echo "$REMOTE_MODIFIED"
#echo "$LOCAL_MODIFIED"
# If the remote file is newer, get it.
if [ "$REMOTE_MODIFIED" -gt "$LOCAL_MODIFIED" ]; then
#echo 'get it'
curl "$IMAGE_URL" -o "$DIR_PATH/$DIR_NAME-$REMOTE_MODIFIED.jpg" -s -L -R
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment