Skip to content

Instantly share code, notes, and snippets.

@howellcc
Created March 3, 2021 13:29
Show Gist options
  • Save howellcc/8f791e4509c488496f9b815e6847656a to your computer and use it in GitHub Desktop.
Save howellcc/8f791e4509c488496f9b815e6847656a to your computer and use it in GitHub Desktop.
Script to sort uploaded photos and videos into year/month sorted forlders
#!/bin/bash
#script to take a source dir and organize photos from that dir into a dest dir with a hierarchy of year/month/photo_name
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 1 ] || die "Usage: processPhotos.sh {input folder}"
inputPath="$1"
outputPath="{output folder}"
cd "$inputPath"
contentsarray=(*)
fileMoveCount=0
for ((i = 0; i < ${#contentsarray[@]}; i++))
do
echo "item: ${contentsarray[$i]##/*/}"
if [ ! -d "${contentsarray[$i]}" ]
then
#echo "Item is a file"
filename=${contentsarray[$i]##/*/}
filepath=$(printf '%s\n' "${contentsarray[$i]}")
fileyear=$(date -r "$filepath" +"%Y")
filemonth=$(date -r "$filepath" +"%m")
#echo "Date: $filemonth Filename: $filename Filepath: $filepath"
[ ! -d "$outputPath/$fileyear" ] && mkdir "$outputPath/$fileyear"
[ ! -d "$outputPath/$fileyear/$filemonth" ] && mkdir "$outputPath/$fileyear/$filemonth"
if [ ! -f "$outputPath/$fileyear/$filemonth/$filename" ]
then
mv "$filename" "$outputPath/$fileyear/$filemonth/" && ((fileMoveCount++))
#echo "mv $filename $outputPath/$fileyear/$filemonth/" && ((fileMoveCount++))
else
[ ! -d "$outputPath/possibleDuplicates" ] && mkdir "$outputPath/possibleDuplicates"
[ ! -f "$outputPath/possibleDuplicates/$filename" ] && mv "$filename" "$outputPath/possibleDuplicates" && ((fileMoveCount++))
fi
else
echo "Item is a folder"
fi
done
echo "File organization complete - $fileMoveCount moved"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment