Trim first 16 chars from filename and rename the file

Used this script to remove the chars from mp3 files. Keep this script file in the directory and execute it. Simple!
Enter the number or chars to remove in the command line param – ./ren.sh 10

#!/bin/bash

# -----------
# Usage: ./sh NoLeadingTrim [NoOfEndTrim]
#
# -----------
if [ $# -eq 0 ]
  then
    echo "Enter number of leading characters to trim"
    exit
fi

files=`pwd`/*.mp3
files=*.mp3
for f in $files
do
  fn="$f"
  len=${#fn}
  if [ $# -gt 1 ]; then
    rel=`expr $len - $2`
    nn=${fn:$1:$rel}
    echo "$nn".mp3
    mv "$fn" "$nn".mp3
  else
    nn=${fn:$1}
    echo "$nn"
    mv "$fn" "$nn"
  fi
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s