More Bash Scripts

with 1 Comment

I need to copy all of my FLAC files out of my directory tree but preserve the folder structure that they were in… Here’s my script.

Unfortunately, it’s not a “perfect” solution as there are issues when handling special characters in files (especially newline characters). However, since my file really just have spaces in them, that’s all this was designed to beat.

SOURCEDIR="/var/mirror/Data/Audio/My CD Archive"
TARGETDIR="/var/mirror/FLAC"
 
mkdir "$TARGETDIR"
 
find "$SOURCEDIR" -type d | while read DIR; do
  if [[ "$DIR" != "$SOURCEDIR" ]]; then
    SHORTDIR=`echo $DIR | sed 's/.*///g'`
    mkdir "$TARGETDIR/$SHORTDIR"
    echo "Now in $DIR"
    find "$DIR" -name "*.flac" | while read FILE; do
      SHORTFILE=`echo $FILE | sed 's/.*///g'`
      echo "Now moving $SHORTFILE"
      mv "$FILE" "$TARGETDIR/$SHORTDIR"
    done
    echo "--------------------"
    sleep 1
  fi
done

One Response

  1. Dave Feucht
    |

    I never did get very good at awk and sed, I should remedy that 🙂 I always loved writing scripts in perl though 🙂

Leave a Reply