nixCraft Poll

Topics

Howto Linux rename multiple files at a shell prompt

Posted by Vivek Gite [Last updated: November 9, 2007]

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:

Discussion on This Article:

  1. Jose Says:

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

  2. tcw Says:

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

  3. leopinzon Says:

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

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.