Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

@terdon's answer provides this script:

find . -type f -name "*mp3" | while read file; do 
    album="$(basename "$(dirname "$file")")"; 
    filename="$(basename "$file")"; 
    artist=${filename%%-*}; 
    title=${filename##*-}; 
    title=${title%%.mp3}; 
    eyeD3 -A "$album" -t "$title" -a "$artist" "$file"; 
done

It makes use of eyeD3 to update mp3 tags. It extracts Album, Artist and Title from file name, supposing that the file name is Album/Artist - Title.mp3.

Well, after running it the Title field is starting with a blank space. See below:

enter image description here

How to fix this if my mp3 files have the pattern

Album/<Artist with possible spaces> - <track number> - <Title with possible spaces>.mp3
share|improve this question
    
This might not be in the domain of bash scripting anymore. Using python instead might be beneficial. –  Alexander Feb 9 at 19:29
    
possible duplicate of MP3 tags Cyrillic chars –  Mikel Feb 9 at 21:48
    
@Sigur I don't understand why you didn't just update your question and comment there. –  Mikel Feb 9 at 21:49
    
It was suggested to me to ask as a new question since the problem now is related with script and strings manipulation. Not necessarily tags. –  Sigur Feb 9 at 21:59

3 Answers 3

up vote 1 down vote accepted

Since I assume that your names do not always have these spaces, the easiest thing to do would be to simply remove the space if present:

find . -type f -name "*mp3" | 
  while read file; do 
   album="$(basename "$(dirname "$file")")"; 
   filename="$(basename "$file")"; 
   artist=${filename%%-*}; artist=${artist%% }; artist=${artist## };
   title=${filename##*-}; title=${title%% }; title=${title## };
   title=${title%%.mp3}; 
   eyeD3 -A "$album" -t "$title" -a "$artist" "$file";
 done
share|improve this answer
    
Perfect. Now everything is fine. Thanks so much. –  Sigur Feb 9 at 20:04

Ksh extended glob patterns give wildcards the power of regular expressions. In bash, turn them on with shopt -s extglob.

Then, instead of

title=${filename##*-}

to remove the part of $filename up to the last -, you can use

title=${filename##*- #}

to also strip whitespace after the -.

share|improve this answer

If you just want to remove the leading space for the Artist, you could use sed. Something like this should work.

artist=${filename%%-*}; 
artist=`echo $artist |  sed 's/^ *//g'`;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.