View Single Post
Old 08-11-2011, 05:58 PM   #10
bruteforce
The Gristle Eating Giant
Max Brawn
Points: 15,492, Level: 80 Points: 15,492, Level: 80 Points: 15,492, Level: 80
Activity: 14% Activity: 14% Activity: 14%
 
bruteforce's Avatar
Default

Save time with shell scripting.

If you find yourself having to perform the same task over and over, consider writing a script to do it for you. This may not work for every task, but if you frequently need to do things like rename pictures or retag your mp3s, figure out how to do it on the command line and then write a script that will loop through all your directories and files to do the job for you. MP3 retagging example follows. ( I know it isnt bullet proof, but to does what I need it to)

Code:
#! /bin/bash
#bruteforce
#this script traverses given directories and updates tags of amy mp3 file it finds 
#tags updated are track number, name, artist, and album title
function taggit {
#track number
    #   echo $1
       track_number=`echo ${1%%_*}`
       id3tag -2 --track="$track_number" "$1"
#track name
       temp_name=`echo ${1#*_}`
       temp_name=`echo ${temp_name%.*}`
       track_name=`echo $(echo "$temp_name"|sed s/_/" "/g)`
      
       id3tag -2 --song="$track_name" "$1"
#getting artist
       temp=`pwd`
       artist=`echo ${temp%/*}`
       artist=`echo ${artist##*/}`
       artist=`echo $(echo "$artist"|sed s/_/" "/g)`
       id3tag -2 --artist="$artist" "$1"
#album time       
       temp2=${temp##*/}
       album=$(echo "$temp2"|sed s/_/" "/g)
      # echo ${album}
       id3tag -2 --album="$album" ${1}
      #echo $(echo "$file"|sed s/_/" "/g)

}

#check for at least 1 arg
if [[ $# -lt 1 ]];then
  echo "Must provide at least 1 directory"
  exit 1
fi
#store current dir to base paths of off
home_dir=`pwd`
#looping
for d in "$@"; do
#echo $d
  if [[ -d "$d" ]]; then
    cd "$d"
    for file in *; do
      if [[ ${file%.mp3} != "$file" ]]; then
      #echo ${file}
      # echo $(echo "$file"|sed s/_/" "/g)
        taggit "$file"
      fi
      if [[ ${file%.MP3} != "$file" ]]; then
        taggit "$file"
      fi
    done
  cd "$home_dir"
  else
    echo "$d Not a directory"
    #exit 1
  fi
  exit 0
done
bruteforce is online now   Reply With Quote