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.
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. 3 Methods To View tail -f output of Multiple Log Files in One Terminal
  2. 5 Methods to Get Quick Help on Linux Commands
  3. How To Mount and View ISO File as Root and Regular User in Linux
  4. Midnight Commander (mc) Guide: Powerful Text based File Manager for Unix
  5. Unix Shell Tips: Change Login Shell From Bash to Others
  

Vim 101 Hacks Book

{ 8 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.

Leave a Comment

Previous post:

Next post: