#!/bin/bash shopt -s globstar src_dir="Music" dest_dir="Music_new" for orig_path in "${src_dir}"/**/*.flac; do #Change first directory in the path new_path=${orig_path/${src_dir}$src_dir/$dest_dir} #Remove filename from new path. Only directories are left. #They are needed for mkdir dir_hier=${new_path%/*} #Remove extension .flac from path, thus we have original path with first directory #changed and file extension removed. #Original path: "Music/one/two/song.flac" #New path: "Music_new/one/two/song" new_path=${new_path%.*} echo mkdir -p "$dir_hier" #New path with extension: "Music_new/one/two/song.m4a" #ffmpeg should create new file by this path, necessary directories #already created by mkdir echo ffmpeg -i "$orig_path" -c copy -acodec alac "${new_path}.m4a" done echo before mkdir and ffmpeg should be removed after checking.