Generated image
|| #resource #automation

Bulk Rename Photos

Rename photos based on the date they were taken

Problem

I take a lot of photos on my phone and I want to quickly rename them all into a standard format.

Solution

for file in $(ls *.jpg); do mv -n $file $(exiftool -d "%Y%m%d_%H%M%S" -DateTimeOriginal -S -s $file).jpg; done

The script above will rename all images in the current directory to the time they were taken.

The resulting files will look like this:

20220514_155042.jpg
20220508_163754.jpg
20220507_114358.jpg

This does not work with files that have a space in their name.

The -n in the move command prevents it from overwriting existing files. This is important if there are multiple photos taken at the same time.

Details

At a high level, this command will iterate over the results of the ls file and apply a mv command to each one. The output file name of the mv command is based on the output of the exiftool command.

The exiftool application is essential to read the photo metadata and extract the original time a photo was taken. This will not work without it installed.

Here is the command broken down into pieces:

You can quickly tweak the script in the following ways:


Maybe try another one?

Experimenting with AI Dipping my toes in the world of running AI models locally 🏡
2024.09.09
Gitlab: Build Previews Setting up build previews for static sites on Gitlab
2021.09.05
Getting back to writing
2024.08.06