Howto Linux rename multiple files at a shell prompt
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.
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Best Linux / UNIX Posts of 2007: Part ~ IV
- Upgrade wordpress quickly in 3 easy steps from UNIX shell prompt
- How Do I Burn MP3 onto an audio CD from Linux shell command prompt?
- Tutorial: Working with UNIX and Linux Shell
- How to Publish Multiple Websites Using a Single Tomcat Web Application
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



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