Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help renaming files

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Wed Dec 18, 2013 7:42 am    Post subject: Help renaming files Reply with quote

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.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Wed Dec 18, 2013 7:51 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
johnlane



Joined: Tue Nov 06, 2012 4:55 pm
Posts: 13

PostPosted: Sun Dec 22, 2013 8:15 pm    Post subject: Reply with quote

One way to sort your file names out is to use EasyTag. See my piece in LXF171.
Back to top
View user's profile Send private message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Sun Dec 22, 2013 9:10 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 806
Location: Hants, UK

PostPosted: Sun Dec 22, 2013 10:29 pm    Post subject: Reply with quote

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:

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.
Back to top
View user's profile Send private message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Mon Dec 23, 2013 7:21 am    Post subject: Reply with quote

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:

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.
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 806
Location: Hants, UK

PostPosted: Mon Dec 23, 2013 9:59 am    Post subject: Reply with quote

Off the top of my head try


Code:


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.
Back to top
View user's profile Send private message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Mon Dec 23, 2013 3:14 pm    Post subject: Reply with quote

MartyBartfast wrote:
Off the top of my head try


Code:


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.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Mon Dec 23, 2013 9:45 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 806
Location: Hants, UK

PostPosted: Mon Dec 23, 2013 10:58 pm    Post subject: Reply with quote

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:

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.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Tue Dec 24, 2013 8:40 am    Post subject: Reply with quote

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:
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)
Back to top
View user's profile Send private message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Tue Dec 24, 2013 8:51 am    Post subject: Reply with quote

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:
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.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8369
Location: Warrington, UK

PostPosted: Tue Dec 24, 2013 1:36 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Tue Dec 24, 2013 2:46 pm    Post subject: Reply with quote

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:

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:

// Original code.
#if HAVE_REGEX_H
  #include <regex.h>
#elif
  #include "regex.h"
#endif



changed to

Code:


#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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast