Friday, April 21, 2023

Organizing Photos Using Bash

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_dir is the directory where the photos are currently located
  • dest_dir is the directory where the organized photos will be stored
  • initials is a string that will be appended to the beginning of the filename
  • find command searches for all the files in the src_dir directory (excluding the .sync directory and its subdirectories) and pipes them to the while loop
  • year and month are extracted from the file's modification date using the date command
  • A new directory is created in dest_dir using the year and month extracted above
  • timestamp is the file's modification date and time
  • extension is the file extension
  • newfilename is 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.



Related Posts


No comments :

Post a Comment