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.
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)); done02What 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
!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 filesEvidence: mv renames files in place; quoting "$f" keeps names with spaces safe.
- Rename files, folders, and disks on MacEvidence: 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
Find files larger than 500 MB on Mac
List the biggest files in your home folder that are eating disk space.
Show file extensions for all files on Mac
Make Finder display .pdf, .docx, .png and every other extension at all times.
Find Which Process Has a File or Volume Open with lsof
List every open file descriptor — files, directories, and network sockets — per process.