Howto: Linux Rename Multiple Files At a Shell Prompt

by on November 26, 2005 · 115 comments· LAST UPDATED February 24, 2012

in , ,

From my mailbag:

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 

The rename command discussed here is nothing but a perl script. You can download the script as described below.

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 


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

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



Linux Shell script to rename files

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

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

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

Perl Script To Rename File

Download the following script and save as rename in /usr/local/bin/ directory or $HOME/bin and run it as follows:
$ ~/bin/rename 'y/A-Z/a-z/' *
$ /usr/local/bin/rename 'y/A-Z/a-z/' *
Perl script (download):

#!/usr/bin/perl -w
#  This script was developed by Robin Barker (,
#  from Larry Wall's original script eg/rename from the perl source.
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
# Larry(?)'s RCS header:
#  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30 
# $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
# $Log: rename,v $
# Revision 1.5  1998/12/18 16:16:31  rmb1
# moved to perl/source
# changed man documentation to POD
# Revision 1.4  1997/02/27  17:19:26  rmb1
# corrected usage string
# Revision 1.3  1997/02/27  16:39:07  rmb1
# added -v
# Revision 1.2  1997/02/27  16:15:40  rmb1
# *** empty log message ***
# Revision 1.1  1997/02/27  15:48:51  rmb1
# Initial revision
use strict;
use Getopt::Long;
my ($verbose, $no_act, $force, $op);
die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
	'v|verbose' => \$verbose,
	'n|no-act'  => \$no_act,
	'f|force'   => \$force,
    ) and $op = shift;
$verbose++ if $no_act;
if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
	warn  "$was not renamed: $_ already exists\n";
    elsif ($no_act or rename $was, $_)
	print "$was renamed as $_\n" if $verbose;
	warn  "Can't rename $was $_: $!\n";
=head1 NAME
rename - renames multiple files
B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>
renames the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr>
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
If no filenames are given on the command line, filenames will be read
via standard input.
For example, to rename all files matching C<*.bak> to strip the extension,
you might say
	rename 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
	rename 'y/A-Z/a-z/' *
=head1 OPTIONS
=over 8
=item B<-v>, B<--verbose>
Verbose: print names of files successfully renamed.
=item B<-n>, B<--no-act>
No Action: show what files would have been renamed.
=item B<-f>, B<--force>
Force: overwrite existing files.
No environment variables are used.
=head1 AUTHOR
Larry Wall
=head1 SEE ALSO
mv(1), perl(1)
If you give an invalid Perl expression you'll get a syntax error.
=head1 BUGS
The original C<rename> did not check for the existence of target filenames,
so had to be used with care.  I hope I've fixed that (Robin Barker).

Page last updated at 5:43 AM, February 18, 2012.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 115 comments… read them below or add one }

1 Jose November 9, 2007 at 2:01 am

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


2 tcw March 16, 2008 at 5:25 pm

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


3 kamran in ciit and haji shahid sahab and haji shehriyar April 5, 2011 at 6:58 am


cd $1
for i in `ls $2`
mv $i file{$j}.txt
j=`expr $j + 1`
press esc

chmod +x
./ ~/file *.txt


4 JOHN April 5, 2011 at 7:01 am



5 leopinzon May 12, 2008 at 4:35 pm


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


6 zougi January 17, 2012 at 2:55 pm

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/” {} \;


7 marco September 3, 2008 at 2:49 am

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


8 mikoto September 10, 2008 at 9:57 am

Wow, thank you!


9 fatma November 18, 2008 at 2:40 pm

Thanks! Did help a lot!


10 Ankur February 6, 2009 at 9:27 am

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


11 bassam March 10, 2009 at 11:24 pm

but how i can rename
thanks again


12 Scott July 20, 2011 at 8:11 pm

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

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


13 Name March 11, 2009 at 8:41 pm

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


14 diepes March 19, 2009 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.


15 Luka March 23, 2009 at 2:12 pm

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


and I want to rename them to get the following:

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


16 Aarthi June 8, 2011 at 12:11 pm

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.


17 Kalleguld October 12, 2011 at 3:29 pm

A little late , but here goes:


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


18 Paul Feakins March 24, 2009 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 ;)



