Howto: Linux Rename Multiple Files At a Shell Prompt

by Vivek Gite on November 26, 2005 · 75 comments

How do I rename multiple files at a shell prompt under Linux or UNIX operating systems?

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

Examples: Linux Rename Multiple Files

Convert all mp3 filenames to more readable and usable format. Most of the time MP3 got multiple blank spaces, which may confuse many command line based Linux utilities and mp3 players

$ 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 using the rename command I was using the 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

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

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

To convert all uppercase filenames to lowercase:

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

Read the man page of rename command for more information:
man rename

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 75 comments… read them below or add one }

1 Jose November 9, 2007

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

Reply

2 tcw March 16, 2008

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

Reply

3 kamran in ciit and haji shahid sahab and haji shehriyar April 5, 2011

“TRY THIS ONE”

vi change.sh
i
cd $1
j=1
for i in `ls $2`
do
mv $i file{$j}.txt
j=`expr $j + 1`
done
press esc
:wq

chmod +x change.sh
./change.sh ~/file *.txt

Reply

4 JOHN April 5, 2011

WOW….
OH MY GOD ,IT REALLY WORKS
I CAN’T BELIVE THIS….

Reply

5 leopinzon May 12, 2008

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

Reply

6 zougi January 17, 2012

leopinzon’s command didn’t worked for me. I used this one:

find -name ‘*exp_to_find_in_folders*’ -exec rename “s/exp_to_find_for_replacement/exp_to_replace/” {} \;

Reply

7 marco September 3, 2008

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

Reply

8 mikoto September 10, 2008

Wow, thank you!

Reply

9 fatma November 18, 2008

Thanks! Did help a lot!

Reply

10 Ankur February 6, 2009

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

Reply

11 bassam March 10, 2009

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

Reply

12 Scott July 20, 2011

rename “s/foo*/bar/g” *.a

Renames any file with an .a extension that contains “foo” to “bar” in the directory.

Reply

13 Name March 11, 2009

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

Reply

14 diepes March 19, 2009

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.

Reply

15 Luka March 23, 2009

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

Reply

16 Aarthi June 8, 2011

Hey even I’m working on this kind of renaming. Did the solution provided worked for you?? It didn’t worked out for me. Please let me know how have you done it.
Thanks!

Reply

17 Kalleguld October 12, 2011

A little late , but here goes:

s/-.*\.mp3/.mp3/

removes everything after the dash and before the .mp3 (dash included)

Reply

18 Paul Feakins March 24, 2009

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.

Reply

19 Aarthi June 8, 2011

This rename ‘s/-*//’ *.mp3 did not work for me… Can you please tell me why it didn’t?? Also, Could you please suggest if there is any approach for this…

Reply

20 Zinc June 11, 2011

it follows perl rules.

What you need to do is to tell it:

rename “s/-.+/\.mp3/” *.mp3

The dash will register as it should. The stop right after the dash signifies a single character. The plus after that means “one or more of the previous thing”

to test it, you don’t need to create a new file, just put -n right after rename like this:
rename -n “s/-.+/\.mp3/” *.mp3

Reply

21 Henry April 30, 2009

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)

Reply

22 Vivek Gite April 30, 2009

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

Reply

23 Henry April 30, 2009

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

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

Reply

24 Owen February 1, 2011

Henry,

Thank You! I’m on Opensuse 11 and this was driving me crazy… I couldn’t figure out if it was my expression or what was going on. I copied the perl file you linked to and put it in my /bin/ folder and set it to executable permissions.

Then I called
/bin/rename ‘s/(_[0-9a-z]{32})+\.jpg/ .jpg/’ *.jpg

Original File: 321-646-001_01_5f81f62b4b8f448ff50eb0788aa628c7.jpeg
New File: 321-646-001_01.jpg

This is just what I needed. I’m using Google Page Speed Firefox Add-On in combo with Firebug to download Optimized versions of all of my sites images. Google rather annoyingly renames all the files and adds a _ character plus a 32 digit alpha numeric string and then changes the extension to .jpeg for .jpg files. This is just the fix that I was looking for.

Thanks Google and Thanks Henry too.

Reply

25 V.Balaviswanathan May 5, 2009

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

Reply

26 Henry May 29, 2009

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.

Reply

27 Callum December 23, 2010

Henry, this is an incredibly useful tip, thanks a lot. I was bashing my head against a wall trying to figure out why rename on one machine worked but not on another. I uploaded my ubuntu prename to ~/bin on my centos machine and it “just worked”.

I’m very grateful, could have spent hours trying to figure that out! :-)

