1. Batch renaming of files
Problem: You've got a lot of mp3-files containing a nasty string like "(www.nastysite.com)" and want to remove it from the name:Code:
find ./ -iname "*.mp3" |
(
while read i; do
m=$(
echo "$i" | sed -e 's/(www.*\.com) //'
);
mv "$i" "$m";
done
)
or copy-paste version:
find ./ -iname "*.mp3" | (while read i; do m=$(echo "$i" | sed -e 's/(www.*\.com) //' ); mv "$i" "$m"; done)
Breakdown
1. find ./ -iname "*.mp3" ... Feeds the fullpath of the mp3s in the current directory to stdin (needed as starting point)
2. Pipe the "Mp3-list" to a little bash-script, which loops through every line of input
First the name is read into variable 'i'.
Second we pipe the name (through echo '$i') to sed, which does the removing of the nasty string through a regex-pattern
's/(www.*\.com) //' ... substitutes first occurrence of '(www.nastysite.com) ' through void.
Then we store the result in variable 'm'.
Last we rename the file by 'mv "$i" "$m"'
2. Batch renaming of id3-tags of mp3-files
Problem: You want your mp3s to be indexed by a programm utilizing id3-tags. Sadly many mp3s have messed up id3-tags or no tags at all.
But we want at least the title to be displayed correctly, so we need to set it.
For that we derive it from the filename:
Say our mp3-files are named like this: 'Triana - El Patio - 3 - Abre la puerta niña.mp3".
It consists of 4 parts:
1. Artist: Triana
2. Album: El Patio
3. Track#: 3
4. Title: Abre la puerta niña
Now the title should be '3 - Abre la puerta niña'
This little script does the trick. It's just a slight variation of the previous one, namely replacing the renaming part (mv "$i" "$m")
through the retagging part (id3 -t "$m" "$i"):
For that we utilize the cmd-tool id3 which mangles the id3-tag info of an mp3-file. The option '-t' sets the title, '-a' the Artist, '-A' the Album,...
Code:
01: find ./ -iname "*.mp3" |
02: (
03: while read i; do
04: m=$(
05: echo "$i" | sed -e 's/.*\([0-9] - .*\)\.mp3/\1/'
06: );
07: #Output status
08: echo "$i: '$m'";
09: #Retag the title
10: id3 -t "$m" "$i";
11:
12: done
13: )
or copy-paste version:
find ./ -iname "*.mp3" | (while read i; do m=$(echo "$i" | sed -e 's/.*\([0-9] - .*\)\.mp3/\1/'); echo "$i: '$m'"; id3 -t "$m" "$i"; done)
Breakdown
The regex is a bit more tricky, because it uses grouping:
s/.*\([0-9] - .*\)\.mp3/\1/
.* is a greedy operator, that means it consumes as most as it can, as long as the whole expression still matches.
\(...\) doesn't actually match anything, it's just a marker (group), so that we can reference the content (all within the brackets) by \1 later.
[0-9] ... this is a character-class which matches only numbers from 0 through 9
\. matches a single dot (Because . by default matches any single character)
0 Kommentare:
Kommentar veröffentlichen