Rename a File with mv
mv old-name.txt new-name.txt
The file stays in the same directory with the same content, owner, and permissions; only the name changes. mv prints nothing when it succeeds, so check the result with ls -l.
Do Not Overwrite Another File
If a file with the new name already exists, mv replaces it without asking. Two options prevent that:
mv -i report.txt archive.txt # asks before overwriting
mv -n report.txt archive.txt # never overwrites
Add -v to print each rename as it happens, which is useful in scripts and loops. On AlmaLinux and Rocky Linux, the root account's shell settings usually define mv as mv -i, so you may see the question even without typing the option.
Rename and Move in One Step
mv draft.txt /var/www/docs/final.txt
The file moves to the other directory and gets its new name at the same time. Within one file system this is instant for any file size, because only the directory entry changes; between file systems mv copies the data and then deletes the original. More options are in the mv command guide.
Names with Spaces and Special Characters
mv "monthly report.pdf" monthly-report.pdf
mv monthly\ report.pdf monthly-report.pdf
mv -- -draft.txt draft.txt
Quote a name that contains spaces, or escape each space with a backslash; the Tab key completes names and adds the escapes for you. A name that starts with a dash looks like an option, so put -- before it or write it as ./-draft.txt.
Change the Extension of Many Files
A for loop with parameter expansion renames a whole set. Run it with echo first to see the commands it would execute:
for f in *.txt; do echo mv -n -- "$f" "${f%.txt}.md"; done
for f in *.txt; do mv -n -- "$f" "${f%.txt}.md"; done
${f%.txt} is the name with .txt cut from the end. Other expansions help too: "${f// /_}" replaces every space with an underscore. Loops are explained in bash for loop.
The rename Tool
Two different programs share the name rename, and their syntax is not the same:
| Systems | Version | Pattern |
|---|---|---|
| Debian, Ubuntu | Perl rename | s/old/new/ |
| AlmaLinux, Rocky | util-linux | old new |
On Debian and Ubuntu, install it with sudo apt install rename. It takes a Perl expression, and -n shows what would happen without renaming anything:
rename -n 's/\.jpeg$/.jpg/' *.jpeg
rename 's/\.jpeg$/.jpg/' *.jpeg
rename -n 'y/A-Z/a-z/' *
The last line previews lowercasing every name. On AlmaLinux and Rocky Linux, rename comes from util-linux and replaces the first occurrence of a plain string, without regular expressions. Its -n option is a dry run as well, but it prints nothing unless you add -v, which shows each change:
rename -nv .jpeg .jpg *.jpeg
rename -v .jpeg .jpg *.jpeg
If you are not sure which one you have, rename --version tells you.
Rename Files in Subdirectories with find
A glob such as *.jpeg only sees the current directory. To rename through a whole tree, combine find with a small shell command, and preview it first by putting echo before mv:
find . -type f -name '*.jpeg' -exec sh -c 'mv -n -- "$1" "${1%.jpeg}.jpg"' _ {} \;
find passes each path as $1, and the expansion changes only the extension, so the file stays in its own directory. The selection options are in the find command guide.
Renaming a Directory
A directory is renamed with the same mv command and needs no -r. The details, including what happens when the target name already exists, are in how to rename a directory in Linux.