This post shows how to rename multiple files in Linux at once, include rename files using pattern and using command find to rename multiple files recursively, of course also include how to rename the extension of multiple files.

Method1 : Rename multiple files using command mv

we can mv command to rename file , normally one file once. But we can use for loop or find command together to rename multiple files at once.

For loop

Below example rename files with extension htm to html

j@ubuntu2004:~/tmp/1010$ ls
1.htm  2.htm  3.htm
j@ubuntu2004:~/tmp/1010$ for f in *.htm ; do mv $f ${f/.htm/.html} ; done
j@ubuntu2004:~/tmp/1010$ ls
1.html  2.html  3.html

Here ${f/.htm/.html} is shell parameter expansion

Alternatively you can try sed version

j@ubuntu2004:~/tmp/1010$ ls
1.html  2.html  3.html
j@ubuntu2004:~/tmp/1010$ for f in *.html; do mv "$f" "$(echo "$f" | sed s/html$/htm/)"; done
j@ubuntu2004:~/tmp/1010$ ls
1.htm  2.htm  3.htm

The benefit is you can use regular expression

Find command

j@ubuntu2004:~/tmp/1010$ ls
1.htm  2.htm  3.htm
j@ubuntu2004:~/tmp/1010$ find . -name '*.htm' -exec bash -c 'mv $0 ${0/.htm/.html}' {} \;
j@ubuntu2004:~/tmp/1010$ ls
1.html  2.html  3.html

Above command used find and shell parameter expansion to rename multiple files.

Using find and sed

j@ubuntu2004:~/tmp/1010$ ls
1.html  2.html  3.html
j@ubuntu2004:~/tmp/1010$ for f in *.html;do mv $f $(echo $f |sed s/.html/.htm/);done
j@ubuntu2004:~/tmp/1010$ ls
1.htm  2.htm  3.htm

The benefit of using find is you can rename multiple files recursively or traversing directories.

One more example:

j@ubuntu2004:~/tmp/1010$ ls
prefix01.htm  prefix02.htm  prefix03.htm
j@ubuntu2004:~/tmp/1010$ for f in *.htm;do mv $f $(echo $f |sed s/^prefix/pre/);done
j@ubuntu2004:~/tmp/1010$ ls
pre01.htm  pre02.htm  pre03.htm

Method 2 : Rename multiple files using rename

Please be noted for rename command it’s different between Debian/Ubuntu and other version of linux like centos/rhel.

In Debian/Ubuntu

To install rename

sudo apt install rename

Below example renames files with extension .htm to .html

j@ubuntu2004:~/tmp/1010$ ls
1.htm  2.htm  3.htm
j@ubuntu2004:~/tmp/1010$ rename 's/.htm/.html/' *.htm
j@ubuntu2004:~/tmp/1010$ ls
1.html  2.html  3.html

One more example:

j@ubuntu2004:~/tmp/1010$ ls
pre01.htm  pre02.htm  pre03.htm
j@ubuntu2004:~/tmp/1010$ rename 's/^pre/PRE/' *.htm
j@ubuntu2004:~/tmp/1010$ ls
PRE01.htm  PRE02.htm  PRE03.htm

Please man rename for more usages

In RHEL/CentOS

[j@centos 1010]$ ls
1.htm  2.htm  3.htm
[j@centos 1010]$ rename .htm .html *.htm
[j@centos 1010]$ ls
1.html  2.html  3.html

One more example:

[j@centos 1010]$ ls
pre01.htm  pre02.htm  pre03.htm
[j@centos 1010]$ rename pre PRE *.htm
[j@centos 1010]$ ls
PRE01.htm  PRE02.htm  PRE03.htm

Here you can see the usage of rename in Centos is totally different with the one in Debian/Ubuntu.

Also please man rename for details