Last active
April 6, 2019 17:59
-
-
Save sgrasso/56dae2089e88c089fa75e00f172fa58b to your computer and use it in GitHub Desktop.
Owncloud cron script. Moves iPhone photos from InstantUpload folder to mounted drive. Organizes by Year, Month of photo.
This file contains 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
#!/usr/bin/env bash | |
BASE_DIR=/var/www/owncloud/data | |
DEST_DIR=/var/www/mydata | |
# Get all InstantUpload folders | |
dirs="$(find "$BASE_DIR" -type d -name InstantUpload)" | |
## Find those files | |
for d in $dirs ; do | |
find "$d" -type f -name "*" | | |
while IFS= read -r file; | |
do | |
finalDate=""; | |
filedate=${file%-*}; | |
filedate=${filedate#*-}; | |
IFS=$"-"; | |
n=1; | |
for i in $filedate; do | |
if [ $n -le 3 ] | |
then | |
finalDate=$finalDate-$i; | |
fi | |
n=$((n + 1)) | |
done | |
unset IFS; | |
finalDate=${finalDate#-}; | |
## Get the file's modification year | |
#"Photo-2016-03-27-16-25-24_1940.JPG" | cut -d '-' -f2 | |
year="$(date -d "$finalDate" +%Y)"; | |
## Get the file's modification month | |
#"Photo-2016-03-27-16-25-24_1940.JPG" | cut -d '-' -f3 | |
month="$(date -d "$finalDate" +%B)"; | |
## Create the directories if they don't exist. The -p flag | |
## makes 'mkdir' create the parent directories as needed so | |
## you don't need to create $year explicitly. | |
[ -d "$DEST_DIR/$year/$month" ] || mkdir -p "$DEST_DIR/$year/$month"; | |
## Copy and Remove the file | |
cp -rp "$file" "$DEST_DIR/$year/$month" && rm -r "$file"; | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment