Batch Rename Files by Replacing Text in the Name

Use a shell loop with parameter expansion to rename many files safely.

macOSrenameFilesautomationMedium risk
Run `for f in *OLD*; do mv -n "$f" "${f//OLD/NEW}"; done` inside the target folder.

01Command

Command
$ cd ~/Downloads/照片 && for f in *IMG*; do mv -n "$f" "${f//IMG/照片}"; done

02What it does

Bash’s `${f//old/new}` replaces every occurrence inside the variable, and quoting keeps spaces from splitting names — the two most common causes of botched renames. `-n` on mv refuses to overwrite an existing file. Test first by replacing `mv -n` with `echo mv -n` to dry-run the mapping.

03Risk

RiskMedium risk
!Warning: Run a dry run before the real rename; `${f//…}` on macOS default bash/zsh replaces all occurrences, while `${f/…}` (one slash) replaces only the first. There is no built-in undo — the reverse loop is your safety net, so keep the folder window open to eyeball results.

04Undo

Undo
$ cd ~/Downloads/照片 && for f in *照片*; do mv -n "$f" "${f//照片/IMG}"; done

05Sources

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

06Related solutions