19 Aarthi June 8, 2011 at 12:10 pm

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…


20 Zinc June 11, 2011 at 11:23 am

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


21 Henry April 30, 2009 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)


22 nixCraft April 30, 2009 at 7:05 pm

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


23 Henry April 30, 2009 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


24 Owen February 1, 2011 at 5:58 pm


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.


25 V.Balaviswanathan May 5, 2009 at 2:11 pm

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


26 Henry May 29, 2009 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 and using that, my regex above works as desired.


27 Callum December 23, 2010 at 2:18 pm

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


28 newbie June 5, 2009 at 12:41 pm

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

to :-


29 b0nUx3R June 30, 2009 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 :)


30 b0nUx3R June 30, 2009 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


31 Richard July 2, 2009 at 9:02 am

abd now his one :






32 Natan July 14, 2009 at 10:02 am

so :

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


33 miguel July 18, 2009 at 11:20 am

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


34 miguel July 18, 2009 at 11:20 am

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


35 konstantin July 20, 2009 at 3:17 pm

hello does somebody knows how to rename the following??



thanks a lot in advance!!



36 SHARYAR AND KAMRAN March 25, 2011 at 11:41 am

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


37 pir July 24, 2009 at 1:34 pm

rename ‘s/konstantin/georg/’*


38 JimmyNY July 30, 2009 at 6:03 am

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…


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.


39 Sakiwi October 7, 2011 at 12:52 pm

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


40 pd August 4, 2009 at 12:32 am

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.


41 Wonky September 2, 2009 at 9:35 pm

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


42 sheeep March 19, 2010 at 2:04 pm



43 strAlan April 5, 2010 at 10:54 am

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


44 khedron May 15, 2010 at 10:27 am

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


45 jingo June 3, 2010 at 7:30 am

cat OPs_taste_in_music > /dev/null


46 vx11 July 10, 2010 at 1:00 pm

thanks…, good information


47 Tarique July 27, 2010 at 6:52 am

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


Report July 2010.xls


48 gazmanic August 26, 2010 at 5:59 am

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


49 gazmanic August 26, 2010 at 7:47 am

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.


50 ejoftiduttu August 28, 2010 at 8:16 am


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


51 nixCraft August 28, 2010 at 8:45 am
for j in *.jpg; do mv "$j" "$i.jpg" ;(( i++ )); done

Backup files before running above code.


52 Jake December 13, 2010 at 8:10 pm

In this command:
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…..


53 Maarten September 2, 2010 at 8:07 am

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:

#rename script
rename ‘s/text/Text/’ *

it works at the following way: $./

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

can somebody help me out how to use variables.


54 Tarique September 5, 2010 at 4:31 am

Adjust this with your script and then try -



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

Then Run-

$./ text Text


55 Shang October 1, 2010 at 7:42 am

how to rename -file to file?


56 willem19 November 4, 2010 at 12:00 pm


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?


57 André November 22, 2010 at 2:52 pm

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


58 willem19 December 19, 2010 at 1:59 pm

This only removes *.mp3 :-(


59 Jake December 13, 2010 at 8:13 pm

using this command:
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…


60 willem19 December 19, 2010 at 2:12 pm

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 -

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:
# Pattern matching using the # ## % %% parameter substitution operators.
pattern1=*-* # * (wild card) matches everything between.
for file in ls *.mp3; do
if [ -e "$file" ]
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!
#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
# ^^^^^ |———-|
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)

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


61 shaded January 30, 2011 at 1:42 am

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.


so i would like to rename them to

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?


62 vogueestylee May 30, 2011 at 1:34 pm

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!


63 Muhammad El-Sergani June 20, 2011 at 11:50 am

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

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


64 vogueestylee February 24, 2012 at 8:43 am

thank you, I will try :)


65 Muhammad El-Sergani June 20, 2011 at 10:23 am

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


66 Vijay June 30, 2011 at 9:37 am

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

Many Thanks


67 Chary August 20, 2011 at 6:04 am

Very Helpful. Thank You :)


68 koma931 August 26, 2011 at 9:29 am

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.


69 swain September 6, 2011 at 5:44 am

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.


70 Henry February 17, 2012 at 6:36 pm
71 Gary September 6, 2011 at 4:01 pm

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!


