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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email this to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: Oct/27/2009



{ 29 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?
so :
for i in *.txt.gz; do mv $i `echo $i |cut -c1-23,30-55` ; done
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..)
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..)
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
rename ’s/konstantin/georg/’ konstantin.bcn*
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.
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.
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