1
0
Fork 0
mirror of https://github.com/sbrl/bin.git synced 2018-01-10 21:33:46 +00:00
bin/organise-photos

93 lines
2.4 KiB
Text
Raw Normal View History

2017-01-04 14:55:14 +00:00
#!/usr/bin/env bash
# This script takes all the jpg and mp4 files in a directory and
# organises them into a folder structure based on the month and year
# they were last modified (taken).
2017-03-30 20:32:04 +00:00
# Sources and References
# - Bash controlled paralellisation - http://unix.stackexchange.com/a/216475/64687
2017-01-26 20:02:57 +00:00
DRY_RUN=false
while test $# -gt 0
do
case "$1" in
--help)
echo organise-photos
echo " by Starbeamrainbowlabs <feedback@starbeamrainbowlabs.com>"
echo "This script takes all the jpg and mp4 files in a directory and organises them into a folder structure based on the month and year they were last modified (taken)."
echo
echo "Options:"
echo " --help"
echo " Show this help message"
echo " --dry-run"
echo " Do a dry run - don't actually move any files."
exit
;;
--dry-run)
echo Activating dry run mode.
DRY_RUN=true
esac
shift
done
2017-03-30 20:32:04 +00:00
Months=(Zero January February March April May June July August September October November December);
handle_file() {
local filename=$1;
2017-01-04 14:55:14 +00:00
echo -ne "Processing ${filename} - ";
2017-03-30 20:32:04 +00:00
rawFilename=$(basename "${filename}");
extension=${filename##*.};
extension=${extension,,};
2017-01-04 14:55:14 +00:00
takenYear=$(date -r "${filename}" +%Y);
takenMonth=$(date -r "${filename}" +%-m);
takenMonthText=$(date -r "${filename}" +%B);
2017-03-30 20:32:04 +00:00
# Extract the date taken from jpegs
if [[ "${extension}" == "jpg" ]] || [[ "${extension}" == "jpeg" ]];
then
takenDate=$(jhead "${filename}" | grep -i 'Date/Time' | cut -d' ' -f6-7);
takenChars=$(echo ${takenDate} | wc -c);
if [[ ${takenChars} -gt 1 ]];
then
takenYear=$(echo ${takenDate} | cut -d ':' -f1);
takenMonth=$(echo ${takenDate} | cut -d ':' -f2);
takenMonthUnpadded=$(echo ${takenMonth//0});
takenMonthText=${Months[$takenMonthUnpadded]};
fi
fi
2017-01-04 14:55:14 +00:00
newFolder="${takenYear}/${takenMonth}-${takenMonthText}/";
newFilename="${newFolder}${filename}";
echo -ne "filing in ${newFolder} - ";
2017-01-26 20:02:57 +00:00
if [ "$DRY_RUN" = true ] ; then
echo dry run
continue
fi
2017-01-04 14:55:14 +00:00
mkdir -p "${newFolder}";
mv "${filename}" "${newFilename}";
echo Done\!;
2017-03-30 20:32:04 +00:00
}
# Set the number of paralell jobs to the number of cpus
N=$(grep -c ^processor /proc/cpuinfo);
find . -maxdepth 1 \( -iname "*.jpg" -o -iname "*.mp4" -o -iname "*.avi" -o -iname "*.png" \) -print0 | while read -r -d '' filename
do
((i=i%N)); ((i++==0)) && wait;
handle_file "${filename}"
2017-01-04 14:55:14 +00:00
done
echo "*** Complete! ***"
2017-01-26 20:02:57 +00:00
exit 0
2017-03-30 20:32:04 +00:00