Rename Files with Sequence Numbers in a Shell Loop

Number a batch of files in order using a one-line for loop — no third-party renamer needed.

macOSrenameFilesMedium risk
Paste the for loop with mv and printf %03d to turn every matching file into photo-001, photo-002 and so on.

01Command

Command
$ cd ~/Desktop/Photos && i=1; for f in *.jpg; do mv "$f" "photo-$(printf '%03d' $i).jpg"; i=$((i+1)); done

02What it does

The glob *.jpg expands in alphabetical order, so sort your filenames first if order matters (ls sorts them the same way). Change %03d to %02d or %04d to control how many digits you get, and edit the photo- prefix as you like. Dry-run first by replacing mv with echo mv to preview the new names.

03Risk

RiskMedium risk
!Warning: The loop overwrites any file that already has a target name — check the folder for existing photo-001.jpg style names first.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • mv – Move or rename files
    ss64.com · technical · 2026-09-17
    Evidence: mv renames files in place; quoting "$f" keeps names with spaces safe.
  • Rename files, folders, and disks on Mac
    Apple Support · vendor-doc · 2026-09-17
    Evidence: Finder can rename batches, but only with a text pattern — a shell loop gives ordered sequence numbers.

Last verified: 2026-09-18 · Category: Files & Folders

06Related solutions