In this tutorial, we’re going to show you how to rename a directory in Linux.
It’s another in our simple, beginner-friendly tutorials to get you accustomed to the Linux CLI.
You’ll often need to rename a directory/folder – it’s one of the most frequent tasks I do on all of my Linux servers and PCs.
If you’re using a GUI/desktop, you just need to Right-click on the directory and Rename it. This tutorial will focus on renaming a directory via the Linux command-line interface (CLI), or Terminal.
How to rename a directory on Linux using the mv command
We’ll be using the mv
command. We already learned about the mv command in our how to Move a File tutorial. The basic syntax of the command is:
mv [options] [sourceDirectory] [destinationDirectory]
For example, if we have a directory called “Audio”, if we wanted to rename it to “Music”, we’d run this command:
mv Audio Music
You have to be in the directory where the directory you want to rename is located. If your current location (pwd) isn’t there, you need to CD to that directory or use the exact location of the directory, like:
mv /opt/Audio /opt/Music
This will rename the Audio directory that’s located in the /opt/ directory, and you can run that command from anywhere. The command will execute and won’t give any output if it’s successful.
To get a list of all possible options and more help about the mv command, use this command:
mv --help
How to rename multiple directories on Linux using the rename command
You can also use the rename
command to rename a directory. In most cases, it won’t be pre-installed on your system.
To install it on Ubuntu, run the following command:
apt install rename
To install it on CentOS/Fedora, run the following command:
yum install prename
You can learn more about the command and its option by running:
rename -h
We’ll always be using -v option so we get an output of what actions were performed with the command.
An example is:
rename -v 's/Audio/Music/' *
The command should give you an output similar to:
Audio1 renamed as Music1 Audio2 renamed as Music2 Audio3 renamed as Music3
This will rename all directories that have the keyword “Audio” in them, to “Music”. So if you had the directories Audio1, Audio2, Audio3, they will be renamed to Music1, Music2, Music3.
This command also works for renaming a single directory.
The rename command has a lot more options and use cases. We’ll explore some of them below.
If you want to rename certain characters in directories, you can use this command:
rename -v 'y/abcd/wxyz/' *
This will replace all instances of ‘a’, ‘b’, ‘c’, and ‘d’ to ‘w’, ‘x’, ‘y’, and ‘z’ respectively.
So if you had directories named “distribution” and “analog”, they will be renamed to “zistrixution” and “wnwlog”. So, in this case, the command above will give you an output similar to:
analog renamed as wnwlog distribution renamed as zistrixution
Another very useful example of this command is to replace all empty spaces in directories with an underscore.
Run the following command:
rename -v 'y/ /_/' *
to replace all empty spaces with an _.
You can also convert all lowercase letters to uppercase using this command:
rename -v 'y/a-z/A-Z/'
How to find and rename a directory on Linux
If you’re not sure where a certain directory is located and you want to rename it, you can combine the find command with the mv command.
For example, this command:
find . -depth -type d -name Products2021 -execdir mv {} Products2022 \;
Will find the “Products2021” directory and rename it to “Products2022”.
There are a lot more examples for the mv, rename, and find commands. Please share your most common usage and examples in the comments below.