How to Rename a Directory in Linux

8

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”:

how to rename a directory in linux How to Rename a Directory in Linux 1623664579 560 How to Rename a Directory in Linux

You can see in the home directory, two directories with the given names are created:

how to rename a directory in linux How to Rename a Directory in Linux 1623664579 570 How to Rename a Directory in Linux

Now, move the “temp” to the “temp2” directory using the “mv” command:

how to rename a directory in linux How to Rename a Directory in Linux 1623664580 230 How to Rename a Directory in Linux

how to rename a directory in linux How to Rename a Directory in Linux 1623664580 476 How to Rename a Directory in Linux

Open the “temp 2” directory to check if the “temp” directory is successfully moved:

how to rename a directory in linux How to Rename a Directory in Linux 1623664581 176 How to Rename a Directory in Linux

After moving, use the “mv” command again to rename a “temp2” directory:

how to rename a directory in linux How to Rename a Directory in Linux 1623664581 356 How to Rename a Directory in Linux

So, the temp2 directory has been renamed successfully to the “new_Dir” directory:

how to rename a directory in linux How to Rename a Directory in Linux 1623664582 976 How to Rename a Directory in Linux

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:

how to rename a directory in linux How to Rename a Directory in Linux 1623664582 716 How to Rename a Directory in Linux

Now, to display the list of files present in the “new_Dir” folder, type the “ls” command:

how to rename a directory in linux How to Rename a Directory in Linux 1623664583 573 How to Rename a Directory in Linux

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

how to rename a directory in linux How to Rename a Directory in Linux 1623664583 257 How to Rename a Directory in Linux

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:

how to rename a directory in linux How to Rename a Directory in Linux 1623664584 910 How to Rename a Directory in Linux

Use the rename command with the following expressions to change letter case:

$ rename ‘y/A-Z/a-z/’ *

how to rename a directory in linux How to Rename a Directory in Linux 1623664584 781 How to Rename a Directory in Linux

To confirm it, type “ls” again:

how to rename a directory in linux How to Rename a Directory in Linux 1623664585 884 How to Rename a Directory in Linux

Example 2:
To rename the text files present in the desktop directory as pdf files, run the command:

$ rename ‘s/.txt$/.pdf/’ *.txt

how to rename a directory in linux How to Rename a Directory in Linux 1623664585 150 How to Rename a Directory in Linux

Type the “ls” command to display the output:

how to rename a directory in linux How to Rename a Directory in Linux 1623664585 294 How to Rename a Directory in Linux

You can also rename a directory through GUI by simply right click on the desired folder and navigate to the “rename” option:

how to rename a directory in linux How to Rename a Directory in Linux 1623664586 213 How to Rename a Directory in Linux

Click on the “rename” option, type the name you want to update, and click to the “rename” button:

how to rename a directory in linux How to Rename a Directory in Linux 1623664587 880 How to Rename a Directory in Linux

And the directory name will be changed:

how to rename a directory in linux How to Rename a Directory in Linux 1623664587 680 How to Rename a Directory in Linux

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More