Renaming your files is one of the most regular tasks that you might need to perform daily. While some users are more used to renaming their files and directories through the GUI, you might be surprised to know that there is more than one way to do so in Linux.
In this tutorial, I will show you some of the ways you can rename your files for easy access to them. Please remember that all the commands used in this guide will be demonstrated on Ubuntu 22.04.
Method 1 – the “mv” command
Short for move, the “mv” command is the most commonly used command to move and rename files through the terminal. The syntax for the command is as follows:
$mv [OPTIONS] source destination
In the source section you can specify more than one file/directory, and for the destination segment you can specify one single file/directory. Please note that:
- When you specify multiple files as source, then you have to specify a single directory as the destination. It will move all of the source files to the destination directory.
- To rename your file, you need to specify your single file as the source and then again, a single file as the destination. Let’s demo how that works:
$mv test.txt sample.txt
By running this command, we are changing the name from “test” to “sample”. You can run the “ls” command to see the change take effect.
Method 2 – Using the “mv” command in combination with other commands
You can use the “mv” command to rename files, one at a time. That said, you can combine it along with other utilities and commands to rename multiple files. Let’s consider the following bash code snippet:
for f in *.jpeg; do
mv — “$f” “${f%.jpeg}.jpg”
done
In this code, we are creating a for loop to iterate through all the files in a directory. Then “mv” applies to all of the files within the directory and rename them from a “.jpeg” extension to a “.jpg” extension. Finally, with the “done”, we are closing out the loop.
We can also use it along with the find command to achieve the same objective. You need multiple options with the find command, but here is how you will do it:
$find . -depth -name “*.jpeg” -exec sh -c ‘f=”{}”; mv — “$f” “${f%.jpeg}.jpg”‘ \;
The find command is passing all the files ending with a “.jpeg” extension in the current directory to the “mv” command by using the “-exec” option and replacing it to the “.jpg” extension.
With these examples, you will see that combining the “mv” command with others is not an easy undertaking and you need to have extensive knowledge of bash scripting.
Method 3 – Using the “rename” utility
The rename command is very versatile and you can use it in a lot of scenarios to rename single or multiple files at a time. This utility is not available out of the box in many Linux distributions, like Ubuntu. Let’s see how can you install it in Ubuntu 22.04:
$sudo apt install rename -y
You can also use this this to install rename in Debian as well.
For Arch Linux, you can use:
$sudo pacman -S rename
For Fedora and CentOS this works:
$sudo yum install prename
After the installation, you need to understand the syntax of the command:
$rename [options] ‘s/[original source file name]/[replacement file name]/’ [filename element]
Example – 1
We can use the rename command to replace parts of the filename as well. For all the test text files, let’s replace test with sample.
$rename -v ‘s/test/sample/’ *.txt
Example – 2
For this example, let’s update the file names and replace lower-case characters in the file names with upper case characters. You can run:
$rename -v ‘y/a-z/A-Z/’ *.txt
Similarly, you can reverse the order and convert upper case characters in the file names to lower case characters.
$rename -v ‘y/A-Z/a-z/’ *.TXT
Rename help
The rename command is super capable of handling complex scenarios. You can learn more about the syntax of the rename command by running:
$rename
And if you want to learn about these options and their details you can pull up the help page:
$rename -help
Conclusion
In this guide we saw multiple ways you can rename files within Linux. There are various other tools and utilities as well, that can help achieve this as well, such as the mmv command. Also, there are GUI tools available as well for users who find the terminal too intimidating.
If you run into any issues throughout the whole guide, feel free to engage us through the comments section below and we’ll walk you through the issue.
For more how-tos and tutorials, please visit Linux Genie.



