How to Rename Multiple Directories in Linux at Once

Last updated: January 16, 2023

To rename a single directory in Linux is simple and straightforward. Whereas to do with multiple directories, we need to use special commands, scripts, or a combination of different commands.

In this tutorial, we learn how to rename multiple directories in Linux at once.

Batch Renaming Directories Using rename Command

Rename command is primarily used to rename multiple files using regular expressions. We may also use it to rename multiple directories at once.

The rename utility is not installed by default in most Linux distributions. It's readily available in default OS repositories and can easily be installed using the package manager. Note on RHEL-based distributions, the rename command is known as prename.


rename 's/old-dir-name/new-dir-name/' old-dir-name*

This will rename all directories in the current directory starting with the name old-directory to new-dir-name.

For example to rename directories named dir1, dir2, and dir3 to newdir1, newdir2, and newdir3 respectively, type:

rename -v 's/dir/newdir/' dir?
bulk rename directories using rename command

All directories with the name dir are changed to newdir using rename command.

Note: The rename command does not prompt for confirmation before making any changes, so be careful when running this command. I would recommend using -n option first to perform a dry run that shows you the changes that would be made without actually making them.

Using a Script

Let's check a few examples of how to utilize the shell scripts to rename directories in bulk. Here I am going to use two bash scripts.

Example 1:

Let's make use of for loop to rename multiple directories by searching directories in the current directory and then rename them using the mv command:



for dir in *; do
  if [ -d "$dir" ]; then
    mv -v "$dir" "$new_dirname"

Run the bash script:

bash script bulk rename directories using loop

We have used for loop to check through the current directory and check if each object is a directory. If it is a directory then use the mv command to rename the directory.

Example 2:

Let's use the following script to find all directories in the current directory with the name 'dir' and rename to 'newdir' using the rename command.


dirs=$(find . -type d -name '*dir*')
rename 's/dir/newdir/g' $dirs

Run the bash script, type:

bash script bulk rename directories using rename command

Here we used find command to search directories with name dir in the current directory. Then used rename command to replace all occurrences of it with the name newdir.

Find and rename directories

Let's check how to use the find command to search directories and rename them. I have provided a few oneliner examples to illustrate it.

01. Find and rename directories using rename command

To find all the directories in the current directory and its subdirectories having dir in their names and rename them with testdir, type:

find . -type d -name '*' | rename 's\dir\newdir\'
find and rename multiple directories

Here we have used find with rename command to rename multiple directories.

02. Find and rename directories using mv command

The following oneliners first use the find command to search all directories in the current directory and its subdirectories whose names contain the string "dir". Then uses exec flag to mv command to rename directories replacing 'dir' with 'testdir' using sed command.

find . -type d -name '*dir*' -exec bash -c 'mv "$1" "$(echo "$1" | sed "s/dir*/testdir/g")"' _ {} \;
find and use mv command to rename multiple directories

The following command uses the mv command to rename the directory by adding the ".abc" extension to the end of the original name.

find . -type d -name 'dir*' -exec mv {} {}.abc \;

In the following command, we have used find command to search directories that match '.*/dir.*' regular expression. The exec flag is run for each match to execute rename operation.

find . -type d -regex '.*/dir.*' -exec mv {} {}testdir \;
find and use regex to rename multiple directories


In this tutorial, we learned how to rename directories in Linux from the command line in bulk. What we used are rename command, bash script, and find with mv.

You may use mmv command with -r option which will recursively rename files and directories. Remember this does for both files and directories so be careful.

Thanks for reading, please leave your suggestions and feedback in the below comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment