Help renaming files

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

Help renaming files

Postby pk_fox » Wed Dec 18, 2013 7:42 am

Hi all, I've just downloaded a lot of Mp3 files from Amazon and they have names like this: B00EXWBVRM_(disc_1)_09_-_Why'd_You_Only_Call_Me_When_You're_.mp3, anyone know a quick and dirty ( or slow and elegant ) way to strip all the crap off the name leaving e.g. Why'd_You_Only_Call_Me_When_You're_.mp3, from what I can see the name is always preceded by two underscores, I could code something but I'm sure one of you knows a command line solution. Many thanks
We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby nelz » Wed Dec 18, 2013 7:51 am

You can use the coreutils rename, the more powerful rename from http://rename.berlios.de/ or a combination of mv and sed (there's also mmv but I've not tried that). But the easiest to get to grips with is probably Krename.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby johnlane » Sun Dec 22, 2013 8:15 pm

One way to sort your file names out is to use EasyTag. See my piece in LXF171.
johnlane
 
Posts: 16
Joined: Tue Nov 06, 2012 4:55 pm

Postby pk_fox » Sun Dec 22, 2013 9:10 pm

johnlane wrote:One way to sort your file names out is to use EasyTag. See my piece in LXF171.


Hi, yes I'm a big fan of easytag, but I can't see a way of renaming thousands of files using easytag, the situation I'm in is Amazon has kept a record of every CD I've bought from them and has made them downloadable in mp3 format with horribly convoluted names and I need to work out a way of renaming them via a batch process - I will probably end up writing a c# programme to achieve it - thank you for your time.
We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby MartyBartfast » Sun Dec 22, 2013 10:29 pm

This would do it, assuming that the delimiter between the bits you do want and the bits you don't want is always a hyphen "-".


Code: Select all
IFS='
'

for x in *.mp3 ; do y=`echo $x | cut -d"-" -f 2- | sed "s/^_//g" `; mv -v "$x" "$y" ; done


The IFS and the following line are in case some files have a space in their name, it will set the field separator to newline only (not required if there are no files with spaces).
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 817
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby pk_fox » Mon Dec 23, 2013 7:21 am

MartyBartfast wrote:This would do it, assuming that the delimiter between the bits you do want and the bits you don't want is always a hyphen "-".


Code: Select all
IFS='
'

for x in *.mp3 ; do y=`echo $x | cut -d"-" -f 2- | sed "s/^_//g" `; mv -v "$x" "$y" ; done


The IFS and the following line are in case some files have a space in their name, it will set the field separator to newline only (not required if there are no files with spaces).


Hi there, thanks for your help, I tried your code and it works fine, my problem now is I need to traverse all directories, the directory structure is /amazonmp3/artist/album/ and so on for every artist plus the artist could have more than one album. Hope you don't mind me asking.
We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby MartyBartfast » Mon Dec 23, 2013 9:59 am

Off the top of my head try


Code: Select all

for dirname in find /amazonmp3 -type d ; do cd $dirname ; for x in *.mp3 ; do y=`echo $x | cut -d"-" -f 2- | sed "s/^_//g" `; echo mv -v "$x" "$y" ; done ; done



note I've changed the command to 'echo mv -v "$x" "$y" ' that way you can run it and it will just display what it's going to do, if everything looks good just remove the "echo" and run it and it will do the job.

It would probably be neater and more maintainable to stick this in a shell script and indent it properly, particularly if it's going to get any more complex.
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 817
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby pk_fox » Mon Dec 23, 2013 3:14 pm

MartyBartfast wrote:Off the top of my head try


Code: Select all

for dirname in find /amazonmp3 -type d ; do cd $dirname ; for x in *.mp3 ; do y=`echo $x | cut -d"-" -f 2- | sed "s/^_//g" `; echo mv -v "$x" "$y" ; done ; done



note I've changed the command to 'echo mv -v "$x" "$y" ' that way you can run it and it will just display what it's going to do, if everything looks good just remove the "echo" and run it and it will do the job.

It would probably be neater and more maintainable to stick this in a shell script and indent it properly, particularly if it's going to get any more complex.


Thanks again, Im new to all this scripting game, if I place this in a script, do I need the semi colons at the end of every command ?
We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby nelz » Mon Dec 23, 2013 9:45 pm

You can replace the semi-colons with line breaks - you only need the semi-colons when you run it as a one-liner in a shell.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby MartyBartfast » Mon Dec 23, 2013 10:58 pm

something like this is easier to read and will be easier to maintain when you've forgotten what the code was supposed to be doing:

Code: Select all
for dirname in find /amazonmp3 -type d
do
   cd $dirname 
   for x in *.mp3    
   do
      y=`echo $x | cut -d"-" -f 2- | sed "s/^_//g" `
      echo mv -v "$x" "$y"    
   done
done
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 817
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby nelz » Tue Dec 24, 2013 8:40 am

If you want easier to read, replace the backticks with $().

But the whole thing is much easier with a dedicated command, like rename. If you want to remove everything up to and including the first double underscore

Code: Select all
rename -R -t -s/.\*__//r /amazommp3


The -t option shows the renames without performing them. Remove this, or replace it with -v, to do it for real.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby pk_fox » Tue Dec 24, 2013 8:51 am

nelz wrote:If you want easier to read, replace the backticks with $().

But the whole thing is much easier with a dedicated command, like rename. If you want to remove everything up to and including the first double underscore

Code: Select all
rename -R -t -s/.\*__//r /amazommp3


The -t option shows the renames without performing them. Remove this, or replace it with -v, to do it for real.


Whoa, that's impressive - I will check it out thanks.
We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby nelz » Tue Dec 24, 2013 1:36 pm

That's the rename command I mentioned earlier in this thread, not the one from coreutils.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8546
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby pk_fox » Tue Dec 24, 2013 2:46 pm

nelz wrote:That's the rename command I mentioned earlier in this thread, not the one from coreutils.


Hi Nelz, I downloaded the source for the rename tool but the "make" fails with this error

Code: Select all
rename.c:68:6: error: #elif with no expression
 #elif
      ^


I checked the file rename.c and line 68 is a standard #include statement, any ideas ? or can I get a binary from somewhere ?

edit
Apparently gcc compiler has changed since rename was written and if you use #elif it must be followed by an expression (it didn't used to be like this ) so I changed it to #else and all was fine. Thanks for the heads up - really useful tool.

Code: Select all
// Original code.
#if HAVE_REGEX_H
  #include <regex.h>
#elif
  #include "regex.h"
#endif



changed to

Code: Select all

#if HAVE_REGEX_H
  #include <regex.h>
#else
  #include "regex.h"
#endif

We can't stop here, this is bat country. Hunter S Thompson RIP.
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK


Return to Help!

Who is online

Users browsing this forum: nelz, Yahoo [Bot] and 3 guests