Reply

28 newbie June 5, 2009

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

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

Reply

29 b0nUx3R June 30, 2009

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

Reply

30 b0nUx3R June 30, 2009

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

Reply

31 Richard July 2, 2009

abd now his one :

AAAAAAB0020090617173034173034AAAA21_-_0001329515.txt.gz

into

AAAAAAB0020090617173034AAAA21_-_0001329515.txt.gz

anyone?

Reply

32 Natan July 14, 2009

so :

for i in *.txt.gz; do mv $i `echo $i |cut -c1-23,30-55` ; done

Reply

33 miguel July 18, 2009

how can I rename in specified directory only ?
filename.extension? -> filename.extension

(I have downloading script, but after pasting filename in windows codepage all extensions are finished with “?” and windows see filename garbled..)

Reply

34 miguel July 18, 2009

how can I rename in specified directory only ?
filename.extension? -> filename.extension

(I have downloading script, but after pasting filename in windows codepage all extensions are finished with �?� and windows see filename garbled..)

Reply

35 konstantin July 20, 2009

hello does somebody knows how to rename the following??

from:

konstantin.bcn
konstantin.bcn0
konstantin.bcn1
konstantin.bcn2

to

georg.bcn
georg.bcn0
georg.bcn1
georg.bcn2

thanks a lot in advance!!

konstantin

Reply

36 SHARYAR AND KAMRAN March 25, 2011

j=1
for i in *.txt
do
mv $i file$j.bak
j=`expr $j + 1`
done

Reply

37 pir July 24, 2009

rename ‘s/konstantin/georg/’ konstantin.bcn*

Reply

38 JimmyNY July 30, 2009

Nobody has ever asked this question online it seems…

