Friday, June 8, 2018

linux - batch rename multiple mp3's stripping leading digits


Hi could anyone help me? Im trying to strip leading digits from multiple mp3 files so
"01 some_file.mp3" would become "some_file.mp3" if any one could show me how to doit with zmv that would be great


Answer



An script like this might work:


for f in *; do
mv "$f" "${f:3}"
done

This will strip the first 3 characters of the filename (the two numbers and the space).


Or something like this:


for f in *; do
mv "$f" "$(printf "$f" | cut -d' ' -f2-)"
done

If there is an actual blank space between the number and the rest of the name. This will print everything from the blank space up to the end of the file.


Note that this will only work for a fixed number of digits (first example) or if there's an actual space between the two fields (second example), if the case doesn't apply for any of the mentioned solutions, you can still count on regex matching:


for f in *; do
mv "$f" "$(printf "$f" | sed s/[[:digit:]]*\ *//)"
done

This would work for any number of leading digits and/or spaces. For example:


echo '0001     hellow  aasa 1212.mp3' | sed s/[[:digit:]]*\ *//

Will print


hellow  aasa 1212.mp3

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...