Howto: Linux Rename Multiple Files At a Shell Prompt

by LinuxTitli · 29 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. 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

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 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:
man rename

Featured Articles:

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!

{ 29 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?

22 Natan 07.14.09 at 10:02 am

so :

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

23 miguel 07.18.09 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..)

24 miguel 07.18.09 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..)

25 konstantin 07.20.09 at 3:17 pm

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

26 pir 07.24.09 at 1:34 pm

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

27 JimmyNY 07.30.09 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…

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.

28 pd 08.04.09 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.

29 Wonky 09.02.09 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

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>

Previous post:

Next post: