How to Rename a Directory in Linux
In Linux and Unix-like systems, we are always amazed to see several ways for a single operation. Whether to install something or to perform through the command-line, you will get multiple utilities and commands.
Even if you want to move, copy or rename a directory, it is quite handy to perform these functions with commands; you don’t need to install any specific tool.
In Linux distributions, everything is in the form of directories. So, it is good to keep all of them in a structured way. Sometimes, we need to create temporary folders to save data and later on, to keep them permanently, we have to rename those directories.
There are no traditional commands to rename a folder/directory; it can be done using several ways. We will discuss in this guide how to change the directory name using the “mv” command and “rename” command. It might shock you that this operation can be performed using the “mv” command. The “mv” command is not only used to move one directory to another; it is a multi-purpose command which helps to rename a directory as well.
So, let’s check how we can do use the “mv” command and “rename” command:
How to Rename a Folder Through “mv” Command
To rename a folder through the “mv” command is the simplest way you have ever seen.
Create a directory in the terminal named “temp”:
To move the “temp” directory, make another directory with the name “temp 2”:
You can see in the home directory, two directories with the given names are created:
Now, move the “temp” to the “temp2” directory using the “mv” command:
Open the “temp 2” directory to check if the “temp” directory is successfully moved:
After moving, use the “mv” command again to rename a “temp2” directory:
So, the temp2 directory has been renamed successfully to the “new_Dir” directory:
You can also confirm it using a terminal to active a “new_Dir” directory in it, and check if the “temp” directory (we created first and moved to temp2 folder is present in the “new_Dir” directory or not):
To activate a “new_Dir” folder in the terminal, use the “cd” command:
Now, to display the list of files present in the “new_Dir” folder, type the “ls” command:
How to Rename a Folder Through “rename” Command
The “rename” command is a built-in tool in most Linux distributions that helps to rename folders and directories. It uses regular expressions to perform functions.
If it is not present in your system. Use the following command:
$ sudo apt install rename
The syntax used for the “rename” command is:
$ rename
Consider the given examples to check if it’s working:
Example 1:
To rename the directories from lowercase to uppercase, run the ls command to display directories in the desktop directory:
Use the rename command with the following expressions to change letter case:
$ rename ‘y/A-Z/a-z/’ *
To confirm it, type “ls” again:
Example 2:
To rename the text files present in the desktop directory as pdf files, run the command:
$ rename ‘s/.txt$/.pdf/’ *.txt
Type the “ls” command to display the output:
You can also rename a directory through GUI by simply right click on the desired folder and navigate to the “rename” option:
Click on the “rename” option, type the name you want to update, and click to the “rename” button:
And the directory name will be changed:
Conclusion
In this write-up, we have seen how to rename a directory in Linux operating system. There are multiple ways to do it, but why choose a difficult way when the simplest approach is available.
We have learned from this guide to rename directories using the “mv” command and “rename” command. The “mv” command is considered a multi-tasking command tool whereas, using the “rename” command directories can be changed through regular expressions. We have also checked it through the GUI approach.