How to Rename Files and Directories in Linux

By 

Updated on

7 min read

How to Rename Files 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

TaskCommand
Rename a single filemv old.txt new.txt
Rename a directorymv olddir newdir
Preview rename changesrename -n 's/old/new/' *.txt
Change file extension (batch)rename 's/\.html$/.php/' *.html
Batch rename with shell loopfor f in *.html; do mv -- "$f" "${f%.html}.php"; done
Replace spaces with underscoresrename 'y/ /_/' *
Convert filenames to lowercaserename 'y/A-Z/a-z/' *
Convert filenames to uppercaserename '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:

txt
mv [OPTIONS] SOURCE DESTINATION

The 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, the DESTINATION must be a directory. In this case, the SOURCE files are moved to the target directory.
  • If SOURCE is a file and DESTINATION is a directory, the file is moved into that directory.
  • If SOURCE is a file and DESTINATION does not exist, the file is renamed.

Renaming a Single File

To rename the file file1.txt to file2.txt, run:

Terminal
mv file1.txt file2.txt

Renaming 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)

Terminal
for f in *.html; do
    mv -- "$f" "${f%.html}.php"
done

Explanation:

  • The first line creates a for loop 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 .html with .php. The part ${f%.html} uses shell parameter expansion to remove the .html suffix from the filename.
  • done indicates the end of the loop segment.

Example: Recursive Renaming with find + mv

Terminal
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 rename on Ubuntu, Debian, and Derivatives

    Terminal
    sudo apt install rename
  • Install rename on Fedora, RHEL, and Derivatives

    Terminal
    sudo dnf install prename
  • Install rename on Arch Linux

    Terminal
    yay perl-rename

The syntax for the rename command is as follows:

txt
rename [OPTIONS] perlexpr files
  • files are renamed according to the specified Perl regular expression. You can read more about Perl regular expressions here .
  • FILES can be glob patterns or explicit file lists.

The following example will change all files with the extension .html to .php:

Terminal
rename 's/\.html$/\.php/' *.html

The 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 .html is matched.
  • \.php — the replacement string (literal text).

Use the -n option to preview changes before applying:

Terminal
rename -n 's/\.html$/\.php/' *.html

The output will look something like this:

output
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:

Terminal
rename -f 's/\.html$/\.php/' *.html

Use 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

    Terminal
    rename '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

    Terminal
    rename 'y/A-Z/a-z/' *

    y/A-Z/a-z/ replaces each uppercase letter AZ with its lowercase equivalent az.

  • Convert filenames to uppercase

    Terminal
    rename 'y/a-z/A-Z/' *

Best Practices

  • Always test first using rename -n to preview changes before applying them.
  • Use quoted variables to handle filenames with spaces safely.
  • Prefer rename over mv for 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:

Terminal
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 .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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