If you have been dealing with Linux operating systems, you already know that it is very important to keep your file system structured. There might be occasions when you had to create several directories and then you realize that you need to rename a directory in linux or rename multiple directories in linux.

Renaming a directory in linux is not something which is done via any dedicated command, instead, you will have to find a workaround. mv command is your best friend to move the files as well as to rename a directory in linux.

In this tutorial we will learn about how to rename directories in linux.

Rename Directories using mv command

To rename a directory in linux, you can use the mv command with the specific directory and the new name for that directory. The command syntax looks like this

mv <source directory name> <new name>

Now, let’s say you wish to rename an existing directory called “mydir” to “importantdir”, assuming both the existing and new directory reside at same location, you can issue the following command:

%  mv mydir importantdir

If you do the listing in your home directory, you will see that your directory has been renamed.

%  mv mydir myimportantdir
% ls -lrt 
total 0
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 14 00:18 myimportantdir

OpenSSL Top 40 Commands

How to use OpenSSL Commands – Top 40 OpenSSL commands with examples

Rename directory using the find command

You can find and rename the directories by using find command.

Assume that you have a directory somewhere in the filesystem which you want to find and rename.

% ll                                                                               
total 4.0K
drwxr-xr-x. 12 itinfotech itinfotech 4.0K Jul 15 12:39 newimportantdir

 % find . -depth -type d -name "newimportantdir" -exec mv {} renamedit  \;  
       
 % ll                                                                               
total 4.0K
drwxr-xr-x. 12 itinfotech itinfotech 4.0K Jul 15 12:39 renamedit
 %                                                                

In the example above, find command will find a directory named as newimportantdir and rename it with the new supplied name renamedit. Option -type means the type of item to be found is directory, -depth option finds the item at depth 1 which means in the current location only.

Renaming multiple directories in Linux using bash

There could be times when you have a task to rename multiple directories all at once. You can find and rename multiple directories in linux using a bash script such as below.

Create a bash script by any name whichever you want to call it. I am calling it renamedir.sh

#!/bin/bash

directory="/tmp/test"  # Directory path

# Loop through each directory in the specified directory
for dir in "$directory"/*/; do
    # Extract the original directory name
    original_name=$(basename "$dir")

    # Generate the new directory name
    new_name="${original_name}_renamed"

    # Rename the directory
    mv $original_name $new_name
done

Above bash script will find and rename all the directories at given Source Path. You can also tweak the script to take an input from the user so the hardcoding of the source directory can be avoided.

We have following test folders created in a directory which will be renamed by using our script.

itinfotech@calabar /t/test> ll                                                             /tmp/test
total 0
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder1
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder10
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder11
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder12
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder13
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder14
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder15
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder16
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder17
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder18
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder19
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder2
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder20
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder3
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder4
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder5
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder6
drwxr-xr-x. 2 itinfotech itinfotech 6 Jul 15 17:01 folder7
#!/bin/bash

After executing the script:

itinfotech@calabar /t/test> ./renamedir.sh                                                    /tmp/test
itinfotech@calabar /t/test> ll                                                             /tmp/test
total 4.0K
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder10_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder11_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder12_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder13_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder14_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder15_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder16_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder17_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder18_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder19_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder1_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder20_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder2_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder3_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder4_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder5_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder6_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder7_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder8_renamed
drwxr-xr-x. 2 itinfotech itinfotech   6 Jul 15 17:01 folder9_renamed

And Voila! You have all the directories renamed by using a bash script. Above script is not really a final blueprint of the only way, through which you can rename the directories using bash. This script can be tailored based on your needs such as asking for user to enter the source location, validating the folders before renaming it, displaying the message while directories are being renamed etc. You can check Bash Script manual to unleash the power of bash.

Conclusion

Renaming directories in Linux is a common task that allows users to organize and manage their file system effectively. Whether you want to update the name of a directory for clarity, consistency, or any other reason. You can check other utilities such as tar to make sure that there is enough space to handle the activities happening in your server. Weather you wish to rename a directory in linux or rename multiple directories using bash script in linux, make sure to use the best practices against any sort of accidental data loss.