Batch Rename Files by Replacing Text in the Name
Use a shell loop with parameter expansion to rename many files safely.
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/照片}"; done02What 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
!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}"; done05Sources
- rename – Renames multiple filesEvidence: Bash parameter expansion replaces text inside file names safely
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.