72 paxpacis September 7, 2011 at 12:58 pm

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

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.


73 Gary September 12, 2011 at 6:38 pm

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


74 Lord Jaguar October 16, 2011 at 8:15 am


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 !


75 Henry February 17, 2012 at 6:37 pm
76 LinuxFTW November 25, 2011 at 6:25 am

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




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


77 Bummer December 13, 2011 at 1:53 pm

“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 “:”.


78 Nasp December 23, 2011 at 9:03 pm

Well god points, but how about this :

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


79 praveen February 16, 2012 at 6:50 pm

Thanks for the info.

rename doesnt seem to be recursive

For recursive do this, to rename .html to .htm.bak. Replace . with your dir name if you want to replace files not in current directory

find . -name “*.html” | xargs rename .html .htm.bak


80 zaphod March 13, 2012 at 5:13 pm

my trials to rename files from pic_1.jpg pic_2.jpg … to pic_1500.jpg 1501.jpg … failed :-(

Hope to get some help


81 sakiwi March 13, 2012 at 8:04 pm

rename -n ‘s/pic_(\d+)/”pic_” . ( $1 + 1500 )/e’ *.jpg


82 zaphod March 14, 2012 at 6:56 pm

Thanks for trying to help me. If I do:
rename -n ‘s/pic_(\d+)/”pic_” . ( $1 + 1500 )/e’ *.jpg
then I get:
Unrecognized character \xE2 in column 5 at (eval 1) line 1.



83 sakiwi March 14, 2012 at 7:58 pm

It works for me, what linux are you using and which version of util-linux are you using? Mine is:
user@host > lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 10.04.4 LTS
Release: 10.04
Codename: lucid
user@host > dpkg -l | grep util-linux
util-linux 2.17.2-0ubuntu1.10.04.2

The other thing to make sure is that the quotes are exactly as I used, single quotes (‘) to enclose the whole command and double-quotes (“) to enclose the pic_ part.


84 zaphod March 15, 2012 at 10:21 pm

Hi Sakiwi,

it’s working now :-) Indeed the double-quotes were the problem. I didn’t know that there are different kinds of double-quotes existing in the world.

Many Thanks and best regards, zaphod !!!


85 sakiwi March 15, 2012 at 10:26 pm

Glad it is working for you now, and glad I could help.


86 gabby March 16, 2012 at 6:38 pm

How to add a prefix to original file names..
eg: moon.jpg –> Pic_moon.jpg
flowers.jpg –> Pic_flowers.jpg


87 SaKiwi March 16, 2012 at 9:58 pm

rename ‘s/(.*)\.jpg/Pic_$1\.jpg/’ *jpg

Or if you files don’t have spaces in the names you could also do:
for files in *jpg; do mv “$files” “Pic_$files”; done


88 tony April 1, 2012 at 11:57 am

For FreeBSD, the syntax is slightly different:

rename -s/oldtext/newtext/ *.ext

will replace oldtext with newtext in all files of type ext

rename -s/\ /_/ *.ext

will replace spaces with underscores


89 tony April 1, 2012 at 12:00 pm

s/in all files/in the names of all files/g



90 Ethan May 29, 2012 at 5:29 am

Copy one source file to different file name.

echo “$(cat $FOLDER_PATTERN_LIST)” | while read line; do
echo “cp test.pdf $LISTLINE”


cp test.pdf File1
cp test.pdf File2
cp test.pdf File3


91 Michael May 30, 2012 at 6:15 pm

Hi there. Would love some help on this:


to become…


Thank you for any suggestions!


92 nixCraft May 30, 2012 at 8:07 pm


 ## first make backup of files ##
for f in *
 set -- $f
 mv "$1.$2.$3" "$1.$3.$2"


93 sakiwi May 30, 2012 at 8:59 pm

rename ‘s/(.*)\.(.*)\.(\d+)/$1.$3.$2/’ *.[0-9]


94 Cr0t June 25, 2012 at 3:02 pm


I did something similar. After I converted my daugther’s shows for her touchpad the old avi extension where still in the filename, which I had/wanted to remove.

The touchpad doesn’t have `rename` so this is what I did…


