English
+370 5 205 5502 sales@monovm.com

How to rename directories in Linux

Renaming directories is one of the simple tasks that can be accomplished using the “mv” command from the command line. To learn how to rename multiple directories simultaneously, read along.

07 Oct, 21 by Susith Nonis 4 min Read

List of content you will read in this article:

Renaming directories is similar to renaming files in a Linux system. It is one of the simple and commonly performed operations on the Linux system. You can rename the directory either from the command-line commands or using the GUI file manager to do so. 

In this article, we will be focusing on various commands that will help in renaming the directory along with various options that will somehow affect the working of the rename command.

  • For renaming the directories, Linux offers a simple command “mv” that will solve the purpose of renaming the directories. The “mv” command can be used for renaming and moving the file from one source location to the destination location. 

You can follow the below-mentioned syntax for the “mv” command.

mv [OPTIONS] source destination

  • Suppose, you are trying to rename the dir_name1 to dir_name2 as shown below.

mv dir_name1 dir_name2

In case, if the dir_name2 already exists then the dir_name1 is moved to the existing dir_name2 directory.

  • For renaming the directory that is not present in your current working directory, then you need to specify the path to that directory while renaming it. You can consider the following example.

mv /home/user/dir_name1 /home/user/dir_name2

If you want to rename a single directory, you can do it effortlessly. But if you want to rename multiple directories at the same time, then you have to face some challenges if you are a new Linux user.

Well, the “mv” command will help in moving only one directory at a time, but you can use the “mv” command in conjunction with other commands that will solve the purpose of renaming multiple directories at the same time.

  • You can consider the following example, where we have used the bash “for” loop for appending the current date to all the directory names that we are considering renaming and are present within the current working directory.

for a in *; do
 if [ -d "$a" ]; then
   mv -- "$a" "${a}_$(date +%Y%m%d)"
 fi
Done

Now we will see how the above bash code will process the directories one by one to rename them. The first line in the above code will iterate through all the files. The second line will check if the specified file is a directory. Then the third line will append the current date to the end of the directory.

  • If you want a less complex solution, you can use the “find” command to do so. Consider the following example.

find . -mindepth 1 -prune -type d -exec sh -c 'd="{}"; mv -- "$d" "${d}_$(date +%Y%m%d)"' \;

The find command will pass all the directories to the “mv” command one by one with the help of the “-exec” option. The string{} will take the name of the directory currently being processed.

Rename command is considered to be more powerful than the mv command as it will require the basic knowledge of the regular expressions and make your complex work easier.

Considering the below example will show how you can replace the spaces in the name of the directories present in the current working directory with an underscore.

find . -mindepth 1 -prune -type d | rename 'y/ /_/'

Well, renaming directories is one of the simple tasks that can be accomplished using the “mv” command from the command line. But if you are looking to rename multiple directories at the same time, you might find it difficult as you need prior knowledge of regular expressions and bash scripting for doing so.

In this article, we have focused on two commands for renaming the directories- the “mv” command and the “rename” command.

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.