How to Rename Files and Directories in Linux

Renaming files is one of the most fundamental tasks you need to perform on a Linux system. You can rename files using a GUI file manager or via the command-line terminal.
Renaming a single file is trivial, but efficiently renaming multiple files at once can be a challenge and requires a solid understanding of command-line tools.
In this tutorial, we will show you how to use the mv and rename commands to rename files and directories.
Quick Reference
| Task | Command |
|---|---|
| Rename a single file | mv old.txt new.txt |
| Rename a directory | mv olddir newdir |
| Preview rename changes | rename -n 's/old/new/' *.txt |
| Change file extension (batch) | rename 's/\.html$/.php/' *.html |
| Batch rename with shell loop | for f in *.html; do mv -- "$f" "${f%.html}.php"; done |
| Replace spaces with underscores | rename 'y/ /_/' * |
| Convert filenames to lowercase | rename 'y/A-Z/a-z/' * |
| Convert filenames to uppercase | rename 'y/a-z/A-Z/' * |
Renaming Files with the mv Command
The mv command
(short for move) is used to rename or move files from one location to another. The syntax for the mv command is as follows:
mv [OPTIONS] SOURCE DESTINATIONThe SOURCE can be one or more files or directories, and DESTINATION can be a single file or directory.
How mv interprets arguments:
- If multiple files are specified as
SOURCE, theDESTINATIONmust be a directory. In this case, theSOURCEfiles are moved to the target directory. - If
SOURCEis a file andDESTINATIONis a directory, the file is moved into that directory. - If
SOURCEis a file andDESTINATIONdoes not exist, the file is renamed.
Renaming a Single File
To rename the file file1.txt to file2.txt, run:
mv file1.txt file2.txtRenaming Multiple Files with the mv Command
The mv command can rename only one file at a time. However, it can be combined with other commands such as find
or shell loops for batch renaming.
Example: Change .html Files to .php (Bash Loop)
for f in *.html; do
mv -- "$f" "${f%.html}.php"
doneExplanation:
- The first line creates a
forloop and iterates through a list of all files ending with.html. - The second line applies to each item of the list and moves the file to a new one replacing
.htmlwith.php. The part${f%.html}uses shell parameter expansion to remove the.htmlsuffix from the filename. doneindicates the end of the loop segment.
Example: Recursive Renaming with find + mv
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;The find command passes all files ending with .html in the current directory to mv one by one using the -exec option. The string {} is the name of the file currently being processed.
As you can see from the examples above, renaming multiple files using the mv command is not an easy task as it requires a good knowledge of Bash scripting.
Renaming Files with the rename Command
The rename command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions.
There are multiple versions of the rename command with different syntax. In this tutorial, we will be using the Perl version of the rename command.
If you do not have this version installed on your system, you can easily install it using the package manager of your distribution.
Install
renameon Ubuntu, Debian, and DerivativesTerminalsudo apt install renameInstall
renameon Fedora, RHEL, and DerivativesTerminalsudo dnf install prenameInstall
renameon Arch LinuxTerminalyay perl-rename
The syntax for the rename command is as follows:
rename [OPTIONS] perlexpr filesfilesare renamed according to the specified Perl regular expression. You can read more about Perl regular expressions here .FILEScan be glob patterns or explicit file lists.
The following example will change all files with the extension .html to .php:
rename 's/\.html$/\.php/' *.htmlThe expression s/\.html$/\.php/ is a Perl substitution with three parts separated by /:
s— substitution operator: find and replace.\.html$— the search pattern.\.matches a literal dot (without the backslash,.would match any character).$anchors the match to the end of the filename, so only a trailing.htmlis matched.\.php— the replacement string (literal text).
Use the -n option to preview changes before applying:
rename -n 's/\.html$/\.php/' *.htmlThe output will look something like this:
rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)Always preview when working on large file sets.
By default, the rename command does not overwrite existing files. Pass the -f option to allow existing files to be overwritten:
rename -f 's/\.html$/\.php/' *.htmlUse with caution as this will overwrite files without prompting.
Below are a few more common examples of how to use the rename command:
Replace spaces in filenames with underscores
Terminalrename 'y/ /_/' *The
y/searchlist/replacementlist/operator is a transliteration — it replaces each character in the search list with the corresponding character in the replacement list.y/ /_/replaces every space with an underscore, one character at a time.Convert filenames to lowercase
Terminalrename 'y/A-Z/a-z/' *y/A-Z/a-z/replaces each uppercase letterA–Zwith its lowercase equivalenta–z.Convert filenames to uppercase
Terminalrename 'y/a-z/A-Z/' *
Best Practices
- Always test first using
rename -nto preview changes before applying them. - Use quoted variables to handle filenames with spaces safely.
- Prefer
renameovermvfor complex or large-scale renaming tasks. - Avoid running batch renames as root unless necessary.
Troubleshooting
mv silently overwrites an existing file
By default, mv replaces the destination file without warning. Use the -n (--no-clobber) flag to prevent overwriting: mv -n old.txt existing.txt. Use -i to be prompted before overwriting.
“rename: command not found”
The Perl rename utility is not installed. Install it with sudo apt install rename on Ubuntu/Debian or sudo dnf install prename on Fedora/RHEL. On some systems the command may be available as perl-rename or prename.
rename changes nothing or gives unexpected results
There are two different rename commands on Linux: the Perl version (covered here) and the util-linux version (found on some distributions by default). Their syntax is incompatible. Check which one you have with rename --version. If it shows util-linux, install the Perl version separately.
Filenames with spaces are not handled correctly
Always quote variables when using mv in loops: mv -- "$f" "$newname". The -- signals the end of options so filenames starting with - are handled safely.
FAQ
What is the difference between mv and rename for renaming files?mv renames one file at a time and is available on every Linux system. rename is a batch tool that applies a Perl regular expression to multiple filenames at once. Use mv for simple one-off renames and rename when you need to transform many filenames with a pattern.
Why does the rename command have different syntax on different systems?
There are two separate tools both called rename. The Perl version uses rename 's/old/new/' files syntax. The util-linux version uses rename old new files syntax. Check which is installed with rename --version before writing scripts that depend on it.
How do I rename files without accidentally overwriting existing ones?
With mv, use the -n flag: mv -n source dest. With rename, the default behavior is to refuse to overwrite — use -f only when you explicitly want to allow it.
How do I recursively rename files in subdirectories?
Use find combined with mv in a loop, or pass the result of find to rename:
find . -name "*.html" -exec rename 's/\.html$/.php/' {} \;Can I undo a rename operation?
Linux has no built-in undo for mv or rename. Always use rename -n to preview changes before applying, and consider making a backup of the directory before running batch operations.
Conclusion
Use mv for simple single-file renames and rename for efficient, regex-based batch operations. Always preview batch changes with rename -n before applying them. For moving files between directories rather than renaming them, see the mv command guide
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page