Renaming files in Linux is a very common operation.
In this article, we will discuss how to rename single or multiple files using the mv and rename commands in various Linux distributions.
Table of Contents
Renaming Files With the mv Command
The mv (move) command moves files from one location to another. You can use this command to move files to another directory, change a file name and leave it in the same directory, or do both.
The syntax for the mv command is as follows:
mv [OPTIONS] source destination
In the above command, the source can be one or multiple files/directories, whereas the destination is a single file or directory.
The number of file/directories as source and destination determines the function of the command. For example:
- multiple source files / single directory destination: if you specify multiple files as source, then the destination should be only one directory, and the source files are moved to the destination directory.
- single source file / single existing destination: if you specify one file as the source, and an existing directory as the destination, then the file will move to the directory.
- single source / single destination: to rename, specify a source file and a destination file.
Renaming a Single File Using the mv Command
To rename a file from file1 to file2:
mv file1 file2
In our example, file1 is the source and file2 is the destination.
Renaming Multiple Files Using the mv Command
The mv file renames only one file at a time. To rename more than one file, use the mv command in conjunction with other commands like find or inside bash for or while loops.
Rename Multiple Files Using the for Loop
Say we want to rename the extensions of all .txt files in the current directory, to .html::
for f in *.txt; do mv -- "$f" "${f%.txt}.html" done
Here’s how the code is working:
- For loop is used to iterate through the list of all files with a .txt extension.
- The code in the second line runs for every item in the list and moves it to a new place replacing .txt with .html. The ${file%.txt} part is using the shell parameter expansion to remove the .txt part of the filename.
- Done in the third line indicates the end of the for loop.
Rename Multiple Files Using the find Command
An alternative way of renaming multiple files is by using the find command:
find . -depth -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.html"' \;
Here is how the code above is working:
- The find command is passing all files ending with .html (in the current directory) to mv one-by-one with the -exec option.
- The string {} in the second part includes the name of the file being processed right now.
Renaming Files With the rename Command
The rename command is an efficient way to rename multiple files in Linux. It’s an alternative to the mv command, and requires some knowledge of the regular expressions.
We recommend getting familiar with regular expressions, if you’re not already. This interactive tutorial at RegexOne should be a quick and painless way to get a solid grasp of the basics.
There are two versions of the rename command available with different syntax and features. In this article, we will be using the Perl version of the rename command. If it’s not installed on your computer, then we will elaborate on how to use the package manager of our Linux distro to install it.
Install Rename on Your Linux Distro
Install The rename Command on Ubuntu and Debian
sudo apt install rename
Install The rename Command on CentOS and Fedora
First we’ll need to install and enable the EPEL repository, if it’s not already:
sudo yum install epel-release -y
Next we can install rename
:
sudo yum install rename
Install The rename Command on Arch Linux
sudo pacman -S perl-rename
Rename Command Syntax
The syntax to use the rename command is:
rename [OPTIONS] s/perlregexp/replacement/[flags] some_files
[OPTIONS] – We’ll cover a few options below, such as -v
(verbose), -n
(no-act/dry run), -f
(overwrite existing files)
s/perlregexp/replacement/[flags] – This is the first argument that we pass to the rename command, and it’s the rule by which the filenames will be replaced. It’s made up of 4 parts, separated by a forward slash (/
). Another way of looking at it is s/<old>/<new>/[flags]
:
s/
stands for substitute, which performs a search-and-replace operation against a string we provide (in our case it’s a filename or multiple filenames).Regex has a few flavors, and the one we’re using is the Perl flavor. In Perl, thats/
is a substitution operator.- perlregexp is the regular expression that matches the strings that we want substituted. For example, if we want to rename some files with the .html extension, here is where we’d have the regular expression to match .html.
- The replacement of the string/regular expression matched at point 2 (perlregexp).
[flags]
are modifiers used in regular expressions. These are often left blank, since they’re not always necessary, however a good example of such a modifier would be/i
(case insensitive), which enables you to perform search-and-replace indifferent of the case of the match.
In the following example, we will change the extensions of all .html files to .php, in the current directory:
rename -n 's/.html/.php/' *.html
Rename Command Options
Rename command in Linux comes with multiple options including:
-v (verbose)
The verbose option prints names of the successfully renamed files. To run the rename command with the verbose option:
rename -f 's/.html/.php/' *.html
file1.html renamed as file1.php file2.html renamed as file2.php file3.html renamed as file3.php file4.html renamed as file4.php file5.html renamed as file5.php file6.html renamed as file6.php file7.html renamed as file7.php file8.html renamed as file8.php file9.html renamed as file9.php
-n (no-act)
The no-act option shows that files are to be renamed without renaming them. It’s a dry run.
rename -n 's/.html/.php/' *.html
file1.html renamed as file1.php file2.html renamed as file2.php file3.html renamed as file3.php file4.html renamed as file4.php file5.html renamed as file5.php file6.html renamed as file6.php file7.html renamed as file7.php file8.html renamed as file8.php file9.html renamed as file9.php
-f (Force)
The rename command doesn’t overwrite existing files by default. This command shows no output as the files are renamed automatically.
rename -f 's/.html/.php/' *.html
Conclusion
We covered the basics of renaming files in Linux using two methods, the mv command and the rename command.
If you have any questions or issues, feel free to leave us a comment or contact us and we’ll get back to you as soon as we can.