How to rename multiple folders in Linux using command line

I am new to Linux programming and bash shell commands. I have a folder structure as dir1, dir2, dir3 and I would like to rename it as dir001, dir002, dir003. How do you rename multiple folders in Linux?

The mv command (mv source target) renames the file/folder named by the source operand to the destination path named by the target operand. However, the mv works with a single file name and directory/folder name on Linux and Unix-like system. Therefore, this page shows alternate methods to rename multiple folders on Linux and Unix-like systems.

How to rename multiple folders in Linux

  1. To rename multiple folders, one can use rename command from MariaDB/MySQL or Linux utilities package.
  2. Another option is to use the bash for loop.

Linux rename multiple folders using rename command

The syntax is:
rename expression replacement file

rename command examples

Let us see some examples. Create some files as follows using the touch command touch file{1..4}.txtz
List those files:
ls file*
Now correct file extension from .txtz .txt i.e. fix the extension of your .txtz files:
rename -v .txtz .txt *.txtz
Verify with the help of ls command ls file*
How to rename multiple folders in Linux

  • -v : Verbose output
  • .txtz Match all .txtz extension
  • .txt Replace with .txt
  • *.txtz Work on all *.txtz file in the current working directory

Working on folders

Let us create some folders using the mkdir command:
mkdir dir{1..5}
ls -d dir*

Now rename dir1, dir2 as dir001, dir002, and so on:
rename -v dir dir00 dir?
ls -d dir*

Linux rename multiple folders using rename command
Please note that the rename command has no safeguards options by default. Therefore, you might get the wrong file/folder names. It is advisable that you first backup all data and do dry run as follows:
rename -n -v dir dir00 dir?
Do not make any changes by passing the -n option (dry run) and we added -v option to see what would be made.

Getting help on rename command

Run man command as follows:
man rename
rename --help
Sample outputs:

 -v, --verbose       explain what is being done
 -s, --symlink       act on the target of symlinks
 -n, --no-act        do not make any changes
 -o, --no-overwrite  don't overwrite existing files
 -i, --interactive   prompt before overwrite
 -h, --help          display this help
 -V, --version       display version

Renaming multiple folders using bash for loop

Say you have dirs as follows:

total 0
drwxrwxr-x. 2 vivek vivek 40 May 19 11:18 'mp 4 dir 1'
drwxrwxr-x. 2 vivek vivek 40 May 19 11:18 'mp4 dir  2'
drwxrwxr-x. 2 vivek vivek 40 May 19 11:18 'music dir 1'
drwxrwxr-x. 2 vivek vivek 40 May 19 11:18 'music  dir 2 '

We need remove the white spaces in the folder names. Run:

for i in *
  mv -v "$i" "${i// /}"

Renaming multiple folders on Linux
In this final example will use POSIX shell rename all *.PY file to *.py:

for j in *.PY
  mv -v -- "$j" "${j%.PY}.py

mmv – a Command line tool for Renaming multiple files in Linux

First install mmv using the apt command/apt-get command/yum command/apt-get command as per your distro:
sudo yum install mmv ### <--CentOS/RHEL and co ###
sudo apt install mmv ### <-- Ubuntu/Debian and co ###
sudo dnf install mmv ### <-- Fedora Linux ###

Sample outputs from Fedora 30 box:

Dependencies resolved.
 Package      Architecture    Version                  Repository       Size
 mmv          x86_64          1.01b-30.fc30            fedora           38 k
Transaction Summary
Install  1 Package
Total download size: 38 k
Installed size: 67 k
Is this ok [y/N]: y
Downloading Packages:
mmv-1.01b-30.fc30.x86_64.rpm                  23 kB/s |  38 kB     00:01    
Total                                         13 kB/s |  38 kB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                     1/1 
  Installing       : mmv-1.01b-30.fc30.x86_64                            1/1 
  Running scriptlet: mmv-1.01b-30.fc30.x86_64                            1/1 
  Verifying        : mmv-1.01b-30.fc30.x86_64                            1/1 

The mvm command move, copy, append/link multiple files/dirs by wildcard patterns. The syntax is:
mmv from to
mmv [options] from to

Say you wan to make all folder names uppercase, try:
mmv "dir*" "#u1"
If you wan to make all folder names lowercase, try:
mmv "dir*" "#l1"
mmv - a Command line tool for Renaming multiple files in Linux
You can change a suffix i.e. extension from *.C to *.cpp as follows:
mmv '*.C' '#1.cpp'

the extension “.bar” is lost and the extension “.foo” is added.
The mmv command is powerful cli tool, and it has many more options. Thus, read the man page of the mmv:
man mmv

Use thunar GUI tool to rename multiple folder at once in Linux

The thunar is an easy to use file manager for the Xfce Desktop Environment. You can install it as follows:
sudo yum install thunar ### <--CentOS/RHEL and co ###
sudo apt install thunar ### <-- Ubuntu/Debian and co ###
sudo dnf install thunar ### <-- Fedora Linux ###

Sample session from thunar:

Thunar bulk file and folder renaming on Linux

File Manager for the Xfce Desktop Environment with the bulk renamer


You learned how to rename multiple folders in Linux using various options. See my page “Linux Rename Multiple Files At a Shell Prompt” for more information. The rename command is part of the util-linux package and is available here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum