ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi, I am looking for a script that renames all MP3 files (and maybe its corresponding tags) of a specific folder and its subfolders.
I want to just rename the file name, so that only the first letter is capitalized, and the first letter that stand behind a "-" (Regard! An empty slot is not a letter!). For examle:
From:
La Quinta Estación - Me Muero
to
La quinta estación - Me muero
(the first letter and the letter that comes after a Hyphen)
songname='La Quinta Estación - Me Muero'
fixed_name=$(echo "$songname" | tr "[A-Z]" "[a-z]" | \
sed 's/\(^[[:alpha:]]\)\(.*\)\(- [[:alpha:]]\)\(.*$\)/\U\1\E\2\U\3\E\4/')
echo "$fixed_name"
The above will:
1) change the whole song name to lower case, using `tr`
2) Uppercase the first letter of the whole song name
3) Uppercase the first letter following a '-'
The above, does the exact same thing, except it omits the part about the stuff after a dash.
I've never heard of pyRenamer, and am not familiar with python, but the above is done in bash shell, using sed and tr. If you want to recurse through whole directories, it's up to you to add that code to search the directories, run each filename/tag/whatever through the above codes, and rename the files how you like.
I would use `find` to do the recursive search, and of course, `mv` to rename the files.
PS - It would be educational for you if you actually understand the sed code used, (IF you're interested in using it, that is..) so that you can create and/or modify similar code in the future for yourself. If you would like an explanation (a breakdown) of what sed is doing here, just ask.
Cheers!
Sasha
Last edited by GrapefruiTgirl; 01-14-2010 at 12:03 AM.
Hi Sasha!
You say that if I have questions, I should ask you, right? So here it goes:
I'm not interested just change the name of one song.
I want to modify the files of all subfolders in a specified folder.
The music folder is located at: /media/160Gb/Musica.
It contains many folders, and these again others and these others, etc ...
I would like to simply run this script from the Music folder, and that he choice all subfolders in the folder where the script is located.
For example: If the script is located on the desktop, and on the desktop are folders with music, when I run the script from the desktop folder, the script will change all files in the subfolders on the desktop.
It seems a bit hard to explain, so I understand if you do not understand me. Sorry.
The most important thing for me is to rename the files. Because there are programs that have the function to change the tags, according to the filename.
Hi Sasha!
You say that if I have questions, I should ask you, right? So here it goes:
I'm not interested just change the name of one song.
I want to modify the files of all subfolders in a specified folder.
The music folder is located at: /media/160Gb/Musica.
It contains many folders, and these again others and these others, etc ...
I would like to simply run this script from the Music folder, and that he choice all subfolders in the folder where the script is located.
For example: If the script is located on the desktop, and on the desktop are folders with music, when I run the script from the desktop folder, the script will change all files in the subfolders on the desktop.
It seems a bit hard to explain, so I understand if you do not understand me. Sorry.
The most important thing for me is to rename the files. Because there are programs that have the function to change the tags, according to the filename.
Thank you very much for your help,
Patrick
Start from
man find
and/or learn a scripting language like Perl/Python/Ruby.
Yes, I understand perfectly what you're asking, and if you want to do the job using Bash (shell) then I have done the hardest part for you (the sed code to clean up each song name) and I did that for my own entertainment and education (I'd never used sed to change case before) but YOU must do some reading and toying around too -- nobody will usually create the whole project for you; you must do some of the work yourself.
I know for a fact that there are numerous threads around LQ, wherein other members have asked about code to do the EXACT same thing as you are trying to do: that is, search a folder or path full of music or videos, and rename all the items in there.
Personally, I don't know Perl, Python, or Ruby, but if you want to use some of these to do the job, take Sergei's advice above, and begin reading about these languages.
If you want to use shell as I have done, then read the bash man page to learn about the basics of FOR loops and IF-THEN-ELSE constructs, and variables; read the man page for the mv command, which will be used to do the actual renaming; and read the man page for find which would be used to do the actual recursive search in the directory and produce each song name for processing.
My offer of further information was mainly for if you wanted me to break down the `sed` statement, and show the various parts, so you would understand how/why the sed statement is doing what it's doing, however if you write some code to do the searching, I'll happily help you debug it.
So, search LQ a bit for some other threads where people are doing the exact same task as you want to do, and modify their examples or write some new code, and present to us what you've come up with. We will gladly help you sort out problems and errors, and point you in the right direction.
Well if you ask me, i would split the problem
1. finding directories which contain mp3
something simple can be done in shell:
find . -type f -name '*.mp3' | sed 's%[^/]*$%%' | uniq
the find searches for "files" whose name matches the pattern *.mp3. it
returns the list of files found, one file per line
the sed removes the last part of each line, i.e it removes the name,
it keeps only the directory name
uniq merge identical lines, so you end up with all the directories you want
you should write it in perl if you have some knowledge, that could be
helpful for the rest below
2. renaming and tagging
for this one, you have to decide whether you want to code it in bash,
or in a higher level language such as perl or python
i personally know perl, and it comes with a simple library to modify
mp3 id3 tag.
so you should adapt the example given in the forum to perl, and then
use the adapted algo twice:
. once for renaming the file
. another time to rename the id3 tags you want (i.e. title, artist, etc...)
i don't remember by heart how to use id3 in perl, you can have a look
at http://husk.org/perl/id3s/notes.html for example
what i like with perl is the programmatic approach to reading tags and
modifying them
exerp from the link above: http://husk.org/perl/id3s/notes.html
#!/usr/local/bin/perl
use strict; use warnings;
use MP3::Info;
use MP3::Info::set_mp3v2tag;
my $file = "example.mp3";
my $tag = get_mp3tag($file);
quite straightforward if you know the perl syntax.
MP3::Info is available by
sudo apt-get install libmp3-info-perl
if you're a warrior, you can even play a bit with perl interface to musicbrainz
this way you could grab the album cover for a mp3 and store it in the mp3 directly or save it to disk
can you explain how/where that works? I just tried to use it for both example file-name & tag provided in the initial post, and the exact same thing came out as went in (so it did nothing)..
Good morning!
It is wonderful to see you help me!
Especially thanks to my friend D.Ciabrini and grail.
@ grail: I'm so glad, that it works with that song. But how must I run the script to make it operates on the entire folder and sub-folders?
As you will understand, I can't copy and paste the name of each song, and then run the script .:Blinking:. !
It must be possible, to put the script in the folder, and that applies to all content (including the contents of all sub-folders).
If you learn python you will have little trouble with bash scripting.
But if you looked at man find as mentioned your problem would already be solved
a little help
Code:
find . -name '*.mp3' | sed ...
I think that needs to be double quotes because of the spaces
Code:
find . -name "*.mp3" | sed ...
Last edited by whizje; 02-26-2010 at 07:07 AM.
Reason: little help
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.