How to Rename Files in Linux

Introduction

Linux offers various methods for renaming files, either through the GUI or multiple specialized terminal commands. Renaming a single file is simple, but renaming multiple files at once can be more complex.

In this tutorial, we will explore different commands available in the Linux terminal for renaming files.

Prerequisites

  • A system running a Linux distribution.
  • An account with sudo privileges.
  • Access to the terminal window/command line.
  • Access to a text editor, such as Vim or nano.

Rename Files with the mv Command

The Linux mv (move) command is used to move or rename files and directories via the terminal. The command’s behavior depends on the specified destination:

  • If you specify a directory as the destination, the source file is moved to that directory.
  • If the destination is a different file name, the mv command renames the source file to that name.

Therefore, to rename files using the mv command, you need to provide the new file name and, optionally, a destination.

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.

mv Command Examples

The mv command is straightforward for renaming files. By combining it with other commands and Bash scripts, you can perform advanced renaming operations.

Here are several examples demonstrating how to use mv to rename files.

1. Rename a File

The mv command, using its default syntax, allows you to rename a single file. For instance, to rename example1.txt to example2.txt, use the following command:

mv example1.txt example2.pdf

The command does not produce any output by default. To verify the name change, you can use the -v option with the command or use the ls command to check the file names 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

Specify 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

When the file already exists, the command prompts you, asking whether to overwrite the file. Utilize this mode to prevent accidental loss of files during renaming..

4. Rename Multiple Files Command

Utilize the find command to select multiple files with similar names, and then employ the mv command to rename them.

Warning: Before executing bulk modifications to files, it’s advisable to create backup copies of critical files to retain their original names. Renaming files is an irreversible action.

For example:

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

The find command traverses through nested directories, specifying the [current pattern] as the search parameter. Subsequently, the -exec option executes the mv command on files matching the pattern. Finally, the changes are applied based on the [new pattern].

For example, to alter the extension from .txt to .pdf for multiple files, execute 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

This command alters the file extension from .txt to .pdf for all files in the current directory. Utilize this command for intricate batch renaming tasks.

5. Rename Multiple Files Bash Script

In lieu of the find command, you can employ the mv command within a Bash for loop to rename multiple files.

Following the same example as in the previous section, execute the following steps:

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

nano rename_files.sh

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 contains the shebang, indicating that it’s a Bash script.
  • The second line initiates a for loop to iterate through files in the current directory ending with .txt.
  • The third line employs the mv command on each found file, replacing the .txt extension with .pdf.
  • The final line marks the end of 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 provides more advanced features compared to the mv command, but it can be more challenging to use as it requires a basic understanding of Perl expressions.

How to Install the rename Command

In many Linux distributions, the rename command is not available by default. If your system lacks the rename command, you can install it using one of the following commands, depending on your 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 operate in three modes: match, substitute, and translate. The rename command utilizes substitute and translate expressions to modify file and directory names.

Substitute expressions replace a portion of the file name with a different string. They follow this syntax:

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

Using 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 optional arguments that modify how the command executes.
  • s: Indicates a substitute expression.
  • [pattern]: Specifies the part of the file name to be replaced.
  • [replacement]: Specifies the new string to replace the pattern in the current file name.
  • [file name]: Defines the file you want to rename.

A translate expression converts one string of characters into another, character by character. This type of expression follows this 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” in the filename is replaced with “x”, every “b” with “y”, and every “c” with “z”.

The rename command uses the following options:

  • -a: Replaces all occurrences of the file name element instead of just the first one.
  • -f: Forces an overwrite of existing files.
  • -h: Displays the help text.
  • -i: Prompts 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 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 uses the same syntax as the example above. To rename example1.txt, example2.txt, and example3.txt to test1.txt, test2.txt, and test3.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 part of the filename by omitting the replacement part of the expression. For instance, if we want to shorten example to 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 of the rename option is to rename files with similar names. For example, to rename files containing example and sample in their names to test, use the following 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, to rename multiple files named example file by replacing the blank space with an underscore (_), use the following command:

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 provides a convenient method to rename one or multiple files. To rename one or more files through the GUI, follow these steps:

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. (For one file) Enter the new file name and press “Rename” to confirm the change for a single file. The prompt prevents renaming if the chosen file name already 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

Upon completing this tutorial, you should be adept at renaming files using the mv and rename commands in Linux.

Leave a Comment