How to Rename A Directory in Linux? (Step By Step Guide)

Wondering how to rename directory on Linux? If your answer is yes, then I am here to help you out. Renaming directories on Linux is one of the basic operations that we perform pretty frequently.

Although, you can always use the desktop UI to rename directories and folders on Linux. But if you want to be a keyboard ninja, then knowing about the Linux commands is pretty helpful.

However, there are quite a lot of newbies who are yet getting started with Linux and learning the Linux terminal may not know how rename directory Linux works.

But there is nothing to worry about. As in this article, I will be walking you through all the steps that you have to follow to rename a folder in Ubuntu or other Linux Distros.

How To Rename a Directory on Linux?

For this tutorial, I have created a folder named DeviceMaze, and under it, I have three directories, which are the dir1, dir2, and dir3. Now my goal is to rename the dir1 directory to dir4.

Also, you should know that renaming directories on Linux cannot be done with a dedicated renaming command. But it uses a specific command called “mv”. Also, this command serves multiple purposes.

With the help of “mv” command, you can move files from one place to another as well as rename the directory on Linux.

Anyway, to get started with the command, here are the steps that you have to follow:

To rename a directory on Ubunutu, you have to use the “mv” command and then specify the directory to be renamed along with the destination of that directory only.

For this, use the following command:

$ mv <source_directory> <target_directory>

Over here, the source_directory and target_directory both refer to the location where the directory to be renamed is located.

After adding “mv” in the Linux terminal, you have to specify the source directory location+ of the old directory name.

Then you have to enter the location of the directory again and add the new directory name.

So in my case, the command will be:

rename directory linux console
$ mv /home/nayan/devicemaze/dir1 /home/nayan/devicemaze/dir4

Also, you should know that the “mv” command will not delete any content stored in that specific directory. So there will not be any data loss while renaming your directories on Linux.

How to use find to rename directory Linux?

In some cases, you may know where the directory is located. Hence, using the “mv” command won’t be any help.

But luckily, there is the “find” command that helps you to find locate directories on a Linux computer. To find directories on Linux, you have to use the “find” command and “type” option in order to look for directories.

After that, you can rename a directory on Linux by executing “mv” command with “-execdir” option.

All of these sounds confusing? Let me just simply it a bit more to you over here:

So I am going to find the directory named dir4 and replace it with dir1. So for this, I have to find the dir4 folder at first. And to do so, I will be running the below command:

$ find . -depth -type d -name "dir4"

Over here, you have to replace your directory name with dir4 and run the command. Once you have found the directory, run the second command to rename it.

$ find . -depth -type d -name dir4 -execdir mv {} dir1 \;
rename folder ubuntu

In this command, you will need to replace “dir4” to your folder name and “dir1” to the folder name that you want to keep. Pretty simple right?

Final Words:

So that was a quick guide on how to rename directory Linux. Now go ahead and check the above commands out and see if they are working for you or not.

Also, if you have any questions to ask, then do feel free to drop a comment below, and I will surely help you out.

Related Posts:

Leave a Comment