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.

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*

 

Where,

 

-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*

 

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

 

OR

rename --help

 

Sample outputs:

 

Options:

-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 *
do 
  mv -v "$i" "${i// /}"
done

 

In this final example will use POSIX shell rename all *.PY file to *.py:

 

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

 

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
=============================================================================
Installing:
 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 
 
Installed:
  mmv-1.01b-30.fc30.x86_64                                                   
 
Complete!

 

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"

 

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 ###
Was this answer helpful? 0 Users Found This Useful (0 Votes)