#!/bin/sh usage() { echo 'Usage: ${0} -p prefix [-d directory] [-t extension] [-u]' echo " -p prefix to be removed" echo " -d destination folder" echo " default current directory" echo " -t files extension" echo " default to ogg" echo " -u encode filename in UTF-8" echo '---------------------' } processing() { echo processing "${@}" newfile=`echo "$@" | sed "s/${prefix}//g"` [ -n ${iconv} ] && newfile=`echo ${newfile} | iconv -cs -t UTF-8` echo " : copying <$newfile> into <${dest}>" cp "${@}" "${dest}/${newfile}" } while getopts "p:d:t:u" option do case ${option} in p) prefix="${OPTARG}" ;; d) dest="${OPTARG}" ;; t) ext="${OPTARG}" ;; u) iconv="y" ;; *) usage exit 1 ;; esac done dest=${dest:-.} ext=${ext:-ogg} [ -z "${dest}" ] && usage && exit 1 [ -z "${prefix}" ] && usage && exit 1 echo echo "Copy and rename ${ext} files, removing <${prefix}> prefixes, into <${dest}>." [ -n "${iconv}" ] && echo '* Filename UTF-8 encoding is on.' echo mkdir -p "${dest}" || (echo "unable to create ${dest}" && exit 1) find . -maxdepth 1 -type f -name "*.${ext}" -print | while IFS= read file; do processing "${file}" ; done