Organizing photos can be a daunting task, especially if you have a lot of them. In this blog post, we will explore how to use Bash to organize photos based on their creation date. This is how I organize m iPhone pics based on the year and month.
Prerequisites
- Basic knowledge of Bash scripting
- A Linux-based system (e.g., Ubuntu, Debian, Raspberry Pi)
- Some photos to work with
Getting Started
The first step is to create a directory where we will store our organized photos. In this example, we will create a directory called organized_photos in the home directory:
Next, we need to create a Bash script that will move the photos to the appropriate directories based on their creation date. Here is an example script:
#!/bin/bash
src_dir="/path/to/photos"
dest_dir="$HOME/organized_photos"
initials="AB"
find "$src_dir" -type f -not -path '*/.sync/*' -print | while read file; do
year=$(date -r "$file" "+%Y")
month=$(date -r "$file" "+%m")
dest="$dest_dir/$year/$month"
mkdir -p "$dest"
timestamp=$(date -r "$file" "+%Y%m%d_%H%M%S")
extension="${file##*.}"
newfilename="${timestamp}_${initials}.$extension"
mv "$file" "$dest/$newfilename"
done
Let's go over this script line by line:
src_diris the directory where the photos are currently locateddest_diris the directory where the organized photos will be storedinitialsis a string that will be appended to the beginning of the filenamefindcommand searches for all the files in thesrc_dirdirectory (excluding the.syncdirectory and its subdirectories) and pipes them to the while loopyearandmonthare extracted from the file's modification date using thedatecommand- A new directory is created in
dest_dirusing the year and month extracted above timestampis the file's modification date and timeextensionis the file extensionnewfilenameis the new filename that consists of the timestamp, initials, and original filename with the extension
Now that we have our Bash script, we need to make it executable:
chmod +x organize_photos.sh
Finally, we can run the script:
./organize_photos.sh
After the script finishes running, the organized photos will be located in the organized_photos directory. The photos will be organized by year and month, with the filenames replaced with the initials and timestamp.
Conclusion
Using Bash to organize photos based on their creation date is a simple yet powerful technique. With a little bit of Bash scripting knowledge, you can easily create a script that will save you hours of manual organization.
No comments :
Post a Comment