2021-01-24 - Sorting 2000 Plus Jpg Files Into Folders
My NatureWatch Raspberry Pi camera has generated over 2000 images since I started running it back in the summer of 2020. I have a script that syncs the images from the camera to another Raspberry PI on a 5 minute interval. I put this in place following a loss of images resulting from a failure of the SD card. I also found that the interface of the camera that allows you to view the image gallery will fall over (server error) when there are lots of images to display so I periodically delete them.
The problem is I didn’t implement any kind of organisation into the process with the current sync script just dumping the images into a folder. I now have over 2000 images in the folder, so what I wanted to do was move the images into folders, 1 folder for each day there is at least one image for. The bonus here is that the images are nicely named with a date and time in the filename in the format YYYY-MM-DD-HH-MM-SS.jpg. e.g. 2021-01-20-10-01-19.jpg. I’m sure there are more efficient ways to do this but here’s the script I ran from the directory holding the images:
#!/usr/bin/env bash
directory=$(pwd)
files=$(ls $directory | grep jpg)
for f in $files
do
dir=$(echo $f | cut -d"-" -f1,2,3)
if [ ! -d "$dir" ]; then
mkdir "$directory/$dir"
fi
mv "$directory/$f" "$directory/$dir"
done
Breaking this down, first get a list of jpg images in the directory specified in $directory:
directory=$(pwd)
files=$(ls $directory | grep jpg)
Next start a loop through the filenames stored in the $files variable, $f will hold the file name on each iteration:
for f in $files
do
Using the filename $f, determine the folder name by selecting YYYY-MMM-DD from the filename by using cut to return the first 3 fields delimited by “-". We could also use -c1-10 instead of -d -f to return the first 10 characters. Whichever way we do it, assign it to the $dir variable. If the directory doesn’t exist then create it:
dir=$(echo $f | cut -d"-" -f1,2,3)
if [ ! -d "$dir" ]; then
mkdir "$directory/$dir"
fi
Finally move the file into the new directory. The loop continues until there are now more filenames in $files and closes with done.
mv "$directory/$f" "$directory/$dir"
done
I’ve added this to my NatureWatch repository along with my sync script.