Search code examples
bashfindcountergnueyed3

GNU Find : Get a running counter for the -exec command ( to tag mp3s with running track number )


I want to automatically tag a collection of MP3 to become custom-name VA Albums. Currently, I'm using quite fail-safe commands like this one :

find . -name "*.mp3" -exec eyeD3 -A "Indie 2015" -b "Various Artists" {} \;

Now, I would like to add a running track number to the files. The most straightforward way would be the current match/line number in the find -exec command - is there such a command?

eyeD3 sets the track number by using -n NUM, --track NUM, so I'm searching for something like

find . -name "*.mp3" -exec eyeD3 -A "Indie 2015" -b "Various Artists" -n **FIND_MATCH_NUMBER** {} \;

Solution

  • One option, using globstar (assuming that you do have files in nested subdirectories, which was why you were using find in the first place):

    shopt -s globstar
    n=1
    for file in **/*.mp3; do
        eyeD3 -A "Indie 2015" -b "Various Artists" -n "$((n++))" "$file"
    done
    

    With globstar enabled, the **/*.mp3 glob expands to all files ending in .mp3 in the current directory, as well as in subdirectories. $((n++)) evaluates to the current value of the counter $n and then increments it.