PatMcTookis
PatMcTookis

Reputation: 35

grab 2 numbers from file name then insert into command

I'm a bit new to programming in general and I'm not sure how to go about accomplish this task in my bash script.

A quick background: when importing my music library (formerly organized by iTunes) to Banshee, all of the files were duplicated to fit Banshee's number style (ex: 02. instead of 02 ) on top of that, iTunes apparently did not save the ID3 tags to the files, so many of them are blank. So now I've got a few thousand tags to fix and duplicate files to get rid of.

To automate the process, I started learning to write bash scripts. I came up with a script (which you can see here) that does four things: removes unnecessary iTunes files, takes input from user about ID3 Tag information and stores it in variables, clears any present tag info from all files, writes new tags with info taken from user, using a program called eyeD3.

Now, here's where I run into my problem. This script is basically blindly writing info to all mp3 files in the dir. This is fine for tags that all the files have in common - like artist, album, total tracks, year, etc. But I can't tag each individual track number with this method. So I'm still editing the track# tags one at a time, manually. And that's something I really don't want to do 2,000+ times.

The files names all look like this:

01. song1.mp3
02. song2.mp3
03. song3.mp3

The command to write a track number to a tag looks like this:

$ eyeD3 -n 1 "01. song1.mpg"

So... I'm not sure how to go about automating this. I need to grab the first two digits of each file name, store them somewhere, then recall each one into a separate eyeD3 command.

Upvotes: 2

Views: 137

Answers (1)

Josh Jolly
Josh Jolly

Reputation: 11786

You can loop over the files using globbing, and use substring expansion to capture the first two characters of the filename:

for f in *mp3; do
    eyeD3 -n ${f:0:2} "$f"
done

Upvotes: 1

Related Questions