Skip to content

Instantly share code, notes, and snippets.

@deckerego
Created December 6, 2013 22:09
Show Gist options
  • Save deckerego/7832994 to your computer and use it in GitHub Desktop.
Save deckerego/7832994 to your computer and use it in GitHub Desktop.
Rename a slew of .ogg files. Actually not a great script, but useful for re-using for other directory recursion/diving operations.
#!/bin/bash
BASEDIR=$1
if [ -z $BASEDIR ] ; then
echo "Usage: checkm3us directory"
exit -1
fi
if [ ! -d $BASEDIR ] ; then
echo "Directory $BASEDIR not found"
exit -1
fi
DIRLIST=`ls -1d "$BASEDIR/"*`
IFS="
"
for DIRECTORY in $DIRLIST ; do
[ ! -d "$DIRECTORY" ] && continue
PLAYLISTS=`ls -1d "$DIRECTORY/"*.m3u`
for PLAYLIST in $PLAYLISTS ; do
SIZE=`grep -c '' "$PLAYLIST"`
TEMP_PLAYLIST="$PLAYLIST.tmp"
rm -f $TEMP_PLAYLIST
for (( INDEX=1 ; $INDEX <= $SIZE ; INDEX++ )) ; do
ENTRY=`sed -n "$INDEX"p "$PLAYLIST"` # Get playlist entry
ENTRY=`echo "$ENTRY" | awk '$0 {print gensub(/\n|\r/,"",1)}'` # Get rid of newlines
TRACKENTRY=`echo "$ENTRY" | awk '$0 {print gensub(/^[0-9][0-9]-/,"",1)}'` # Get rid of track numbers
if [ ! -r "$DIRECTORY/$ENTRY" ] ; then
echo "Entry $ENTRY does not exist."
fi
if [ $INDEX -lt 10 ] ; then
TRACKENTRY="0$INDEX-$TRACKENTRY"
else
TRACKENTRY="$INDEX-$TRACKENTRY"
fi
mv "$DIRECTORY/$ENTRY" "$DIRECTORY/$TRACKENTRY"
echo "$TRACKENTRY" >> $TEMP_PLAYLIST
done
mv $TEMP_PLAYLIST $PLAYLIST
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment