How To Rename Multiple Files Together in Linux Using 3 Methods

by SathiyaMoorthy on June 12, 2009

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.

  1. 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.
  2. 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.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 13 comments… read them below or add one }

1 Binny V A June 12, 2009 at 12:24 pm

Thanks for the midnight commander tip – I did not know that. Anyway, I recommend using KRename for this – really nifty tool.

2 Iftikhar Ul Hassan June 12, 2009 at 12:59 pm

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/)

3 !ncognito June 13, 2009 at 8:41 am

1st method seems dead simple.. Nice post!

4 NARDI June 13, 2009 at 2:57 pm

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 =)

5 Nabin Limbu June 14, 2009 at 12:16 am

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

6 Flynets June 14, 2009 at 8:07 am

Replace space char with underscore from files
find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;

7 Ramesh Natarajan June 14, 2009 at 11:33 pm

@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.

8 Nick Stoianov December 2, 2009 at 5:02 pm

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.

9 john.dennis@mailcatch.com January 4, 2011 at 2:09 pm

None of these examples appear to work in xandros linux (eeepc) except midnight commander which is so klunky I am frightened of using it.
Why is it SO difficult to rename multiple files in linux when it is so easy in DOS ?’
When i chose linux for my netbook I was swayed by the supposed easy availability of thousands of free programs. Well I can’t find a free program which will rename files – such a common requirement, and none of the command line solutions appear to work. (including chcase ) Does anyone know of one? Is it Xandros that is the problem?
There are dozens for Windoze….I did see in one forum where somebody actually suggested changing the file names manually one by one. Shome joke shurely?

10 nardi January 5, 2011 at 3:07 am

to john.dennis@mailcatch.com:
Hi John. From your complaints we can hardly figure out how to help. It might be just a little misunderstanding or nonstandard behavior.
Could you please be more specific about what commands did you run and what output did you get? It would also be nice to know what shell are you using (type in “echo $SHELL”) just to make sure it unedrstands bash commands.
Yeah and how is this done in DOS actually?

11 richard January 6, 2011 at 1:57 pm

In response to Nick, removing the s/ and all subsequent /’s using a space separator with Fedora worked perfectly.

rename .html .htm *.html

12 reet November 6, 2012 at 8:37 am

in ubuntu server is working this version
[code]
rename 's|was|new|g' /target/dir
[code]
it does the job ex cept removing the space from filenames

13 @ropherpanama May 25, 2013 at 1:41 pm

Hey! In CentOS you can type :
rename abc 123 * and all the files in your directory that contain the abc string will be replace with 123, it’s really nice! Thanks!

Leave a Comment

Previous post:

Next post: