More Bash Scripts

Posted by Andrew Flanagan

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
Share and Enjoy:
  • Digg
  • Sphinn
  • Mixx
  • Google
  • Fark
  • Furl
  • StumbleUpon

Andrew Flanagan on September 25th 2007 in Actual Events, Geekiness

One Response to “More Bash Scripts”

  1. Dave Feucht responded on 06 Nov 2007 at 3:13 pm #

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

Trackback URI | Comments RSS

Leave a Reply