Renaming files and directories is one of the most basic tasks you often need to perform on a Linux system.
Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux. You can rename files using a GUI file manager or via the command-line terminal.
In this tutorial, we will show you how to use the mv
and rename
commands to rename files and directories.
Renaming files with mv Command
The mv
command (short from move) is used to rename or move files from one location to another. The syntax for the mv
command is as follows:
mv [OPTIONS] source destination
The source
can be one or more files or directories and destination
can be a single file or directory.
- If you specify multiple files as
source
, thedestination
must be a directory. In this case, thesource
files are moved to the target directory. - If you specify a single file as
source
, and thedestination
target is an existing directory then the file is moved to the specified directory. - To rename a file you need to specify a single file as
source
, and single file asdestination
target.
For example, to rename the file file1.txt
as file2.txt
you would run:
mv file1.txt file2.txt
Renaming multiple files with mv Command
The mv
command can rename only one file at a time but it can be used in conjunction with other commands such as find
or inside bash for or while loops to rename multiple files.
The following example shows how to use the Bash for loop to rename all .html
files in the current directory by changing the .html
extension to .php
.
for f in *.html; do
mv -- "$f" "${f%.html}.php"
done
Let’s analyze the code line by line:
- The first line creates a for loop and iterates through a list of all files edging 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${file%.html}
is using the shell parameter expansion to remove the.html
part from the filename. - done indicates the end of the loop segment.
We can also use the mv
command in combination with find
to achieve the same as above.
find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' ;
The find command is passing all files ending with .html
in the current directory to the mv
command one by one using the -exec
switch. 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 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 two versions of the rename
command with different syntax. In this tutorial, we will be using the perl version of the rename
command. If you don’t have this version installed on your system, you can easily install it using the package manager of your distribution.
- 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 syntax for the rename
command is as follows:
rename [OPTIONS] perlexpr files
The rename
command will rename all files
according to the specified perlexpr
regular expression. You can read more about perl regular expressions here.
For example, the following command will change all files with the extension .html
to .php
:
rename 's/.html/.php/' *.html
You can use the -n
argument to print names of files to be renamed, without renaming them.
rename -n 's/.html/.php/' *.html
The output will look something like this:
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)
By default, the rename command will not overwrite existing files. Pass the -f
argument to allow existing files to be over-written.
rename -f 's/.html/.php/' *.html
Below are a few more common examples of how to use 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
By now you should have a good understanding of how to use the mv
and rename
commands to rename files. Of course, there are other commands to rename files in Linux such as mmv
. New Linux users who are intimidated by the command line can use GUI batch rename tools such as the Métamorphose.
If you have any questions or feedback, feel free to leave a comment.