Bulk rename files by extension

Medium risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Renames every file with one extension to another (for example .jpeg to .jpg) in a directory, previewing the changes before applying them.

filesrenamebatchextension

Parameters

Directory containing the files to rename.

Current extension, without the dot, e.g. "jpeg".

New extension, without the dot, e.g. "jpg".

Commands

Preview every rename before doing it

for f in "."/*.<from_ext>; do echo "$f -> ${f%.<from_ext>}.<to_ext>"; done

Rename each matching file to the new extension

for f in "."/*.<from_ext>; do mv "$f" "${f%.<from_ext>}.<to_ext>"; done

Verification

ls "."/*.<to_ext>

The renamed files are listed; no files with the old extension remain.

Undo

Rename the files back to the original extension

for f in "."/*.<to_ext>; do mv "$f" "${f%.<to_ext>}.<from_ext>"; done

Pitfalls

  • If no file matches, the unquoted glob is passed through literally and mv fails on a file named "*.ext" — run the preview first.
  • Renaming to an extension that already exists on another file silently overwrites it on most shells.
  • The undo only works if no file with the original extension was created in the meantime.

Related