Skip to content
Log In Sign Up info@muvcloud.com

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:

SystemsVersionPattern
Debian, UbuntuPerl renames/old/new/
AlmaLinux, Rockyutil-linuxold 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations