To rename multiple files at once, use a for loop in Terminal:

for f in *.jpg; do mv "\$f" "photo_\$f"; done

This adds "photo_" to the beginning of every .jpg file in the current folder.

Common Batch Rename Patterns

Add a prefix to all files

for f in *; do mv "\$f" "2024_\$f"; done

Turns report.pdf into 2024_report.pdf.

Add a suffix (before the extension)

for f in *.png; do mv "\$f" "\${f%.png}_backup.png"; done

Turns image.png into image_backup.png.

Change file extension

for f in *.jpeg; do mv "\$f" "\${f%.jpeg}.jpg"; done

Renames all .jpeg files to .jpg.

Replace text in filenames

for f in *old*; do mv "\$f" "\${f//old/new}"; done

Replaces "old" with "new" in any filename containing "old".

Number files sequentially

i=1; for f in *.jpg; do mv "\$f" "photo_\$i.jpg"; ((i++)); done

Renames files to photo_1.jpg, photo_2.jpg, etc.

How the Syntax Works

Part Meaning
for f in *.jpg Loop through all .jpg files, calling each one \$f
do ... done Run this command for each file
mv "\$f" "newname" Rename the file
\${f%.jpg} The filename without the .jpg extension
\${f//old/new} Replace "old" with "new" in the filename

The quotes around "\$f" are important - they handle filenames with spaces.

Test Before You Commit

Before actually renaming, preview what would happen:

for f in *.jpg; do echo "mv '\$f' 'photo_\$f'"; done

This prints the commands without running them. Check the output looks right, then remove echo and the extra quotes to run it for real.

Using the rename Command

If you prefer a dedicated tool, install rename via Homebrew:

brew install rename

Then use it like this:

rename 's/old/new/' *.txt

This replaces "old" with "new" in all .txt filenames. It uses Perl regex syntax, which is powerful but has a learning curve.

Lowercase All Filenames

for f in *; do mv "\$f" "\$(echo \$f | tr '[:upper:]' '[:lower:]')"; done

Converts MyFile.TXT to myfile.txt.

Remove Spaces from Filenames

for f in *\ *; do mv "\$f" "\${f// /_}"; done

Replaces spaces with underscores: my file.txt becomes my_file.txt.

Common Mistakes

Forgetting quotes: If your filenames have spaces, you need quotes around "\$f". Without them, the command breaks.

Running in the wrong folder: Always pwd first to confirm you're in the right directory. Batch operations are hard to undo.

Not testing first: Use echo to preview before you run. It takes 10 seconds and can save you from a mess.

Overwriting files: If the new names already exist, mv will silently overwrite them. Check for conflicts first.

Undo a Batch Rename

There's no built-in undo. If you made a mistake:

  1. If you can reverse the pattern (like removing a prefix you added), write another loop to do that
  2. If the files are in version control (Git), restore them from there
  3. If you have Time Machine, restore from backup

This is why testing first matters.


Keep Learning

Batch renaming is one of many things Terminal does faster than clicking through Finder. The free course teaches you to think in these patterns.

Check it out at Mac Terminal for Humans.