How to Rename Files in Linux: mv, rename and Loops
Updated
Linux has no separate rename command for single files: renaming is moving a file to a new name in the same folder, so the tool is mv. For many files you combine mv with a loop or use rename, which comes in two incompatible versions depending on your distribution. This guide covers all three, plus the graphical file managers.
Rename a single file with mv
mv old-name.txt new-name.txt
mv "Quarterly report.pdf" quarterly-report.pdf # quote names with spaces
mv -i draft.md notes/final.md # rename and move, ask before overwritingBy default mv silently overwrites an existing file with the target name. Three options protect you: -i asks before overwriting, -n never overwrites, and -v prints every rename. Linux file systems are case-sensitive, so Photo.JPG and photo.jpg are two different names and mv Photo.JPG photo.jpg works directly.
Rename many files with a for loop
A loop works in bash, zsh and every other POSIX shell and needs no extra package. Always quote "$f" so that names with spaces stay intact, and put -- before the names so that a file called -draft.txt is not read as an option.
Change the extension
for f in *.jpeg; do
mv -n -- "$f" "${f%.jpeg}.jpg"
done${f%.jpeg} removes .jpeg from the end of the name. ${f#prefix} removes text from the start in the same way.
Add a prefix
for f in *.pdf; do
mv -n -- "$f" "2026-11_$f"
doneReplace spaces with underscores (bash)
for f in *\ *; do
mv -n -- "$f" "${f// /_}"
done${f// /_} replaces every space. This substitution is a bash and zsh feature; it does not exist in plain sh or dash.
Number files with leading zeros
n=1
for f in *.jpg; do
new=$(printf 'holiday_%03d.jpg' "$n")
mv -n -- "$f" "$new"
n=$((n + 1))
doneThe shell expands *.jpg in alphabetical order (according to your locale), so the numbers follow the old names. %03d produces 001, 002 and so on.
Include subfolders with find
find . -type f -name '*.jpeg' -exec sh -c '
for f; do mv -n -- "$f" "${f%.jpeg}.jpg"; done
' sh {} +To test any loop, write echo mv instead of mv. The loop then prints each command instead of running it.
The rename command: two different programs
Many tutorials show rename commands that fail on the reader's system. The reason: there are two unrelated programs with this name.
| Perl rename | util-linux rename | |
|---|---|---|
| Default on | Debian, Ubuntu, Linux Mint (package rename) | Fedora, RHEL, CentOS, Arch, openSUSE |
| Syntax | rename 's/old/new/' files | rename old new files |
| Matching | Perl regular expressions | Plain text, first occurrence |
| Dry run | -n | -n (shows changes with -v) |
| Other name | file-rename; Arch: perl-rename; Fedora: prename | Debian: rename.ul in some releases |
Run rename --version to find out which one you have. The Perl version mentions File::Rename or Perl, the other one says util-linux. On Ubuntu the command may be missing entirely; install it with sudo apt install rename.
Perl rename (Debian and Ubuntu)
rename -n 's/\.jpeg$/.jpg/' *.jpeg # preview
rename 's/\.jpeg$/.jpg/' *.jpeg # rename
rename 's/ /_/g' * # all spaces to underscores
rename 'y/A-Z/a-z/' * # lowercase
rename 's/^IMG_/lisbon_/' IMG_*.jpg # replace a prefixThe first argument is a Perl expression applied to each name. s/old/new/ replaces the first match, s/old/new/g replaces all matches, and $ anchors the match to the end of the name. Perl rename refuses to overwrite existing files unless you add -f.
util-linux rename (Fedora, Arch, openSUSE)
rename -nv .jpeg .jpg *.jpeg # preview
rename .jpeg .jpg *.jpeg # rename
rename IMG_ lisbon_ IMG_*.jpg # replace a prefix
rename -o draft final *draft* # never overwrite existing filesThis version replaces the first occurrence of plain text, without regular expressions. Newer releases add -a to replace all occurrences and -l to replace the last one; check man rename on your system. For regular expressions on Fedora or Arch, install the Perl version (prename or perl-rename).
Graphical file managers
- GNOME Files (Nautilus): select several files and press F2. The dialog renames with a template (text plus numbering such as
001) or finds and replaces text, with a live preview. - KDE Dolphin: select the files and press F2. Type a name with
#where the number should go;###gives three-digit numbers. - Thunar (Xfce) includes a separate Bulk Rename tool with numbering, search and replace and case conversion.
When the command line is overkill
For a one-off batch of photos or documents, the online bulk renamer on bulkrename.com runs in Firefox or Chrome on Linux. It creates renamed copies with patterns such as holiday_[num] and leaves the originals unchanged. For scripts you want to keep, see renaming files with Python; an overview of all systems is in how to rename multiple files at once.