If you’re a Linux user, you’ll frequently need to rename files. Fortunately, the process is simple, whether you prefer using a graphical file manager or the command-line interface. However, renaming several files at once can be difficult, particularly for newcomers to the Linux world.
In this tutorial, we’ll teach you how to rename files and directories using the mv
and rename
commands. Our step-by-step guide will show you how to complete this task with ease, regardless of your experience level. So, whether you’re a seasoned Linux user or just getting started, read on to learn more about renaming files in Linux!
Renaming Files with the mv Command
The mv
command, which stands for move
, is employed for the purpose of renaming or relocating files from one location to another. The syntax of the mv
command is depicted below:
mv [OPTIONS] source destination
One or multiple files or directories can serve as the source
, while the destination
can be either a singular file or a directory.
- When multiple files are selected as the
source
, thedestination
must be a directory. Consequently, the selectedsource
files will be relocated to the targeted directory. - When you provide a singular file as the
source
and thedestination
is a pre-existing directory, the file will be transferred to the designated directory. - To change the name of a file, you must indicate one file as the
source
and another file as thedestination
target.
An instance of how to rename a file named file1.txt
to file2.txt
would involve executing the following command:
mv file1.txt file2.txt
Renaming multiple files with the mv Command
The mv
command has the ability to rename individual files only. However, it can be combined with other commands, such as find
or incorporated within bash for
or while
loops to rename multiple files simultaneously.
To illustrate, consider the following example which demonstrates how to leverage the Bash for
loop to rename all .html
files in the present directory by modifying the file extension from .html
to .php
.
for f in *.html; do
mv -- "$f" "${f%.html}.php"
done
Let’s go over the code step-by-step:
- In the first line, a
for
loop is created to go through all files that have the.html
extension. - For each item in the list, the second line renames the file by replacing the
.html
extension with.php
. The expression${file%.html}
uses shell parameter expansion to remove the.html
part of the file name. - The keyword
done
marks the end of the loop segment.
Here’s an alternative example that uses the find
command with mv
to accomplish the same task:
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;
The find
command is utilizing the -exec
option to pass each file ending with .html
in the current directory to mv
individually. The placeholder {}
represents the current file name being processed.
As demonstrated in the aforementioned examples, renaming multiple files with the mv
command is a complex task that necessitates proficiency in Bash scripting.
Renaming Files with the rename Command
The rename
command allows you to rename multiple files at once, and it requires some familiarity with regular expressions. Compared to the simpler mv
command, it offers more advanced functionality.
There are two different versions of the rename
command, each with its own syntax. In this tutorial, we’ll be working with the Perl version of the command. If you don’t already have it installed on your system, you can easily install it using your distribution package manager.
- Install
rename
on Ubuntu and Debian
sudo apt install rename
- Install
rename
on CentOS and Fedora
sudo yum install prename
- Install
rename
on Arch Linux
yay perl-rename ## or yaourt -S perl-rename
The format for the rename
command can be expressed in the following way:
rename [OPTIONS] perlexpr files
By using the rename
command with a perlexpr
regular expression, you can rename files
based on specific patterns. If you need more information on perl regular expressions, you can read this resource.
For instance, the following command would replace the extension .html
with .php
for all files:
rename 's/.html/.php/' \*.html
Use the -n
option to print names of files to be renamed, without actually renaming them.
rename -n 's/.html/.php/' \*.html
The resulting display will have a similar appearance to the following:
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)
The rename
command does not overwrite existing files by default. To enable the overwriting of existing files, use the -f
option:
rename -f 's/.html/.php/' \*.html
Here are some additional typical ways to utilize the rename command:
- Replace spaces in filenames with underscores
rename 'y/ /\_/' \*
- Convert filenames to lowercase
rename 'y/A-Z/a-z/' \*
- Convert filenames to uppercase
rename 'y/a-z/A-Z/' \*
Conclusion
In our demonstration, we illustrated how to utilize the rename and mv commands to change the names of files in Linux. Alongside these commands, there exist other alternatives to rename files, including mmv. For those new to Linux who may find the command line intimidating, there are also GUI batch rename utilities available such as Métamorphose.
Please don’t hesitate to share your inquiries or feedback with us by leaving a comment.