How do I include a shell variable in an ExifTool rename path?

Asked 9/3/2017

4 views

2 answers

0

I want to rename and move photos with ExifTool into this structure:

YYYY/ MM/ EVENT/ YYYYMMDD_HHMMSS.ext

For example, I prompt for an event name in a bash shell script, then want ExifTool to use that value in the destination path. My current command uses %event inside the -d format string, but it does not work. How should I pass the user-provided event name so it becomes part of the folder structure?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

8y ago

2 Answers

5

You aren't using the variable with the user-provided input correctly. This answer assumes you're trying to run this script using a bash (or compatible) shell. If not, you'll need to refer to how to handle variables within your shell.

You need to reference the shell variable using the syntax of the shell, not the exiftool argument, and remove the single ("strong") quotes, which cause the variable reference to be a mere literal string (passing the name, not the value, to exiftool). What you actually want is:

event="EVENT"
exiftool -r '-FileName<CreateDate' -d "%Y/%m/$event/%Y%m%d_%H%M%S%%-c.%%le" .

The double ("weak") quotes will allow the correctly-referenced variable (using "$" as a prefix) to be substituted by the shell, while still protecting any whitespace in the user input.

Originally by user37649. Source · Licensed CC BY-SA 4.0

user37649

8y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

If you’re running this from bash (or a similar shell), the event name must be expanded by the shell, not by ExifTool.

Use a shell variable like $event, and put the -d argument in double quotes so the shell substitutes the value:

echo 'Name of event?'
read event
exiftool -r '-FileName<CreateDate' -d "%Y/%m/$event/%Y%m%d_%H%M%S%%-c.%%le" .

Why your original command failed:

  • %event is not an ExifTool date-format token.
  • Single quotes prevent shell variable expansion, so the text is passed literally.

Key points:

  • Use $event to reference the shell variable.
  • Use double quotes around the -d string so spaces in the event name are preserved.
  • Keep '-FileName<CreateDate' quoted so the shell doesn’t interpret < as redirection.

This will create paths like: 2024/06/Birthday/20240603_154500.jpg

UniqueBot

AI

8y ago

Your Answer