find . -name ‘*.mp4′ >> /tmp/$$
while read line; do
foo=`echo $line | sed s/.avi//g`
mv $line $foo
done < /tmp/$$

rm /tmp/$$


95 Ben Altman July 19, 2012 at 4:28 pm

Anyone remember about brace expansion?
mv file.{foo,bar}
will expand to:

Or in a loop:
# removes the foo extension before the expansion:
for file in *.foo; do mv ${file%foo}.{foo,bar}; done

A lot less involved than some of the examples here.


96 Ralph Eichelberger September 12, 2012 at 2:05 pm

The date of my phone was reset to 2009 some day after removing the battery and I just set the time. So after some weeks I uploaded my fotos to my PC and got all the pictures with the wrong date as a file name. Here is the script that corrected that issue at once:

Please make a backup before you use it. This comes without any warranties of course.

for  filename in `ls 2009*` ; do mv $filename `date --date="${filename:0:10}+1301 days" +"%Y-%m-%d"`${filename:10}; done


97 dntel November 9, 2012 at 7:44 am

hey if I want to rename a folder of files which have names like:
artist – album – 01 song
artist – album – 02 song
and I want to rename them to
01 – song
02 – song
how do I do this? Have been struggling to adjust the examples given here

Kind regards


98 sakiwi November 11, 2012 at 7:39 pm

rename -v ‘s/.* – .* – (\d\d) (.*)/$1 – $2/’

The brackets tell rename that those are portions you want to keep/reuse in the replace portion and are referenced by $1, $2, $3 etc… So in your case it keeps the 2 digits “(\d\d)” as $1 and then a space and then everything else “(.*)”.

It will then in the second part of the regex rename the file as \d\d – .*
i.e. 01 – song, 02 – song, 03 – song etc…


99 mert November 11, 2012 at 10:58 pm

Hello ,

Can anyone assist me with following replacement

avatarXXX_1.gif => photo-XXX.gif

I tried

rename -n ‘s/avatar(\d+)_1\.gif/photo-$1\.gif’ *gif

But it didn’t work


100 Ben Altman November 12, 2012 at 3:52 pm

