How to Rename Files in Linux Using mv & rename Commands

Renaming files is one of the many regular tasks in any operating system. Linux provides command-line tools and expressions to help you perform this task efficiently. You can employ the mv
command, a built-in tool that ships with Linux systems, or leverage the rename
utility. This is an advanced command that uses Perl expressions for batch file renaming.
Let’s check out how to use these CLI tools to rename files.
#How to rename files using the mv command
The mv
command is shorthand for "move". It comes included by default in all Linux distributions and serves two purposes: moving and renaming files. Our focus will be on renaming files.
#Syntax
The mv
command takes the following syntax:
mv oldfilename newfilename
Where:
oldfilename
is the name of the current file to be renamed
newfilename
is the new name that the current file will adopt.
Let’s now take a look at the mv
command in action.
#Renaming a single file
To rename a file, provide the original file name followed by the new one. For instance, to rename
sample_1.txt
to sample_2.txt
, run:
mv sample_1.txt sample_2.txt
If the file to be renamed is located in a separate directory, ensure that its path matches that of the new file name. In this example, we are renaming file1.txt
, which is located in the ~/data/reports
directory, to file2.txt
.
#Renaming multiple files
The mv
command is only designed to rename one file. You can, however, incorporate a for-loop inside a shell script to modify file extensions from one extension type to another.
To illustrate this, create a new shell script using your preferred command-line text editor.
nano myscript.sh
Then use the for loop script as shown. In this example, the loop examines all the files with the .php
extension and modifies the extension to .html
.
#!/bin/bash
for i in *.php; do
mv -- "$i" "${i%.php}.html"
done
Let’s break down this shell script:
- The first line,
#!/bin/bash
is the shebang header - The
for i in *.php; do
line iterates through all files with a.php
file extension. - The
mv -- "$i" "${i%.php}.html
line replaces the.php
extension with the.html
extension for all PHP files. - The
done
line simply marks the end of the shell script.
Exit the file.
Next, make the shell script executable by assigning the execute permission.
sudo chmod +x /path/to/file/script.sh
To run the script, run the following command. Ensure the path to the script is correct.
./script.sh
From the output, all the .php
files have been renamed to .html
Linux Dedicated Servers with Full Control
Optimize your workloads with customizable Linux bare metal servers, offering high-performance processors, reliable storage, and full root access.
#How to rename files on Linux using the rename
command
The rename
command is an advanced command used for batch file renaming. The command uses Perl expressions, which provide a versatile way of renaming multiple files and directories. It performs various operations, including altering file extensions from one extension type to another, modifying character cases ( from lowercase to uppercase and vice versa ), and deleting portions of the file names.
#Installing the rename Command
By default, the rename
command is not installed in Linux. Thankfully, you can install it using your distribution’s package manager as follows:
On Ubuntu / Debian
sudo apt install rename -y
On Rocky Linux / RHEL / Fedora
sudo dnf install rename -y
On Arch Linux / Manjaro
sudo pacman -S rename -y
#Rename command examples
This section looks at various ways of running the rename
command to rename files. The command uses the Perl substitute and translate expressions to modify file names.
#Syntax
The rename command takes the following syntax:
rename [options] 's/[pattern]/[replacement]/' [filename]
Where:
options
- refers to optional command flags that alter the execution of a command.
s
- Is a substitute expression that replaces a string(s)
[pattern]
- Portion of the file name to be replaced
[replacement]
- The string that replaced the specified portion of the current filename
[filename]
- The name of the file
In the syntax provided, the rename command uses the substitute expression denoted by the s
character to replace the string specified by the [pattern]
part with the [replacement]
portion.
#Convert a file extension
The following example illustrates how to change or convert files with the .conf
file extension to .txt
. For illustration, we have three files with the .conf
extension.
rename 's/\.conf$/.txt/' *.conf
After running the command, the file extensions have been converted from .conf
to .txt
.
#Rename part of a file name
The rename
command also lets you rename part of the file name. For example, the command shown replaces example
with sample
in all files with the .txt
extension.
rename -v 's/example/sample/' *.txt
Here, the s
option replaces the string example
with sample
. The -v
option stands for verbose and prints out the operation being carried out.
#Delete a portion of a file name
You can delete part of a file name by omitting the replacement portion of the syntax. The command below renames the files administrator1.txt
and administrator2.txt
to admin1.txt
and admin2.txt
respectively. The istrator
portion is deleted by omitting the replacement part of the command syntax.
rename -v 's/istrator//' *.txt
#Transform lowercase to uppercase characters
The translate
Perl expression lets you transform a string of characters, character by character, using the y
notation. This takes the following syntax:
For example, the following command converts characters of all text file names from lowercase to uppercase.
rename -v 'y/a-z/A-Z/' *.txt
#Transform uppercase to lowercase characters
Conversely, you can convert uppercase characters to lowercase by reversing the order from y/a-z/A-Z/
to y/A-Z/a-z/
.
The following command converts all uppercase characters of a file name to lowercase. Note that the file extension is also capitalized.
rename -v 'y/A-Z/a-z/' *.TXT
#Conclusion
In this tutorial, we have explored different ways of renaming files on a Linux system. We started with the mv command which is primarily used for basic file renaming. We later explored the rename command, an advanced tool for renaming multiple files using substitute and translate Perl expressions.
Unlike the mv
command, which performs basic file renaming operations, the rename
command employs advanced techniques for renaming file names, including converting file extensions, renaming and deleting sections of file names, and converting uppercase characters to lowercase and vice versa. Between the two CLI tools, you’re more likely to use the mv command more often, unless when performing advanced batch renaming of files.
Starting at just $3.24 / month, get virtual servers with top-tier performance.