首页 > Sysadmin > 正文

Linux File Renaming: The Complete Guide

2026-11-03 Sysadmin

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 -n or rename.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.ul only modifies the filename, not the path or directory.
  • The Perl rename (prename package) supports regex, but its syntax differs from rename.ul.
  • Large-scale batch renames across filesystems use mv copy + delete, doubling I/O time.

Test record

  • Environment: WSL2 Ubuntu 24.04, kernel 5.15.
  • mv series: basic rename, -n block overwrite, -f force overwrite, -i interactive prompt, directory rename all passed.
  • rename.ul: substring replacement, -n preview, -a replace all, --no-overwrite all passed.
  • Cleanup: temp directory /tmp/rename-test removed, no WSL artifacts remain.

References

Commands tested on WSL Ubuntu (24.04) and cross-checked against official Debian and upstream documentation.