Problem: Stupid MAC (which I dont have anymore named files with these characters:
“:”
“?”
etc.. in the file names.. I could not find any file renamer that would rename these files in Windows/DOS..

So I tried FENDORA linux and I can rename the manually one by one but i have hundreds…

HELP!!!!

No RENAME or mv command works.. I trired everything i can find online..

I tried specifing the “:” like this “\:” “\x3a” etc.. and nothing.

does anyone know how to strip these retardted OS-illegal characters out of the names.. in one shot.?

I know the ren command in DOS.. but I have no clue it seems in Linux.
and no i don’t have a mac or access to one..

Thanks ahead of time, for your help.

Reply

39 Sakiwi October 7, 2011

rename ‘s/[^[:print:]]/_/g’ *txt
This will replace all ‘non-printable’ characters in the name with and underscore.

Or if that does not work try escaping the character, e.g:
rename ‘s/\?/_/g;s/\:/_/g’ *.txt

Reply

40 pd August 4, 2009

I want to rename files and folder like:
test .txt—–>test.txt
text1 .xls—-> text1.xls
“folder1 ” —->>folder1
“folder2 “—–>>fodler2
Actually there are some files and folders in windows which has blank space at the end of file and folder name.
Basically I want to remove blank space at the end of each file and folder.But there are some different extension files as well.

Reply

41 Wonky September 2, 2009

what if i have a load of files named differently(all .jpg) and i want to rename them into a sequence caleld for example ( test001.jpg test002.jpg and so on )? cheers

Reply

42 sheeep March 19, 2010

thx!

Reply

43 strAlan April 5, 2010

What if I have a bunch of files with no extension and I want to add an extension to them? I tried:
rename * *.mp3
rename * .mp3 *
rename * .mp3

but I keep getting a Bareword error :(

Reply

44 khedron May 15, 2010

strAlan: rename’s probably not the best here, try:
[code]
for file in *; do
mv "$file" "$file".mp3;
done
[/code]

Reply

45 jingo June 3, 2010

cat OPs_taste_in_music > /dev/null

Reply

46 vx11 July 10, 2010

thanks…, good information

Reply

47 Tarique July 27, 2010

Didn’t worked for me. My file name consists spaces :(

Like,

Report July 2010.xls

Reply

48 gazmanic August 26, 2010

In a directory we have two files a1a and a2b.
rename a. a a*
rename ‘s/a./a/’ a*
where . represents wildcard “one character”
The expected result should be aa and ab by stripping the numeric character 1 and 2 from the file name.

Neither is working under GNOME Terminal 2.30.1
Why?

Reply

49 gazmanic August 26, 2010

Note spaces are easily done using rename.
For example
rename ” ” “_” *.mp3

rename “s/ *//g” *.mp3 as per the original example of the OP does not work for me.
Why?

Reply

50 ejoftiduttu August 28, 2010

gal02_1024-768_tcm251-138878.jpg
gal03_1024-768_tcm251-138882.jpg
gal04_1024-768_tcm251-138886.jpg
gal05_1024-768_tcm251-138890.jpg
gal07_1024-768_tcm251-138898.jpg

How to rename these files as 1.jpg 2.jpg….

Reply

51 Vivek Gite August 28, 2010
i=1
for j in *.jpg; do mv "$j" "$i.jpg" ;(( i++ )); done

Backup files before running above code.

Reply

52 Jake December 13, 2010

In this command:
i=1
for j in *.jpg; do mv “$j” “$i.jpg” ;(( i++ )); done

How would I get the system to number as _00.jpg, _01.jpg, _02.jpg etc……
instead of _00.jpg, _1.jpg, _2.jpg, etc…..
?

Reply

53 Maarten September 2, 2010

I would like to use the script but use strings.
For example i have:

rename s ‘/text/Text/’ *
this checks all files for have the name text in it and renames it to Text.
If i put it in a script:

#!/bin/bash
#rename script
rename ‘s/text/Text/’ *

it works at the following way: $./rename.sh

As i want to fill in what file to rename i want to use the script as following:
$./rename.sh text Text

can somebody help me out how to use variables.
Thnx!

Reply

54 Tarique September 5, 2010

Adjust this with your script and then try -

#!/bin/bash

match=$1
substitute=$2

rename ‘s/’$match’/'$substitute’/’ *

Then Run-

$./rename.sh text Text

Reply

55 Shang October 1, 2010

how to rename -file to file?

Reply

56 willem19 November 4, 2010

Ok,

I want to check if a file starts with numbers, i.e.:
101-howdy-832.mp3 -> howdy-832.mp3 ->
10-willem-hello-(radio_edit).mp3 -> willem-hello.mp3
and if it does remove it.

Any tips?

Reply

57 André November 22, 2010

if rename didn’t work for you, then use this:

for file in *; do if [ -f $file ] ; then name=${file%\.*}; mv $file ${name}; fi ; done

Reply

58 willem19 December 19, 2010

This only removes *.mp3 :-(

Reply

59 Jake December 13, 2010

using this command:
i=1
for j in *.jpg; do mv “$j” “$i.jpg” ;(( i++ )); done

How would I get the system to add a 0 when using single digits? So instead of having the results being
_00.jpg, _1.jpg, _2.jpg, etc…….
it would return
_00.jpg, _01.jpg, _02.jpg, etc…

Reply

60 willem19 December 19, 2010

Im writing my own script to rename my mp3 collection, and i want to remove all junk from the filenames:

Is it possible to check if a filename starts with numeric digits?
Cause now it remove all the first characters till -
But i only want it remove (ONLY!) numeric characters till -

i.e.
09-willem-hello.mp3 -> willem-hello.mp3 (remove 09-)
hesss-hodwy.mp3 -> hesss-hodwy.mp3 (nothing removed)

if filename starts with 09; then do; etc :-)

Another thing is how to remove (*_bpm) where * = a wildcard, i.e 120, 59 etc.

This what i’ve made so far:
#!/bin/bash
# Pattern matching using the # ## % %% parameter substitution operators.
pattern1=*-* # * (wild card) matches everything between.
for file in ls *.mp3; do
if [ -e "$file" ]
then
echo “Number of characters ${#file}”
echo Oldname: ${file}
newname=”$(echo ${file#$pattern1} | sed ‘s/ /_/g’ | tr ‘[A-Z]‘ ‘[a-z]‘ | sed ‘s/_feat_/_ft._/g’ | sed ‘s/_feat._/_ft._/g’| sed ‘s/_-_/-/g’)”

if [ "$file" != "$newname" ] ; then
echo Newname: $newname
mv “$file” “$newname”
echo —————————————-
else echo Nothing to do!
fi
#echo ‘${var1#$pattern2} =’ “${var1#$pattern2}” # d12345abc6789
# Shortest possible match, strips out first 3 characters abcd12345abc6789
# ^^^^^ |-|
#echo ‘${var1##$pattern1} =’ “${var1##$pattern1}” # 6789
# Longest possible match, strips out first 12 characters abcd12345abc6789
# ^^^^^ |———-|
fi
done
exit 0

I also want include this:

match=feat versus (radio)
substitute=ft. vs. (radio_edit)

newname=”$(echo ${file#$pattern1} | sed ‘s/’$match’/’$substitute’/g’)”

willem versus wca feat wasd – hello world (radio)
willem_vs._wca_ft.wasd-hello_world-(radio_edit)

So i can as much items to change as i want.
Hope someone can help me out! :-)

Im sick and tired of using windows based tools in wine to do this, while bash is even stronger and more fun to learn ;-)

Reply

61 shaded January 30, 2011

wow this is a popular post for good reason. I wonder if its still active.

Can this descend in to directories? I’d like to rename some image files with the following file and directory structure.

01/01-0012.jpg
01/01-0342.jpg
02/02-3495.jpg
…..
98/34-0424.jpg

so i would like to rename them to
01/01-001.jpg
01/01-034.jpg
02/02-349.jpg
…..
98/34-042.jpg

So i really just want to cut the last number before the .jpg but the files literally span 99 directories, so don’t want to do it for each one. Any ideas?

Reply

62 vogueestylee May 30, 2011

hi, I need to add a letter into file name in many files but not using automator, is it possible?

for example: I have files like 1.png, fs.png, ghasd.png and need to add “-hd” so the name will be 1-hd.png fs-hd.png, and original files will be not deleted, how to do that? :) thanx!

Reply

63 Muhammad El-Sergani June 20, 2011

you can do the following:
# rename .png -hd.png *.png

How about that? :)
Let me know if you need any further help

Reply

64 Muhammad El-Sergani June 20, 2011

Excellent write up!!
Thank you sir…

Been browsing the internet for a while for this, and just came across this neat little command, instead of people writing different scripts and such.. AWESOME!!

Reply

65 Vijay June 30, 2011

$ rename .bak .txt *.bak – command worked for me to rename all *.bak to *.txt

Many Thanks

Reply

66 Chary August 20, 2011

Very Helpful. Thank You :)

Reply

67 koma931 August 26, 2011

On OSX I didn’t have rename handy, so I came up with this to change extensions of a bunch of files:
find . -name “*.ext1″ | sed s/ext1// | xargs -I % mv %.ext1 %.ext2

I imagine it could be done in a more simple fashion, but it works well if you need to swap something at the end or the beginning of the filenames.

Reply

68 swain September 6, 2011

Hi, I am trying to rename some files with .ext to ones without .ext in RedHat 5

For eg: files like “ocean_11h20110906.txt” have to renamed as “ocean_11h20110906″

tried almost all the commands mentioned above. i have several such files to be renamed without extensions. Can anyone help me out. Plz treat this as urgent.

Reply

69 Gary September 6, 2011

This is a very usefule and informative thread. However, i haven’t quite got exactly how how I would do my renaming. I need to rename daily files from format
XX_AA1234_201109.txt to AA1234_201109_XX.ext. Note that I need to change both position of a string and the extension itself. Any suggestions would be highly appreciated!

Reply

70 paxpacis September 7, 2011

@Gary
rename XX_ “” *.txt; rename .txt _XX.ext *.txt

@VIVEK
I think your example about the AXEL-CRAZYFROG.mp3 file is not correct. The – will not be added with your code. I think you forgot to include the hypen in the original name.

Reply

71 Gary September 12, 2011

@paxpacis-Appreciate your input. however, I don’t think I understand your solution. Here are some example files:
AF_DD2795_199905_20110824.txt should be renamed to DD2795_199905_20110824_AF.raw
AF_DD2795_199905_20110924.txt should be renamed to DD2795_199905_20110924_AF.raw
How do I automate this?

Reply

72 Lord Jaguar October 16, 2011

Hey,

Thanks for the tip, but the rename command didnt work on my Fedora 15 x64. I didnt get any error message, but the spaces didnt go ! As one suggested above, does it work only on debian systems ? I checked man rename, there is no info on usage. Just has description of the -v switch. I am using “rename (util-linux 2.19.1)”

Thanks !

Reply

73 LinuxFTW November 25, 2011

i=0; for image in *.jpg; do mv “$image” “Name with spaces `printf “%.3d” $i`.jpg”; ((i++)); done

Renames:

asdlkjas.jpg
reojsd.jpg
asfkjdf.jpg

to

Name with spaces 000.jpg
Name with spaces 001.jpg
Name with spaces 002.jpg

Reply

74 Bummer December 13, 2011

“Figure 03-01-02: GradeBook class declaration with one method: Createing a Gradebook object an calling its displayMessage method.avi”
How can we bulk rename files like this? These are located in subfolders like
“Blaa: blaaaaah: bla bla: bla” etc.

None of the posted above methods not working. :(
I need replace all these stupid “:”.

Reply

75 Nasp December 23, 2011

Well god points, but how about this :

rename muiltiple files in subfolders… (.nfo to movie.nfo )

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 14 + 6 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: