LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-26-2007, 02:54 PM   #1
maker10
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:

Iron_Maiden_-_11_-_Juanita.mp3
Iron_Maiden_-_11_-_My_Generation.mp3
Iron_Maiden_-_13_-_Black_Bart_Blues.mp3
Iron_Maiden_-_16_-_Wasted_Years_'99_(Live).mp3
Iron_Maiden_-_15_-_Charlotte_The_Harlot_'88.mp3
Iron_Maiden_-_10_-_Judgement_Day.mp3
Iron_Maiden_-_14_-_Prowler_'88.mp3

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:

11-Juanita.mp3
11-My_Generation.mp3

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

Thanks for the help.

George
 
Old 10-26-2007, 04:13 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

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

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

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, 04:16 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,397
Blog Entries: 1

Rep: Reputation: 64
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.

Code:
for f in Iron_Maiden*.mp3; do 
 new=$(echo $f | sed -e 's/Iron_Maiden_-_[0-9]*_-_//'); 
 echo mv "${f}" "${new}"; 
done
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, 05:30 PM   #4
PMorph
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 213

Rep: Reputation: 31
One more:
Code:
rename 's/Iron_Maiden_-_//' *.mp3
rename 's/_-_/-/' *.mp3
 
Old 10-27-2007, 12:25 AM   #5
m.a.l.'s pa
Member
 
Registered: Oct 2007
Location: albuquerque
Posts: 220

Rep: Reputation: 50
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, 08:00 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,397
Blog Entries: 1

Rep: Reputation: 64
Quote:
Originally Posted by PMorph View Post
One more:
Code:
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, 10:24 AM   #7
dancro
LQ Newbie
 
Registered: Oct 2003
Location: Carlsbad, California
Distribution: Ubuntu 8.04 server: mythbuntu 8.04
Posts: 15

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

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

Rep: Reputation: 55
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
 
  


Reply


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 02:54 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 01:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 07:06 AM
Multiple File Rename windisch Linux - General 11 11-10-2005 01:19 PM
Multiple file rename jrdioko Linux - Newbie 10 11-07-2004 06:25 PM


All times are GMT -5. The time now is 06:04 AM.

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