title: Convert all files of .flac format found in any sub-directory of main directory to .mp3
body: <p>I have the following problem: Imagine you have a directory named "Music" and inside there, in various sub-directories, there are files ending in .flac.<br>
  I want to write a bash script that finds all these files and for each one
  converts in the same directory that it was found, a .mp3 file and (optionally) if this file is created correctly to delete the original .flac file.</p>
  
  <p>I tried the following code, which I constructed by collecting bits and pieces from various places, which I leave as a more formal indication of what I want to achieve:</p>
  
  <pre><code>#!/bin/bash
  
  find ./Music -type f -name "*.flac" -print0 | while read -d $'\0' a; do
      echo "converting file: $a"
  
      # Get File name
      FN=`echo $a | sed 's/.\{5\}$//'`
  
      # Convert to mp3
      ffmpeg -i "$FN.flac" -ab 320k -map_metadata 0 -id3v2_version 3 -y "$FN.mp3"
  
      # If converted correctly delete
      if [ -f "$FN.mp3" ]; then
          echo "done"
          rm "$a"
      fi 
  done
  </code></pre>
  
  <p>Use of ffmpeg is optional (but preferred).<br>
  Thanks in advance for your help!</p>