for i in *; do mv $i photo-${i#avatar}; done


101 sakiwi November 12, 2012 at 7:27 pm

mert – This works for me:
rename -n ‘s/avatar(\d+)_.*\.gif/photo-$1\.gif/’ *.gif
–> avatar123_1.gif renamed as photo-123.gif
–> avatar234_5.gif renamed as photo-234.gif
–> avatar4565_125.gif renamed as photo-4565.gif


102 mert November 12, 2012 at 7:45 pm

It’s strange but it doesn’t function at all for me. Now time to debug what is wrong with it.

[root@test tet]# rename -n ‘s/avatar(\d+)_.*\.gif/photo-$1\.gif/’ *.gif
[root@test tet]# ls
avatar605323_2.gif avatar605379_1.gif avatar605425_2.gif avatar605443_1.gif
avatar605336_1.gif avatar605386_2.gif avatar605427_1.gif
[root@test tet]# rename ‘s/avatar(\d+)_.*\.gif/photo-$1\.gif/’ *.gif
[root@test tet]# ls
avatar605323_2.gif avatar605379_1.gif avatar605425_2.gif avatar605443_1.gif
avatar605336_1.gif avatar605386_2.gif avatar605427_1.gif

[root@test tet]# rename –help
call: rename from to files…


103 sakiwi November 12, 2012 at 7:58 pm

mert – are you using the “util-linux” rename utility? I am using ubuntu, and this is the details of my “rename” utility:
clive@dogmatix > which rename
clive@dogmatix > apt-file search /usr/bin/rename
ladr4-apps: /usr/bin/renamer
util-linux: /usr/bin/rename.ul
clive@dogmatix > dpkg -l | grep util-linux
ii util-linux 2.20.1-1ubuntu3 Miscellaneous system utilities

So if you are using an ubuntu derivate system then install the package:
clive@dogmatix > sudo apt-get install util-linux


104 mert November 12, 2012 at 4:07 pm

Hello Adam ,

It functions up to a level
but it converts the file as photo-XXX_1.gif

Is there any way to get rid of _1 ?


105 Carry November 29, 2012 at 6:19 pm

I want to rename file from model1 model2….model 50 as model100 model102…model150…I tried codes similar to above but didnt work…


106 Enshala April 26, 2013 at 3:22 pm

From the command line, this worked for me:

find -name ‘*.lst’ -exec rename .lst a.lst {} \;

That is, I changed:




I’m running Scientific Linux 6.


107 jui April 29, 2013 at 2:28 am

dear owner this site i’m thai people can write english a bit

i have muti files start with number 3 digit and 1 char is underscore

145_เธšเธฑเธ™เธ—เธถเธเธ‚เน‰เธญเธ„เธงเธฒเธก manage1.doc
147_เธšเธฑเธ™เธ—เธถเธเธ‚เน‰เฒเธก manage5.pdf
149_เธšเธฑเธ™เธเธ‚เน‰เธญธงเธฒเธก manage2.ppt

i want to follow this

Help me please.


108 Kasumi Miyano June 25, 2013 at 2:35 pm

It is to mention, that `rename’ and `perl-rename’ are not the same thing!

rename is a part of the util-linux Package.

Heres the man-page(Arch/ util-linux 2.23.1)
rename – rename files

rename [options] expression replacement file…

rename will rename the specified files by replacing the first occur‐
rence of expression in their name by replacement.

-v, –verbose
Give visual feedback which files where renamed, if any.

-V, –version
Display version information and exit.

-s, –symlink
Peform rename on symlink target

-h, –help
Display help text and exit.

Given the files foo1, …, foo9, foo10, …, foo278, the commands

rename foo foo0 foo?
rename foo foo0 foo??

will turn them into foo001, …, foo009, foo010, …, foo278. And

rename .htm .html *.htm

will fix the extension of your html files.

The renaming has no safeguards. If the user has permission to rewrite
file names, the command will perform the action without any questions.
For example, the result can be quite drastic when the command is run as
root in the /lib directory. Always make a backup before running the
command, unless you truly know what you are doing.

mmv(1), mv(1)

The rename command is part of the util-linux package and is available

It is a binary. The Arch package is called perl-rename:
rename – renames multiple files

rename [-bfilnv] [-B prefix] [-S suffix] [-V method] [-Y prefix] [-z
suffix] [--backup] [--basename-prefix=prefix] [--dry-run] [--force]
[--help] [--interactive] [--just-print] [--link-only] [--prefix=prefix]
[--suffix=suffix] [--verbose] [--version-control=method] [--version]
perlexpr [files]…

rename renames the filenames supplied according to the rule specified
as the first argument. The argument is a Perl expression which is
expected to modify the $_ string for at least some of the filenames
specified. If a given filename is not modified by the expression, it
will not be renamed. If no filenames are given on the command line,
filenames will be read via standard input.

If a destination file is unwritable, the standard input is a tty, and
the -f or –force option is not given, mv prompts the user for whether
to overwrite the file. If the response does not begin with `y’ or `Y’,
the file is skipped.

-b, –backup
Make backup files. That is, when about to overwrite a file, rename
the original instead of removing it. See the -V or
–version-control option fo details about how backup file names are

-B prefix, –prefix=prefix
Use the simple method to determine backup file names (see the -V
method or –version-control=method option), and prepend prefix to a
file name when generating its backup file name.

-f, –force
Remove existing destination files and never prompt the user.

-h, –help
Print a summary of options and exit.

-i, –interactive
Prompt whether to overwrite each destination file that already
exists. If the response does not begin with `y’ or `Y’, the file
is skipped.

-l, –link-only
Link files to the new names instead of renaming them. This will
keep the original files.

-n, –just-print, –dry-run
Do everything but the actual renaming, insted just print the name
of each file that would be renamed. When used together with
–verbose, also print names of backups (which may or may not be
correct depending on previous renaming).

-v, –verbose
Print the name of each file before renaming it.

-V method, –version-control=method
Use method to determine backup file names. The method can also be
given by the RENAME_VERSION_CONTROL (or if that’s not set, the
VERSION_CONTROL) environment variable, which is overridden by this
option. This option does not affect wheter backup files are made;
it affects only the name of any backup files that are made.

The value of method is like the GNU Emacs `version-control’
variable; rename also recognize synonyms that are more descriptive.
The valid values are (unique abbreviations are accepted):

existing or nil
Make numbered backups of files that already have them,
otherwise simple backups. This is the default.

numbered or t
Make numbered backups. The numbered backup file name for F is
F.~N~ where N is the version number.

simple or never
Make simple backups. The -B or –prefix, -Y or
–basename-prefix, and -z or –suffix options specify the
simple backup file name. If none of these options are given,
then a simple backup suffix is used, either the value of
SIMPLE_BACKUP_SUFFIX environment variable if set, or ~

Print version information on standard output then exit

-Y prefix, –basename-prefix=prefix
Use the simple method to determine backup file names (see the -V
method or –version-control=method option), and prefix prefix to
the basename of a file name when generating its backup file name.
For example, with -Y .del/ the simple backup file name for a/b/foo
is a/b/.del/foo.

-z suffix, -S suffix, –suffix=suffix
Use the simple method to determine backup file names (see the -V
method or –version-control=method option), and append suffix to a
file name when generating its backup file name.

To rename all files matching *.bak to strip the extension, you might

rename ‘s/\e.bak$//’ *.bak

To translate uppercase names to lower, you’d use

rename ‘y/A-Z/a-z/’ *

More examples:

rename ‘s/\.flip$/.flop/’ # rename *.flip to *.flop
rename s/flip/flop/ # rename *flip* to *flop*
rename ‘s/^s\.(.*)/$1.X/’ # switch sccs filenames around
rename ‘s/$/.orig/ */*.[ch]‘ # add .orig to source files in */
rename ‘y/A-Z/a-z/’ # lowercase all filenames in .
rename ‘y/A-Z/a-z/ if -B’ # same, but just binaries!
or even
rename chop *~ # restore all ~ backup files

Two environment variables are used, SIMPLE_BACKUP_SUFFIX and

mv(1) and perl(1)

If you give an invalid Perl expression you’ll get a syntax error.

Peder Stray , original script from Larry Wall.


109 Johnny June 26, 2013 at 7:31 am

Thanks for the rename command to me, but for my need it had limitations.

My requirement: Rename multiple files under a folder tree like below:


All above files should get renamed, by inserting ‘old’ after “d/”.

I achieved above feat with single find command.

You should be at the parent folder of the folder tree, then it will work as below:

$ find . -name ‘*ITM*’ | xargs rename d\/ d\/old
$ find . -name ‘*ITM*’

It worked! Hope it will be helpful in such complex needs.
Please feedback if any issues faced.


110 wayne July 14, 2013 at 4:33 pm

My problem was that when copying files from linux to Windows, Windows would complain about the filenames that were longer than what Windows can handle.
I tried a few of the examples posted here previously but they didn’t work for me either (I’m running Fedora 17 64-bit) so I wrote my own … Copy the following text to a plain file and save it as a shell script – don’t forget to make it executable (chmod a+x “”)

#start script
for filename in *.jpg
fname=`echo $filename | cut -c1-137`;
rename $fname $nname *.jpg

So what I’m doing is looking at everything up to the 137th position in the filename that I want to work with as the repeating pattern, then assigning my new file name pattern in the variable “nname”. The result is a shorter file name (also in numeric sequence) that Windows can handle. Run this script in the same directory as the files. Hope this helps someone else.


111 ben altman July 18, 2013 at 2:09 am

i would firstly quote filename as windows typically will have spaces in the path causing problems. Also, if you are using bash or ksh you can cut a variable doing this:
using the shell instead of a utility.


112 mohsen September 16, 2013 at 2:46 pm

To change all file with ‘php’ format to ‘jpg’ in a folder:

rename ‘s/\.php$/\.jpg/’ *.php


113 javi October 23, 2013 at 12:54 pm

for i in `ls`; do `mv $i $i.orig`; done


114 bala May 14, 2014 at 6:58 am

ls * | while read line; do mv $line $line.txt; done
will rename all files in that folder to .txt extension


115 harry May 16, 2014 at 3:12 pm

Hi I need help, i am using cygwin & trying to copy & create multiple files in a sequence eg :-

filename_1234609 I need to duplicate this file muliple times creating an incremental sequence number

al the way to filename_1234700

can anyone help ?/

I am trying to cp a file & rename with a sequence


Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: