Why a batch rename overwrites or skips files (name collisions explained)

You ran a bulk rename on 100 files and ended up with 97. Or the command stopped halfway with 'a file with that name already exists'. Every one of these comes from a small set of collisions.

The five collisions

  1. Two files, one new name. Removing a number or a prefix can make different names identical: photo1.jpg and photo2.jpg with the digits stripped both become photo.jpg. A plain mv lets the second one replace the first without asking. Fix: number them, or add something unique.
  2. A new name equals a file you are not renaming. Replacing spaces with dashes in my file.txt collides with an existing my-file.txt. Fix: preview, then pick a different rule or rename the other file too.
  3. Case-only differences. On Windows and (by default) macOS, Photo.jpg and photo.jpg are the same file. Lowercasing a set that contains both is a collision, and renaming Photo.jpg to photo.jpg alone can fail with 'already exists' on tools that compare names naively. Fix: treat names case-insensitively when checking, and rename case-only changes through a temporary name.
  4. Swaps and chains. Renaming a.txt to b.txt and b.txt to a.txt cannot be done in two direct steps, and a chain like 1→2, 2→3 works only in the right order. Fix: first move each file to a temporary name, then to its final name. The tool does this automatically when needed.
  5. Names the system will not accept. Windows rejects < > : " / \ | ? *, names that end in a dot or space, and reserved names such as CON, PRN, AUX, NUL, COM1 to COM9 and LPT1 to LPT9, with or without an extension. Many systems also limit a name to 255 bytes. Fix: remove the characters, or choose a different name.

Safe commands

ShellSafe formWhy
bash / zshmv -n -- 'old' 'new'-n never overwrites; -- ends options so -weird.txt works; quotes keep spaces together
PowerShellRename-Item -LiteralPath 'old' -NewName 'new'Fails if the target exists; -LiteralPath stops [1] being read as a wildcard
Windows cmdren "old" "new"Also fails on an existing name, but cannot rename a file to a different folder

If it already happened

Stop and do not run more renames in that folder. Check your backups, the Recycle Bin or Trash, and file-history features: an overwritten file is gone from the folder, but a backup may hold it. If you still have the original list of names, the hash generator can confirm whether two surviving files are identical copies. Next time, keep the undo script that the batch rename guide describes.

Related

How to batch rename files safely (without uploading them) · Why file10 sorts before file2 (leading zeros) · Case-only renames on Windows, macOS and Git · Windows reserved names: CON, NUL, AUX · Remove spaces and symbols from file names · Batch rename tool (names only, in your browser) · Compress many photos at once · Invisible characters in text

Questions

Why did my batch rename end up with fewer files?

Probably two or more files were given the same new name and each one replaced the one before it. Preview the new names first and number or otherwise distinguish them.

Why does renaming Photo.jpg to photo.jpg say the file already exists?

On Windows and default macOS volumes names are case-insensitive, so both names point to the same file. Rename through a temporary name, which is what the batch rename tool's script does.

How do I swap the names of two files?

Rename the first to a temporary name, the second to the first name, then the temporary one to the second name. A direct pair of renames cannot work.

Which file names does Windows not allow?

Names containing < > : " / \ | ? *, names ending with a dot or space, and reserved device names such as CON, PRN, AUX, NUL, COM1 to COM9 and LPT1 to LPT9.

Does mv -n protect me?

Yes for files that already exist in the folder: -n makes mv skip instead of overwrite. It does not stop two of your own renames from fighting, so still preview first.

Published .

Try it

Related guides

Newest guides