Created
February 16, 2013 19:38
-
-
Save PHLAK/4968388 to your computer and use it in GitHub Desktop.
Screenshot archiving script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DAYS=5 | |
# Calculate the threshold value | |
THRESHOLD=$(($DAYS * 24 * 60 * 60)) | |
# Set current time | |
TIMESTAMP=$(date +%s) | |
# Set script path | |
SCRIPT_DIR=$(dirname $(readlink -f $0)) | |
# Set the screenshot path | |
SCREENSHOTS=$(readlink -f "$SCRIPT_DIR/../*") | |
if [ "$1" == "-v" ]; then | |
echo "================================================================================" | |
echo "THRESHOLD: $THRESHOLD" | |
echo "NOW: $TIMESTAMP" | |
echo "SCRIPT_DIR: $SCRIPT_DIR" | |
echo "SCREENSHOTS: $SCREENSHOTS" | |
echo "================================================================================" | |
fi | |
# Process the screenshots | |
for FILE in $SCREENSHOTS; do | |
# Check if file is a file | |
if [ -f "$FILE" ]; then | |
if [ "$1" == "-v" ]; then | |
echo; echo "$FILE" | |
fi | |
# Get file's MIME type | |
MIME_TYPE=$(file -ib "$FILE" | grep "image") | |
if [ -n $MIME_TPYE ]; then | |
# Do some time-based calculations | |
MODIFIED=$(stat -c %Y "$FILE") | |
FILE_AGE=$(($TIMESTAMP - $MODIFIED)) | |
if [ "$1" == "-v" ]; then | |
echo " MIME: $MIME_TYPE" | |
echo " MOD_TIME: $MODIFIED" | |
echo " AGE: $FILE_AGE" | |
fi | |
# Compare file age to threshold | |
if [ $FILE_AGE -gt $THRESHOLD ]; then | |
# Send notification to user | |
echo -n "Archiving $FILE ... " | |
# Get modified year and month | |
YEAR=$(date -d @$MODIFIED +%Y) | |
MONTH=$(date -d @$MODIFIED +%m) | |
# Set the archive directory | |
ARCHIVE_DIR="$SCRIPT_DIR/$YEAR-$MONTH" | |
if [ "$1" == "-v" ]; then | |
echo " ARCHIVE_DIR: $ARCHIVE_DIR" | |
fi | |
# Create archive dir if it doesn't exist | |
if [ ! -d $ARCHIVE_DIR ]; then | |
mkdir -p $ARCHIVE_DIR | |
fi | |
if [ -d $ARCHIVE_DIR ]; then | |
# Move file to archive | |
mv "$FILE" $ARCHIVE_DIR | |
# Final | |
FINAL_FILE="$ARCHIVE_DIR/$(basename "$FILE")" | |
if [ "$1" == "-v" ]; then | |
echo " FINAL_FILE: $FINAL_FILE" | |
fi | |
# Verify file move | |
if [ -f "$FINAL_FILE" ]; then | |
echo "SUCCESS!" | |
else | |
echo "FAILED!" | |
fi | |
else | |
echo "FAILED!" | |
fi | |
fi | |
fi | |
fi | |
done | |
echo "Archive process complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment