mv handles single-file renames; rename.ul excels at batch substring replacement. All commands below were tested on WSL2 Ubuntu 24.04.
1. TL;DR: Pick the Right Tool
- Single file / directory rename:
mv old new, simplest and most universal. - Batch suffix/prefix changes:
rename.ul old new *, one-liner substring swap. - Avoid overwrites:
mv -norrename.ul --no-overwrite, skips existing targets. - Preview before bulk rename:
rename.ul -n old new *, dry-run to see effects.
2. mv: Single Files and Directories
mv old.txt new.txt
mv -n old.txt new.txt
mv -f old.txt new.txt
mv -i old.txt dst.txt
mv dir_a dir_b
mv is the default GNU coreutils tool for renaming files and directories [1]. -f forces overwrite, -n refuses overwrite, and -i prompts interactively—only the last one specified takes effect. For directories, no -r flag is needed; mv old_dir new_dir works directly [3].
Verified behavior:
$ mv -n file1.txt newfile.txt
mv: not replacing 'newfile.txt'
$ echo 'n' | mv -i src.txt dst.txt
mv: overwrite 'dst.txt'?
src.txt kept, dst.txt untouched
Across filesystems, mv automatically falls back to copy + delete without raising an error [3].
3. rename.ul: Batch Substring Replacement
rename.ul txt bak *.txt
rename.ul -n old new *
rename.ul -a old new *
rename.ul --no-overwrite old new *
rename.ul belongs to the util-linux package. On Ubuntu 24.04 the binary is rename.ul (not rename); install with sudo apt install util-linux [2]. It replaces the first occurrence of a substring in filenames; -a replaces all occurrences, -n previews without changing anything, and --no-overwrite skips existing files.
Real-world example: convert all .htm files to .html
rename.ul .htm .html *.htm
4. Use Cases, Permissions, and Version Differences
| Use case | mv | rename.ul |
|---|---|---|
| Single file rename | Yes | Yes |
| Batch suffix/prefix change | Needs loop | One-liner |
| Directory rename | Yes | No (files only) |
| Cross-filesystem | Copy + delete | Not suitable |
| Overwrite protection | -n / -i / -f | –no-overwrite / -n |
| Ubuntu < 22.04 | Default | May be rename.ul |
| Containers (no apt) | Default | May be missing |
Version compatibility note: --no-overwrite requires util-linux 2.39+. On older Ubuntu releases, use -n (preview) as a workaround [2].
5. Rollback, Troubleshooting, and Limitations
Quick diagnostics
# If rename.ul is missing
which rename.ul || sudo apt install util-linux
# Preview before bulk rename
rename.ul -n old new *
# Force overwrite existing file
mv -f source.txt dest.txt
| Symptom | Cause | Fix |
|---|---|---|
rename: command not found |
Binary is rename.ul | Use rename.ul, or install prename |
mv: not replacing |
-n used but target exists | Add -f or remove the target first |
mv: overwrite 'x'? hangs |
Interactive mode waiting for input | Press y / n, or add -f to skip |
| Batch rename produces mixed names | Skipped dry-run | Preview first with rename.ul -n |
Limitations
rename.ulonly modifies the filename, not the path or directory.- The Perl
rename(prenamepackage) supports regex, but its syntax differs fromrename.ul. - Large-scale batch renames across filesystems use
mvcopy + delete, doubling I/O time.
Test record
- Environment: WSL2 Ubuntu 24.04, kernel 5.15.
- mv series: basic rename,
-nblock overwrite,-fforce overwrite,-iinteractive prompt, directory rename all passed. - rename.ul: substring replacement,
-npreview,-areplace all,--no-overwriteall passed. - Cleanup: temp directory
/tmp/rename-testremoved, no WSL artifacts remain.
References
- [1] mv(1) – Linux manual page (man7.org)
- [2] rename – Ubuntu manpages (util-linux)
- [3] mv invocation – GNU Coreutils Manual
Commands tested on WSL Ubuntu (24.04) and cross-checked against official Debian and upstream documentation.