Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-26-2007, 01:54 PM   #1
LQ Newbie
Registered: Aug 2003
Posts: 15

Rep: Reputation: 0
Multiple File Rename

Hi All,

I'm trying to rename multiple files in a directory that are named as such:


What I would like to do is remove up to the Iron_Maiden_-_14_-_ and rename them to start with the track number and then finish with the name as such:


I've looked on the web and I'm completely baffled by the all the info out there.

Thanks for the help.

Old 10-26-2007, 03:13 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Something like the following should do it:

cd directory
for file in `ls -1`
do newfile=`echo $file | awk -F_ '{print $4"-"$6}'`
echo mv $file $newfile

In above change "directory" to the actual directory name where your music files are.

What it does is uses the underscore (_) as the delimiter for awk and prints the 4th and 6th and field as split by that delimiter. It puts the literal dash (-) in between those.
Old 10-26-2007, 03:16 PM   #3
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501
Blog Entries: 2

Rep: Reputation: 68
It is not really difficult, you just need to known a bit of regular expressions and bash...I hope this motivate you to learn more about.

for f in Iron_Maiden*.mp3; do 
 new=$(echo $f | sed -e 's/Iron_Maiden_-_[0-9]*_-_//'); 
 echo mv "${f}" "${new}"; 
Please, note there is a echo in front of the mv command. This is a debug/safe option. run this as is just to see what is the output. If everything looks good, remove the echo command and the files will be renamed.
Old 10-26-2007, 04:30 PM   #4
Registered: Sep 2003
Distribution: Debian
Posts: 213

Rep: Reputation: 31
One more:
rename 's/Iron_Maiden_-_//' *.mp3
rename 's/_-_/-/' *.mp3
Old 10-26-2007, 11:25 PM   #5
m.a.l.'s pa
Registered: Oct 2007
Location: albuquerque
Distribution: Debian, Arch, Kubuntu
Posts: 366

Rep: Reputation: 139Reputation: 139
The applications gwenrename and krename work well with Konqueror and/or Krusader for doing bulk renaming of files.

The light-weight file browser Thunar comes with a nice GUI for doing the same thing, called Bulk Rename.
Old 10-30-2007, 07:00 AM   #6
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501
Blog Entries: 2

Rep: Reputation: 68
Originally Posted by PMorph View Post
One more:
rename 's/Iron_Maiden_-_//' *.mp3
rename 's/_-_/-/' *.mp3
strange, but the rename command installed in my system does not have this sed-like feature. It is not mentioned in the rename's man page either...Mine comes from util-linux-2.13-0.54.1.fc7, and yours ?
Old 11-06-2007, 09:24 AM   #7
LQ Newbie
Registered: Oct 2003
Location: Carlsbad, California
Distribution: Ubuntu 8.04 server: mythbuntu 8.04
Posts: 15

Rep: Reputation: 0
I tried this the way you described and it worked beautifully.
Thank you, and all the others who contribute help so freely.

Old 11-06-2007, 09:32 AM   #8
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
I also have this very cool "sed feature" of rename which I discovered not so long ago.

/usr/bin/rename is linked to /etc/alternatives/rename which is linked to /usr/bin/prename

prename is part of perl:

perl 5.8.8-11.1 Larry Wall's Practical Extraction and Report


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 01:54 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM
Multiple File Rename windisch Linux - General 11 11-10-2005 12:19 PM
Multiple file rename jrdioko Linux - Newbie 10 11-07-2004 05:25 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration