如何在 Windows、macOS 和 Linux 安全地批次重新命名檔案
How to bulk rename files safely on Windows, macOS and Linux
這篇指南整理 Windows、macOS 和 Linux 的安全批次改名方法,建議先預覽結果、避免覆寫,並保留復原方式。檔名衝突可能導致檔案被覆寫或批次操作中途失敗,文中建議先將檔案移至暫存名稱,再改成最終名稱。文中亦介紹作者自製的免費開源命令列工具 TidyBatch,它預設預覽改名結果、檢查大小寫不敏感的檔名衝突,並記錄操作以供復原。
Renaming hundreds of files takes seconds. Undoing a bad rename can be impossible. Here is how to do it on Windows, macOS and Linux without losing anything, with the free built-in options first.
Disclosure: this article was written with AI help (Claude), and every command below was run before publishing; the output shown is real. We make TidyBatch, a free command-line tool mentioned at the end, which has a paid Pro version (9 €).
The three rules
- Preview before you apply. Every option below has a way to see the result first.
- Never let a rename overwrite a file. If two files end up with the same name, one of them can silently disappear.
- Have a way back. A backup, a copy of the folder, or a tool with undo.
Windows
File Explorer. Select the files, press F2, type a name and press Enter. Windows names them Name (1), Name (2), … Fine for quick numbering, but no preview and no patterns.
PowerToys PowerRename (free, from Microsoft). Install Microsoft PowerToys, select files, right-click, Rename with PowerRename. Live preview, regular expressions, numbering and date variables. Ctrl+Z in Explorer undoes the batch right afterwards.
PowerShell. Add -WhatIf to preview:
# Preview: lowercase all .jpg names and replace spaces with underscores
Get-ChildItem -File *.jpg | Rename-Item -NewName { $_.Name.ToLower() -replace ' ', '_' } -WhatIf
# Looks right? Run the same line without -WhatIf
Get-ChildItem -File *.jpg | Rename-Item -NewName { $_.Name.ToLower() -replace ' ', '_' }
With two files, Holiday Photo.JPG and Beach Day.JPG, the -WhatIf line prints:
What if: Performing the operation "Rename File" on target "Item: …/Beach Day.JPG Destination: …/beach_day.jpg".
What if: Performing the operation "Rename File" on target "Item: …/Holiday Photo.JPG Destination: …/holiday_photo.jpg".
Rename-Item refuses to replace an existing file (Rename-Item: The file '…/a_b.jpg' already exists.). Good, but in a long batch a few files fail halfway through, so read the red errors.
macOS
Finder. Select the files, right-click, Rename…. Replace Text, Add Text or Format with a counter, with an example of the result. Edit → Undo straight away if it is wrong.
Terminal. Use mv -n (no-clobber) and print the commands before you run them:
# Preview (echo only), then remove "echo" to run
for f in *.JPG; do echo mv -n -- "$f" "${f%.JPG}.jpg"; done
mv -n -- IMG_1.JPG IMG_1.jpg
mv -n -- Photo One.JPG Photo One.jpg
Linux
There are two different rename commands (Perl and util-linux) with different syntax. Run rename --version to see which you have. The util-linux one only prints the plan with -v:
# Perl rename (Debian/Ubuntu "rename" package): spaces -> underscores
rename -n 's/ /_/g' *
# rename(My Trip 2.jpg, My_Trip_2.jpg)
# rename(Photo One.jpg, Photo_One.jpg)
# util-linux rename (Arch, Fedora…): replace the first "IMG_" with "photo_"
rename -nv IMG_ photo_ *
# `IMG_1.JPG' -> `photo_1.JPG'
The trap: chains silently overwrite files
Say you renumber 1.txt, 2.txt, 3.txt to 2.txt, 3.txt, 4.txt. A simple loop renames 1.txt → 2.txt first. On Linux and macOS a plain mv or Python's os.rename overwrites the original 2.txt without warning. On Windows it fails instead, leaving the folder half renamed.
The fix is two phases: move every file to a temporary name, then to its final name. Or rename into a new, empty folder. A related trap: on Windows and macOS Photo.JPG and photo.jpg are the same file, so collision checks must ignore case.
A tool that does this for you
TidyBatch is a free, open-source command-line tool (we made it) built around these rules. It previews by default, renames in two phases, checks collisions without case, and keeps a journal so you can undo with one command, even after an interrupted run. Install it with pipx install tidybatch, or try it without installing with uvx tidybatch. On a folder with three files:
$ tidybatch rename Downloads --slug --lower
Café Menu.txt -> cafe_menu.txt
Holiday Photo 1.JPG -> holiday_photo_1.jpg
Report Final (v2).PDF -> report_final_v2.pdf
3 file(s) would change.
Dry run: nothing was changed. Add --apply to do it.
$ tidybatch rename Downloads --slug --lower --apply
Café Menu.txt -> cafe_menu.txt
Holiday Photo 1.JPG -> holiday_photo_1.jpg
Report Final (v2).PDF -> report_final_v2.pdf
3 file(s) will change.
Done. To revert: tidybatch undo "Downloads" (journal: journal-20261103-010602-321629.json)
$ tidybatch undo Downloads
Restored 3 file(s), skipped 0. Journal: journal-20261103-010602-321629.json
It runs locally and needs no account. rename and undo are free; the paid Pro (9 €) adds organize and dedupe. More guides: tidybatch.github.io/guides. Code: github.com/tidybatch/tidybatch.
If you only use Windows and like clicking, PowerRename is excellent and free. TidyBatch is for when you want the same command on every OS, in scripts, with an undo that survives closing the window.
來源:DEV Community · dev.to