Renaming a single file is very simple task for Linux user. But renaming multiple files in group may not be a straight forward task. In this article, let us review 3 different methods to rename multiple files together.
Method 1. Use Rename Linux Command
Using rename command you can rename group of files. The syntax for renaming multiple files in group using rename command is given below.
Syntax: rename perlexpr [ files ]
To rename all the html files from html to htm extension in the current directory do the following.
rename s/.html/.htm/ *.html
Using the same concept, you an also rename all the *.txt files to *.bak files (or) all the *.bak files to *.txt files etc.,
Method 2. Use Linux Shell Script to Rename Multiple Files Together
You can write your own shell script to rename the files of specified pattern recursively as:
for filename in *.sql do echo $filename; w_o_ext=`basename $filename .sql`; echo $w_o_ext; mv $filename $w_o_ext.psql; done
Method 3. Use Midnight Commander to Rename Multiple Files
A while back we reviewed about the powerful Linux midnight commander (mc) file explorer. Using midnight command you can rename multiple files using regular expression as explained below.
- Select the required files using regular expression. Press + which will ask the regex to select files. For example, giving *.psql will select all the files with psql extension.
- Rename all the selected files using regex. Press F6 which will ask for the source and destination regex, doing so will change the file names. For this example, give *.psql in source and *.sql in destination which will rename all *.psql files to *.sql files.
Get free Unix tutorials, tips and tricks straight to your email in-box.
If you enjoyed this article, you might also like..


My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 8 comments… read them below or add one }
Thanks for the midnight commander tip – I did not know that. Anyway, I recommend using KRename for this – really nifty tool.
Hi, that’s really nice, thanks for the great post.
But have you tried using the “mmv” command (multiple move). This utility is not part of linux standard command set (I don’t know why) but it’s really nice.
So you just need to install the utility
hassan@linux:~$ sudo apt-get install mmv
Then you can move multiple files very easily, here is an example.
==>I want to rename all files which have extension *.htm to *.html<==
hassan@linux:~$ mmv “*.htm ” “#1.html”
You can read more about it from the excellent tutorial here (http://linux.dsplabs.com.au/mmv-copy-append-link-move-multiple-files-under-linux-shell-bash-by-wildcard-patterns-p5/)
1st method seems dead simple.. Nice post!
wow.. didn’t know that rename is so powerfull. I’ll try to use it instead of my favourite
for f in * ; do mv “$f” “${f//foo/bar}” ; done
I’m not sure if it was mentioned here before but JFYI:
f=foofoo.foo
#replace first
echo ${f/foo/bar} # -> barfoo.foo
#replace all occurences
echo ${f//foo/bar} # -> barbar.bar
#replace first match from the beginning
echo ${f#f*f} # -> oo.foo
#notice the ’star’ consumes only ‘oo’ here
#replace all (longest match of regexp) from the beginning
echo ${f##f*f} # -> oo
#here, there is ‘oofoo.’ hidden under the ’star’ in regex ‘f*f’
#extension replacement
echo ${f%foo}bar # -> foofoo.bar
#replace first from the end
echo ${f%oo*} # -> foofoo.f
#replace all (longest match of regexp) from the end
echo ${f%%oo*} # -> f
enjoy =)
Hi,
There are lots of time when we require to swap files between two configuration files. Is there any single command which will swap two files instead of writing 3 commands like this: mv abc.txt tmp.txt; mv def.txt abc.txt; mv tmp.txt def.txt
Replace space char with underscore from files
find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;
@Binny,
Thanks for the information about KRename, which definitely a very useful tool.
For those who have not used KRename, check-out some screenshots of Krename
@Iftikhar,
Thanks for the info. mmv looks similar to rename. Is there anything special that mmv does which rename cannot do? (or) Is it pretty much the same?
@Nardi,
Your for-loop is similar to the method-2. Thanks for showing your detailed examples on how you do the move.
@Nabin,
I'm not sure whether there is a single command that will do the swap. I hope someone else can throw some ideas on your question. One thing that came to my mind is -- Instead of doing multiple mv's probably you can create a alias called mvswap which can have the multiple mv commands.
@Flynets,
Replacing space in a filename with underscore or hypen in a common requirement. Thanks for sharing your great example to solve that problem.
FYI: rename command in RedHat DOES NOT accept perl regular expressions. So…..the command: rename s/.html/.htm/ *.html WILL NOT work on RedHat.
If you want to use regex on Redhat in renaming files – use: ren
Example: /usr/local/bin/ren ’s/ //g’ *.txt
This example will remove all the white spaces from the text files in a directory.