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.
- Email this to a friend
- Printable version
- Rss Feed
- Last Updated: Nov/9/2007

{ 21 comments… read them below or add one }
Very Good job dude with that examples.. was helpfull thanks
The ‘rename’ command seems not working for me.. :(
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/"I knew only the crapy mv method, i never thought that linux has a “rename”. :P Thanks! :)
Wow, thank you!
Thanks! Did help a lot!
thanx a ton……its amazing, i though linux dont hav rename……but does.
Thanks…
but how i can rename
foo1.a,foo2.a,…,foo10.a
To
xxx1.a,xxx2.a,….,xxx10.a
???
thanks again
rename ’s/foo/xxx/’ foo*.a
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.
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.)
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.
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)
It should work 2.13 was around for sometime and 2.14 is latest. Can paste your example here..
Example file name: 2001DODGE3500546424-10_12_1.JPG
Desired file name: 2001DODGE3500546424-10.jpg
Command:
rename "s/(_[0-9]+)+\.JPG/.jpg/" *.JPGCool one… Thanks a lot… Good job… Keep this good work going :)
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.
how can we rename 100 files in folder
1 fooo1.txt
2 fooo2.txt
3 fooo3.txt
….
to :-
1fooo1.txt
1fooo2.txt
1fooo3.txt
…….
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 :)
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
abd now his one :
AAAAAAB0020090617173034173034AAAA21_-_0001329515.txt.gz
into
AAAAAAB0020090617173034AAAA21_-_0001329515.txt.gz
anyone?