How to Rename Files in Linux

November 20, 2023

Introduction

Linux provides several options for renaming files using the GUI or multiple dedicated terminal commands. Renaming individual files is straightforward, but it can be challenging to rename multiple files simultaneously.

In this tutorial, we will go over different commands you can use in the Linux terminal to rename files in Linux.

How to rename files in Linux

Prerequisites

Rename Files with the mv Command

The Linux mv (move) command moves or renames files and directories through the terminal. The command's effects depend on the provided destination:

  • If you specify a directory as the destination when using the mv command, the source file moves to that directory.
  • If the destination is another file name, the mv command renames the source file to that name instead.

Therefore, to rename files with the mv command, provide a new file name and, optionally, a destination directory.

Note: Learn more about using the mv command in our guide to moving directories in Linux.

mv Command Syntax and Options

The mv command uses the following syntax:

mv [options] [source] [destination]

The command has several options to control overwrite and display behavior. Some options are in the table below:

OptionDescription
-f
--force
Overwrites without prompting.
-i
--interactive
Prompts before overwriting.
-n
--no-clobber
Avoids overwriting an existing file.
-v
--verbose
Shows a verbose output.

Use the man command to view all the available options.

mv Command Examples

The mv command is simple to use for file renaming. Combining it with other commands and with Bash scripts enables advanced renaming operations.

Below are several examples showcasing how to use mv to rename files.

1. Rename a File

The mv command with its default syntax allows you to rename a single file. For example, to rename example1.txt into example2.txt, use:

mv example1.txt example2.pdf

The command does not show an output. Use -v option with the command or the ls command to check the name change before and after executing the mv command:

ls -l
Renaming a single file using the mv command

Use this method to change a single file's extension or to rename a file.

2. Move and Rename a File

Provide the destination path and new file name to move and rename a file. For example:

mv dir1/example1.txt dir2/example2.txt

The command moves the file from dir1 to dir2 and renames it from example1.txt to example2.txt.

3. Prompt on Overwrite

To avoid accidental overwrites, add the -i option to enter interactive mode:

mv -i example1 example2
mv -i terminal output overwrite

If the file exists, the command prompts asking whether to overwrite the file. Use this mode to avoid losing a file when renaming.

4. Rename Multiple Files Command

Use the find command to select multiple files with a similar name, and then use the mv command to rename them.

Warning: Before making bulk modifications to files, create backup copies of critical files to preserve original names. Renaming files is irreversible.

For example:

find . -depth -name "[current pattern]" -exec sh -c 'f="{}"; mv -- "$f" "${f%[current pattern]}[new pattern]"' \;

The find command searches through nested directories and defines the [current pattern] as the search parameter. Next, the -exec option executes the mv command on files matching the pattern. Lastly, the changes apply according to the [new pattern].

For instance, to change the .txt extension to .pdf for multiple files, use the following command:

find . -depth -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.pdf"' \;
Renaming multiple files using the find and mv commands

The command changes the file extension from .txt to .pdf for all files in the current directory. Use this command to perform complex batch renaming

5. Rename Multiple Files Bash Script

Instead of the find command, you can use the mv command as a part of a Bash for loop to rename multiple files.

Using the same example as in the section above, do the following:

1. Create and open a Bash script file via a text editor such as nano:

nano rename_files.sh

Note: Learn more about using bash scripts to manage files and directories in Linux.

2. Add the following lines to the script:

#!/bin/bash
for f in *.txt; do
    mv -- "$f" "${f%.txt}.pdf"
done

Each line does the following:

  • The first line is the shebang, indicating it's a Bash script.
  • The second line begins a for loop to iterate through files in the current directory ending with .txt.
  • The third line uses the mv command on each file found to replace the .txt extension with .pdf.
  • The last line ends the loop segment.

3. Press Ctrl+X, type Y, and press Enter to save the changes to the script and exit.

4. Use the sh command to execute the script:

sh rename_files.sh
Renaming multiple files using a bash script

Note: Learn how to compare two files using the diff command.

Rename Files with the rename Command

The rename command is used to rename multiple files or directories in Linux. It offers more features than the mv command but can be challenging since it requires basic knowledge of Perl expressions.

How to Install the rename Command

On many Linux distributions, the rename command is not available by default. If your system does not have the rename command, use one of the commands below (depending on the distribution):

  • For Ubuntu and Debian, use:
sudo apt install rename
  • For CentOS and Fedora, use:
sudo yum install prename
  • For Arch Linux, use:
sudo pacman -S rename

rename Command Syntax and Options

Perl regular expressions have three working modes: match, substitute, and translate. The rename command uses substitute and translate expressions to change file and directory names.

Substitute expressions replace a part of the file name with a different string. They use the following syntax:

rename [options] 's/[pattern]/[replacement]/' [file name]

With this syntax, the command renames the file by replacing the first occurrence of the pattern with the replacement. In the command above:

  • rename. Invokes the rename command.
  • [options]. Provides an optional argument that changes the way the command executes.
  • s. Indicates a substitute expression.
  • [pattern]. Specifies the part of the file name you want to replace.
  • [replacement]. Specifies a replacement for the part of the current filename.
  • [file name]. Defines the file you want to rename.

A translate expression translates one string of characters into another, character for character. This type of expression uses the following syntax:

rename [options] 'y/[string 1]/[string 2]/' [filename]

An example of a rename command using a translate expression:

rename 'y/abc/xyz/'

In this example, every "a" character in the filename is replaced by an "x", every "b" by a "y", and every "c" by a "z".

The rename command uses the following options:

  • -a. Replaces all the file name element occurrences instead of just the first one.
  • -f. Forces an overwrite of existing files.
  • -h. Displays the help text.
  • -i. Displays a prompt before overwriting existing files.
  • -l. Replaces the last occurrence of the filename element instead of the first one.
  • -n. Performs a dry run, making no permanent changes. Best combined with the verbose output (-v).
  • -s. Renames the target instead of the symlink.
  • -v. Shows a verbose version of the output.
  • -V. Displays the command version.

rename Command Examples

1. Change File Extension

Using our last example, change the file extension from .txt to .pdf with:

rename -v 's/.txt/.pdf/' *.txt
Using the rename command to replace the file extension

2. Replacing a Part of a Filename

Replacing a different part of the filename follows the same syntax as the example above. To rename example1.txt, example2.txt, and example3.txt to test1.txt, test2.txt, and text3.txt, use:

rename -v 's/example/test/' *.txt
Renaming multiple files using the rename command

3. Delete a Part of a Filename

The rename option also allows you to delete a part of the filename by omitting the replacement part of the expression. For instance, if we want to shorten example into ex:

rename -v 's/ample//' *.txt
Removing a part of the file name using the rename command

4. Rename Files with Similar Names

Another use for the rename option is to rename files with similar names. For instance, if we want to rename files with example and sample in their name to test, use this command:

rename -v 's/(ex|s)ample/test/' *.txt
Renaming multiple files with similar names using the rename command

5. Rename Files Character-by-Character

The rename command also allows you to use translate expressions to rename files character-by-character. For instance, if you want to rename multiple files named example file by replacing the blank space with an underscore (_), use:

rename -v 'y/ /\_/' *.txt
Removing blank spaces from file names using the rename command

6. Convert Lowercase Characters

To convert lowercase characters in filenames into uppercase characters, use:

rename -v 'y/a-z/A-Z/' *.txt
Converting file names from lowercase to uppercase using the rename command

7. Convert Uppercase Characters

The reverse also works if we switch the order of the uppercase and lowercase characters in the expression:

rename -v 'y/A-Z/a-z/' *.TXT
Converting file names from uppercase to lowercase using the rename command

Note: Be careful when changing the character case, as this also changes the file extension.

Rename Files with GUI

The GUI is a convenient way to rename one or multiple files. To rename one or more files through the GUI, do the following:

1. Open the Files menu and navigate to the correct location.

Files location example files

2. Select a file or all the files to be renamed.

3. Press F2 to open the renaming prompt. Alternatively, right-click and choose Rename.

4a. (One file) Enter the new file name and press Rename to confirm for a single file. The prompt does not allow renaming if the chosen file name exists in the directory.

File rename GUI Ubuntu

4b. (Multiple files) Choose whether to rename the files using a template (such as appending numbers to file names) or to find and replace text in names. The menu shows a preview before renaming.

Bulk file rename GUI Ubuntu

Click Rename to confirm renaming multiple files.

Conclusion

After reading this tutorial, you should know how to rename files using the mv and rename commands in Linux.

Learn more about using Linux commands in our Linux Commands Cheat Sheet.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Rename a Directory in Linux
May 7, 2024

Renaming directories is one of the most basic tasks for a Linux system administrator. This tutorial shows you how to rename directories using the terminal window.
Read more
How To Use shred Linux Command
November 28, 2024

Deleting a file in Linux or any other OS does not actually remove the data from the drive. The best practice is to use the shred command to permanently destroy sensitive data so that it cannot be recovered.
Read more
How to Set Environment Variables in Linux
December 5, 2024

Environment variables play a significant role in setting up the system and shell configuration. This tutorial explains how to view, set, manage, and unset environment variables in Linux.
Read more
How to Format Disk Partitions in Linux
December 2, 2020

Formatting and mounting disk partitions in Linux is common because partitions cannot be used without this process. In this tutorial you will learn how to format and mount disk partitions with ext4, FAT32, or NTFS file systems.
Read more