How to Rename Files and Directories in Linux
Updated on
•6 min read

Renaming files is one of the most basic tasks you often need to perform on a Linux system. You can rename files using a GUI file manager or via the command-line terminal.
Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux.
In this tutorial, we will show you how to use the mv and rename commands to rename files and directories.
Renaming Files with the mv Command 
The mv command
(short of 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, thedestinationmust be a directory. In this case, thesourcefiles are moved to the target directory. - If you specify a single file as 
source, and thedestinationtarget 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 a 
sourceand a single file as adestinationtarget. 
For example, to rename the file file1.txt as file2.txt you would run:
mv file1.txt file2.txtRenaming multiple files with the 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 
forloop 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 
.htmlwith.php. The part${file%.html}is using the shell parameter expansion to remove the.htmlpart from the filename. doneindicates the end of the loop segment.
Here is an example using mv 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 mv one by one using the -exec option. 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 the 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
renameon Ubuntu and Debiansudo apt install renameInstall
renameon CentOS and Fedorasudo yum install prenameInstall
renameon Arch Linuxyay 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 the files according to the specified perlexpr regular expression. You can read more about perl regular expressions here
.
The following example will change all files with the extension .html to .php:
rename 's/.html/.php/' \*.htmlYou can use the -n option to print names of files to be renamed, without renaming them.
rename -n 's/.html/.php/' \*.htmlThe 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 doesn’t overwrite existing files. Pass the -f option to allow existing files to be over-written:
rename -f 's/.html/.php/' \*.htmlBelow 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
We’ve shown you how to use the mv and rename commands to rename files.
There are also 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.


