To batch rename an image sequence reliably, give every file the same prefix and a zero-padded number of fixed width, such as shot_0001.png, shot_0002.png and so on. Zero-padding makes alphabetical order match numerical order, which is what editors, FFmpeg and most file managers expect. The safest method is to pad the numbers you already have first, then renumber if you need to, and to test every command on a copy of the folder before running it for real.
This guide explains why sequences break, then gives working commands for Windows PowerShell and for macOS or Linux shells, along with the mistakes we see most often when people hand us image folders for caption and video tools.
Why numbering goes wrong
Computers sort file names as text unless told otherwise. Text sorting compares one character at a time, so img10.png comes before img2.png because the character 1 is lower than 2. A sequence that looks fine when you view it in Windows Explorer (which uses a "natural" sort) can come out scrambled in a script, an FFmpeg command or a cloud storage listing.
Common symptoms:
- A Ken Burns slideshow jumps from image 1 to image 10, then back to image 2.
- An editor imports a sequence as several separate clips because the numbering has gaps or inconsistent widths.
- FFmpeg stops reading a sequence early because it cannot find the next expected number.
- Two tools disagree about the order of the same folder.
Zero-padding removes the ambiguity. When every number has the same width, text order and numeric order are identical: img_0002.png sorts before img_0010.png everywhere.
How much padding?
Pick a width that covers the largest number you will ever need, plus some room:
| Expected files | Suggested width | Example |
|---|---|---|
| Up to 99 | 3 digits | scene_007.png |
| Up to 9,999 | 4 digits | frame_0042.png |
| Up to 99,999 | 5 digits | frame_00042.png |
For rendered frame sequences, four digits is the usual minimum; a 10-minute render at 25 fps is 15,000 frames, which needs five. For stills in a storyboard or slideshow, three or four is plenty. Choose once per project and keep it consistent (our guide to video project folder structure covers naming conventions more broadly).
Before you rename anything
Renaming is quick to do and slow to undo. A short routine prevents most disasters:
- Work on a copy. Duplicate the folder and run commands there first.
- Close anything using the files. Editors and preview apps can lock files on Windows, leaving you with a half-renamed folder.
- Check the current order. List the files and confirm the order you see is the order you want.
- Watch for collisions. If you rename
img_2.pngtoimg_1.pngwhileimg_1.pngstill exists, you can overwrite or fail. Renaming into a new prefix avoids this. - Dry run. Every command below has a version that prints what it would do without doing it.
Windows PowerShell
These commands work in Windows PowerShell 5.1 and PowerShell 7. Open PowerShell in the folder (Shift + right-click in Explorer, or cd to it).
Pad existing numbers
If your files are named like img1.png, img2.png … img120.png, this pads the number to four digits whilst keeping everything else:
# Dry run: show old -> new names
Get-ChildItem -Filter 'img*.png' | ForEach-Object {
if ($_.BaseName -match '^(.*?)(\d+)$') {
$new = '{0}{1:D4}{2}' -f $Matches[1], [int]$Matches[2], $_.Extension
"$($_.Name) -> $new"
}
}
When the output looks right, run the real rename. Note the parentheses around Get-ChildItem: they make PowerShell collect the full file list before renaming, so renamed files are not picked up a second time.
(Get-ChildItem -Filter 'img*.png') | ForEach-Object {
if ($_.BaseName -match '^(.*?)(\d+)$') {
$new = '{0}{1:D4}{2}' -f $Matches[1], [int]$Matches[2], $_.Extension
if ($_.Name -ne $new) { Rename-Item -LiteralPath $_.FullName -NewName $new }
}
}
The {1:D4} format pads to four digits; change it to D3 or D5 as needed.
Renumber from 1 in natural order
Sometimes the existing names are messy (IMG_4471.JPG, IMG_4475.JPG, final shot.jpg) and you want a clean shot_0001 sequence. Sort-Object Name would sort as text, so this uses a natural sort that pads any digits inside the name for comparison only:
$natural = { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20, '0') }) }
$i = 1
(Get-ChildItem -File -Include *.jpg, *.png -Path .\*) | Sort-Object $natural | ForEach-Object {
$new = 'shot_{0:D4}{1}' -f $i, $_.Extension.ToLower()
Rename-Item -LiteralPath $_.FullName -NewName $new -WhatIf
$i++
}
-WhatIf makes this a dry run. Remove it once you are happy. Because the new prefix (shot_) differs from the old names, nothing collides.
macOS and Linux shells
These examples work in bash and in zsh, the default shell on current macOS. Run them from inside the folder.
Pad existing numbers
# Dry run: replace "echo mv" with "mv -n" to rename for real
for f in img*.png; do
n=${f#img} # strip the prefix
n=${n%.png} # strip the extension
case $n in (*[!0-9]*|'') continue;; esac # skip names without a plain number
new=$(printf 'img%04d.png' "$((10#$n))")
[ "$f" != "$new" ] && echo mv -n -- "$f" "$new"
done
A few details matter here:
10#$nforces the number to be read as decimal. Without it, a name likeimg08.pngcan be treated as an invalid octal number.mv -nrefuses to overwrite an existing file, which protects you from collisions.- The
--stops file names that begin with a dash being read as options.
Renumber from 1
Once numbers are padded, the shell's glob order (*.png) is also the correct numeric order, which makes renumbering simple:
i=1
for f in *.png; do
new=$(printf 'shot_%04d.png' "$i")
echo mv -n -- "$f" "$new"
i=$((i + 1))
done
This is why we pad first and renumber second: padding fixes the order, and then any tool that sorts as text agrees with you. If your source names mix several different number patterns, pad each pattern first or rename into a clean prefix by hand-checking the list.
Using the sequence afterwards
With a clean, padded sequence, FFmpeg can read the images directly using a pattern:
ffmpeg -framerate 25 -i shot_%04d.png -c:v libx264 -pix_fmt yuv420p slideshow.mp4
%04d means "a four-digit, zero-padded number". If your sequence starts at a number other than 0 or 1 (FFmpeg looks in a small range near the start by default), add -start_number 101 before -i. Gaps in the numbering will stop the read, which is another reason to renumber cleanly. Our list of FFmpeg commands for creators has more on image sequences, and the official options are documented at >ffmpeg.org.
For still-image videos, a padded sequence also makes it easy to line images up with narration beats and to add movement later with the Ken Burns effect.
When you need duplicates as well as new names
A common storyboard task is "scene 3 needs its image five times, scene 4 needs it twice", so that each still covers a set number of slots in a timeline. You can script that, but it is fiddly to get the counts and the padding right. Our Image Replicator does this in the browser: it duplicates each image the number of times you choose and names the copies as one continuous, zero-padded sequence per scene.
Common mistakes
- Mixing widths.
img_001.pngalongsideimg_1000.pngbreaks as soon as the sequence passes 999. Choose the width up front. - Renaming in place with overlapping names. Renumbering
img_0005toimg_0001while the originalimg_0001exists either fails or destroys a file. Use a new prefix or a two-pass rename. - Trusting the file manager's order. Explorer and Finder both show a natural sort, so a folder can look correct and still be wrong for scripts.
- Mixed case extensions.
.JPGand.jpgare different to many tools on macOS and Linux. Normalise them to lower case during the rename. - Hidden files. macOS can create
._files on external drives; they match*.pngpatterns and break sequences. Delete or exclude them. - Spaces and special characters. They work if you quote everything, but they cause trouble in older scripts. Prefer letters, numbers, underscores and hyphens.
Quick checklist
- Copy the folder and test there first.
- Decide the padding width for the whole project.
- Pad existing numbers before any renumbering.
- Use a new prefix to avoid collisions.
- Run a dry run (
-WhatIforecho mv) and read the output. - Confirm the final list sorts correctly as plain text.
- Check there are no gaps if another tool will read the sequence.
FAQ
Why does my sequence play in the wrong order in my editor?
The file numbers are probably not zero-padded, so text sorting puts 10 before 2. Pad every number to the same width and re-import.
Is it safe to undo a batch rename?
Only if you kept a record. The dry-run output lists old and new names, so save it to a text file before running the real command. Easier still, work on a copy and delete the original only when you are happy.
Can I rename thousands of files at once?
Yes. Both the PowerShell and shell loops handle thousands of files in seconds. The limit is usually the tools reading the folder afterwards, not the rename itself.
What about RAW or video files with sidecar files?
Rename the main file and its sidecar (for example .xmp) together using the same new base name, or the link between them is lost. Test on a few pairs first.