How to Rename Directories in Linux?

In Linux, the directories are the folders that contain the sub-directories or the user files. Like other operating systems, the directories can be renamed, moved, deleted, copied, etc., in Linux.

This post will explain some of the most used methods to rename directories in Linux and the sections:

Let’s start the process!

How to Rename the Directories Using a Terminal in Linux?

Before proceeding towards the explanation of the terminal methods, first, make some directories using the command:

$ mkdir directory1 directory2 directory3 directory4

After creating the new directories, display all the content using the ls command:

$ ls

The directories are created, and in the upcoming sections these directories will be used to practice the renaming methods in Linux.

Using the find Command

The “find” command Utility is used to find the files and directories, but it can also be used to rename the directories; for example, we will rename the directory1 using the find command:

$ find . -depth -type d -name directory1 -execdir mv {} directoryA \;

In the above command, the find command is complex, so the explanation of the options are explained as:

  • find: It is used to find the directory
  • Full stop: It is used to mention that the command should execute in the current directory
  • depth: It is used to check all the directories
  • type: It will choose the type of the files
  • d: The type is directory defined which is supposed to be found
  • execdir: It is used used to perform the specified command on the research directory if found
  • mv {}: It is used to move the directory

To confirm this, list down the directories:

$ ls

The “directory1” has been renamed with the “directoryA” successfully.

Using the mv Command

Second method to rename the directories is by using the mv command utility; for example, we will rename the “directory2” with the “directoryB” using the mv command:

$ mv directory2 directoryB

To confirm the changes, list down the contents using the “ls” command:

$ ls

Using the rename Command

The Last method to rename the directories in Linux is by using the rename command. The general syntax of the rename command is:

$ rename 's/Directory1/Directory_name/' *

Then explanation of the above general syntax is:

  • The rename command is used
  • “s” is used to tell that we are replacing the old name with some new name
  • The “Directory1” should be replaced with the directory name which is supposed to be change
  • The “Directory_name” should be replaced with the new directory name
  • “*” is used to find the name in the Home directory which matches the old directory name

For the better understanding of the usage of the rename command to rename the directory name, we will rename the “MyDirectory” with the “MyDirectory_newName” using the command:

$ rename 's/MyDirectory/MyDirectory_newName/' *

List down the contents to confirm the changes:

$ ls

The rename command can also be used to rename the multiple directories. For example, we will change all the directories having the “Directory” word with the “Folder” using the command:

$ rename -v 's/Directory/Folder/' *

Method 2: How to Rename Directories in Linux Using the GUI?

This is the last method which is much more convenient as this is the graphical user interface method. Right-click on the directory you want to rename and choose “Rename” from the drop-down menu:

Then type the new name and click on the “Rename” button:

The directory has been renamed:

These are all the possibilities to rename directories in Linux.

Conclusion

The directories in Linux can be renamed using the terminal and the GUI. To rename directories using the terminal, use the “mv”, “find”, and “mv” commands. For GUI, right-click on the directory and then type the new name. This post has demonstrated the possible methods to rename directories in Linux.