Howto Linux rename multiple files at a shell prompt

Q. How do I rename rename multiple files at a shell prompt under Linux or UNIX?

A. Renaming multiple files at a shell prompt is always considered as a black art by many UNIX gurus.

To be frank if you understand regex then it is not a black art anymore. But what the hell regex is? A regular expression is a string that describes or matches a set of strings, according to certain syntax rules (see regex @ wikipedia for more information). Linux (and *BSD) comes with handy utility called rename. As a name suggest 'rename' renames the filenames supplied according to the rule specified (syntax):
rename "regex-rule" files

Rename command syntax

rename oldname newname *.files

For example rename all *.bak file as *.txt, enter:
$ rename .bak .txt *.bak

Linux rename multiple files - more examples

Let us see couple of practical examples:

A) Convert all mp3 filenames to more readable and usable format. Most of the time MP3 got multiple blank spaces, which my confuse many Linux utilities and mp3 players (before rename command):

$ ls 

Output:

06 -  Gorillaz - Feel Good Inc.mp3
DDR - Kung- Fu Fighting (bus stop).mp3
AXEL CRAZYFROG.mp3

Remove all blank space with rename command:
$ rename "s/ *//g" *.mp3
$ ls

Output:

06-Gorillaz-FeelGoodInc.mp3
DDR-Kung-FuFighting(busstop).mp3
AXEL-CRAZYFROG.mp3

Linux Shell script to rename files

Before rename command I was using following shell script to rename my mp3s:

#!/bin/bash
# To remove blank space
if [ $# -eq 0 ];
then
 echo "Syntax: $(basename $0) file-name [command]"
 exit 1
fi
FILES=$1
CMD=$2
for i in $FILES
do
# remove all blanks and store them OUT
OUT=$(echo $i | sed 's/  *//g')
if [ "$CMD" == "" ];
then
#just show file
echo $OUT
else
#else execute command such as mv or cp or rm
[ "$i" != "$OUT" ] && $($CMD  "$i"  "$OUT")
fi
done

B) To remove .jpg file extension, you write command as follows:

$ rename 's/\.jpg$//' *.jpg

C) To convert all uppercase filenames to lowercase:

$ rename 'y/A-Z/a-z/' *

Read man page of rename command for more information.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 21 comments… read them below or add one }

1 Jose 11.09.07 at 2:01 am

Very Good job dude with that examples.. was helpfull thanks

2 tcw 03.16.08 at 5:25 pm

The ‘rename’ command seems not working for me.. :(

3 leopinzon 05.12.08 at 4:35 pm

Perfect!!

You can also use the standard input to find in a folder structure like:

find . -name \*exp_to_find_in_folders\* | rename "s/exp_to_find_for_replacement/exp_to_replace/"

4 marco 09.03.08 at 2:49 am

I knew only the crapy mv method, i never thought that linux has a “rename”. :P Thanks! :)

5 mikoto 09.10.08 at 9:57 am

Wow, thank you!

6 fatma 11.18.08 at 2:40 pm

Thanks! Did help a lot!

7 Ankur 02.06.09 at 9:27 am

thanx a ton……its amazing, i though linux dont hav rename……but does.

8 bassam 03.10.09 at 11:24 pm

Thanks…
but how i can rename
foo1.a,foo2.a,…,foo10.a
To
xxx1.a,xxx2.a,….,xxx10.a
???
thanks again

9 Name 03.11.09 at 8:41 pm

rename ’s/foo/xxx/’ foo*.a

10 diepes 03.19.09 at 9:02 pm

rename ’s/foo(.*)\.a/xxx$1.avi/’ *.a

the (.*) in the round brackets creates a reference that you can use in the name as $1.
you can have more than one match.

11 Luka 03.23.09 at 2:12 pm

Thanks for all the tips. Can you help mi with this specific example? I have files named:

latinum_20080101_0305-200272-93930-2-25.mp3
latinum_20080101_0310-200279-93934-2-25.mp3
latinum_20080101_0315-200671-94112-2-25.mp3

and I want to rename them to get the following:
latinum_20080101_0305.mp3
latinum_20080101_0310.mp3
latinum_20080101_0315.mp3

How can I do that using rename? (I have several hundred of files like that.)

12 Paul Feakins 03.24.09 at 11:37 am

How about:

rename ’s/-*//’ *.mp3

Hopefully that should remove everything after the first hyphen. It’s a guess though, try it on a small sample first ;)

Paul.

13 Henry 04.30.09 at 6:52 pm

My rename doesn’t seem to support regexes. Fedora 10. How can I get this version?
# rename -V
rename (util-linux-ng 2.14.1)

14 Vivek Gite 04.30.09 at 7:05 pm

It should work 2.13 was around for sometime and 2.14 is latest. Can paste your example here..

15 Henry 04.30.09 at 7:17 pm

Example file name: 2001DODGE3500546424-10_12_1.JPG
Desired file name: 2001DODGE3500546424-10.jpg

Command: rename "s/(_[0-9]+)+\.JPG/.jpg/" *.JPG

16 V.Balaviswanathan 05.05.09 at 2:11 pm

Cool one… Thanks a lot… Good job… Keep this good work going :)

17 Henry 05.29.09 at 5:33 pm

It seems to be a Debian vs. other distros issue. Debian uses prename, perl rename, which it maps to /usr/bin/rename. I found the source for one version of perl rename online at http://tips.webdesign10.com/files/rename.pl.txt and using that, my regex above works as desired.

18 newbie 06.05.09 at 12:41 pm

how can we rename 100 files in folder
1 fooo1.txt
2 fooo2.txt
3 fooo3.txt
….

to :-
1fooo1.txt
1fooo2.txt
1fooo3.txt
…….

19 b0nUx3R 06.30.09 at 3:15 pm

I have a lot of .png file with extension “*.png;1″ but I want to rename all in time to “*.png” how to do that?

Thanks :)

20 b0nUx3R 06.30.09 at 3:17 pm

oh yeah,i forgot…

example :
addedit.png;1 filesave.png;1 publish_r.png;1
addusers.png;1 folder_add_f2.png;1 publish_x.png;1
apply_f2.png;1 folder_add.png;1 publish_y.png;1

and want to convert to :
addedit.png filesave.png publish_r.png
addusers.png folder_add_f2.png publish_x.png
apply_f2.png folder_add.png publish_y.png

21 Richard 07.02.09 at 9:02 am

abd now his one :

AAAAAAB0020090617173034173034AAAA21_-_0001329515.txt.gz

into

AAAAAAB0020090617173034AAAA21_-_0001329515.txt.gz

anyone?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , ,

Previous post: How do I Read ext2 or ext3 filesystems under Windows 2000 or XP Desktop?

Next post: How do I print out a Linux man or info page?