[Gllug] Swapping two parts of a filename...

Pete Ryland pdr at createservices.com
Mon Jun 7 10:03:43 UTC 2004


On Mon, Jun 07, 2004 at 01:43:17AM +0100, Russell Howe wrote:
> On Wed, Jun 02, 2004 at 10:59:19PM +0100, Dylan wrote:
> > Hi All,
> > 
> > I have a bunch of mp3's with filenames like:
> > 
> > Track Name - Artist Name.mp3
> > 
> > which I'd really rather like as
> > 
> > Artist Name - Track Name.mp3
> > 
> > Is there an easy way to accomplish this? I also need to handle cases 
> > where the track name or artist name contains a hyphen (in which case, 
> > it would have a non-space character on each side.)
> 
> If you save this as a shell script...

There's also a "rename" command that comes with perl.

If you do "man rename" and you have a command with the following SYNOPSIS:

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

Then you have the right rename installed.

Just do (untested):

rename 's/^([^ -]*) ?- ?([^.]*)(.*)$/$2 - $1$3/' *.mp3

To explain the regex briefly:

s/.../.../      search/replace
^               start of string
(...)           remember this
[^ -]           match anything *except* space or dash
[^ -]*          match zero or more of the above
 ?              match zero or one space
.               match any character
.*              match zero or more anythings
$               match end of string
$2              insert second remembered string

Pete
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list