LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 12-20-2012, 04:43 PM   #16
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0

Thanks for your replies David; i will look into the links provided tomorrow.
Your approach looks also rather interesting. Ill try to implement this tomorrow as it would cleanup the code allot!

I was, btw, already making use of the while read function for a loop (find $DIR -name \*$TYPE | head -200 | while read ITEM; do).

Here's the total version:
Code:
#!/bin/bash
#set -vx

IFS=$'\n'           #changing IFS to newline means spaces
                    #are ignored during word-breaking

DIR=${1:-/home/mainstream/Music/rename}
TYPE='.mp3'
shopt -s extglob    #enable extended globbing

function my_rename_func() {
     #break input filename into path and name
     #so that names with paths included are correctly handled
     filepath=${file%/*}
     newname=${file##*/}
     mp3=${file##*/}

     #remove junk characters from filename
     newname=${newname//+([,\'])/}

     regex='_\(final\)|\ \(Full+CDQ\)|\ \(CDQ\)|\ \(NoShout\)|\ \(CDQ \& Final\)|\ \(Dirty\)|\ \(Mastered\)|\ \(full\)]'
     newname=${newname//+($regex)/}

     #replace delimiter
     regex='\ \-\ |\_\-\_|\-\-'
     newname=${newname//+($regex)/-}

     regex='_featuring_\|_feat_\|_f._\|_ft_|_feat._'
     newname=${newname//+($regex)/_ft._}

     regex='\(feat._|\(feat_'
     newname=${newname//+($regex)/\(ft._}

     #replace all instances
     newname=${newname//+(_and_)/_&_}

     #change spaces (and tabs) to underscores in filename
     #extglob reduces multiple spaces to single underscore
     newname=${newname//+([[:space:]])/_}

     #lowercase filename
     newname=${newname,,}

     #remove all initial "digit+[-_]" combinations
     #again extglob handles variable numbers of digits
     newname=${newname#+([0-9])[_-]}

     #remove any final "-zzzz" strings
     #similar to the last operation
     #assumes the file ends in ".mp3", however
     regex='zzzz|mst|alki|you|wus|htf|whoa'
     newname=${newname/-+($regex).mp3/.mp3}

     #remove any "-(nnn_bpm)" strings
     #remember, spaces have been changed to underscores already
     newname=${newname/-(+([0-9])_bpm)}

     #you can continue adding as many changes as you want here.

     #call mv to change the filename.
     #I used -iv to make it interactive and verbose, but that's up to you.
     if [[ $mp3 != $newname ]]
     then
          #echo $mp3
          echo $newname
          mv $DIR/$mp3 $DIR/$newname
     fi
}

tag_the_stuff() {
read -e -p "Enter Year: " -i "2012" YEAR
echo "Select a Genre:"
select GENRE in "Classical" "Dubstep" "Hip-Hop" "Skip"; do
    case $GENRE in
        Classical ) echo $GENRE; break;;
        Dubstep ) echo $GENRE; break;;
        Hip-Hop ) echo $GENRE; break;;
        Rap ) echo $GENRE; break;;
        Skip ) exit;;

    esac
done
id3v2 -D $DIR/*$TYPE #how can tag per file?
id3v2 -g $GENRE -y $YEAR $DIR/*$TYPE
}

tag_the_stuff #i tried this in the loop (below), but it asked for the genre and year all the time, which is the same on all files
              #this most be done easier somehow.
              #should i declare a local variables or something? 

find $DIR -name \*$TYPE | head -200 | while read ITEM; do
   echo $ITEM
   new_file=$(my_rename_func ${ITEM})
done

exit 0

Last edited by mainstream; 12-20-2012 at 04:46 PM.
 
Old 12-28-2012, 11:30 AM   #17
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Hmm somehow the whole script doesn't work anymore... :-(

My script:
Code:
#!/bin/bash
set -vx

IFS=$'\n'           #changing IFS to newline means spaces
                    #are ignored during word-breaking

DIR=${1:-/home/mainstream/Music/rename}
TYPE='.mp3'
shopt -s extglob    #enable extended globbing

function my_rename_func() {

     #break input filename into path and name
     #so that names with paths included are correctly handled
     filepath=${file%/*}
     newname=${file##*/}
     mp3=${file##*/}

     #remove all initial "digit+[-_]" combinations
     #again extglob handles variable numbers of digits
     newname=${newname#+([0-9])[_-]}

     #remove junk characters from filename
     newname=${newname//+([,\'])/}

     regex='_(final)| (Full+CDQ)| (CDQ)| (NoShout)| (CDQ & Final)| (Dirty)| (Mastered)| (full)|( 2o12 ) [ www.MzHipHop.Me ]'
     newname=${newname//+($regex)/}

     #replace delimiter
     regex=' - |_-_|--'
     newname=${newname//+($regex)/-}

     regex='_featuring_|_feat_|_f._|_ft_|_feat._'
     newname=${newname//+($regex)/_ft._}

     regex='(feat._|(feat_'
     newname=${newname//+($regex)/(ft._}

     #replace all instances
     newname=${newname//+(_and_)/_&_}

     #change spaces (and tabs) to underscores in filename
     #extglob reduces multiple spaces to single underscore
     newname=${newname//+([[:space:]])/_}

     #lowercase filename
     newname=${newname,,}

     #remove any final "-zzzz" strings
     #similar to the last operation
     #assumes the file ends in ".mp3", however
     regex='zzzz|mst|alki|you|wus|htf|whoa'
     newname=${newname/-+($regex).mp3/.mp3}

     #remove any "-(nnn_bpm)" strings
     #remember, spaces have been changed to underscores already
     newname=${newname/-(+([0-9])_bpm)}

     #you can continue adding as many changes as you want here.

     #call mv to change the filename.
     #I used -iv to make it interactive and verbose, but that's up to you.
     if [[ $mp3 != $newname ]]
     then
          #echo $mp3
          echo $newname
          mv $DIR/$mp3 $DIR/$newname
     fi
}

tag_the_stuff() {
read -e -p "Enter Year: " -i "2012" YEAR
echo "Select a Genre:"
select GENRE in "Classical" "Dubstep" "Hip-Hop" "Skip"; do
    case $GENRE in
        Classical ) echo $GENRE; break;;
        Dubstep ) echo $GENRE; break;;
        Hip-Hop ) echo $GENRE; break;;
        Rap ) echo $GENRE; break;;
        Skip ) exit;;

    esac
done
#id3v2 -D $DIR/*$TYPE
#id3v2 -g $GENRE -y $YEAR $DIR/*$TYPE
}

tag_the_stuff #invoke ID3V2-tag (else the loop ask for genre all the time?)

find $DIR -name \*$TYPE | head -200 | while read ITEM; do
   echo $ITEM
   new_file=$(my_rename_func ${ITEM})
done

exit 0
Output:
Code:
+ read ITEM
+ echo '/home/mainstream/Music/rename/Action Bronson - Tim Westwood (Freestyle).mp3'
/home/mainstream/Music/rename/Action Bronson - Tim Westwood (Freestyle).mp3
+ my_rename_func
+ filepath=
+ newname=
+ mp3=
+ newname=
+ newname=
+ regex='_(final)| (Full+CDQ)| (CDQ)| (NoShout)| (CDQ & Final)| (Dirty)| (Mastered)| (full)|( 2o12 ) [ www.MzHipHop.Me ]'
+ newname=
+ regex=' - |_-_|--'
+ newname=
+ regex='_featuring_|_feat_|_f._|_ft_|_feat._'
+ newname=
+ regex='(feat._|(feat_'
+ newname=
+ newname=
+ newname=
+ newname=
+ regex='zzzz|mst|alki|you|wus|htf|whoa'
+ newname=
+ newname=
+ [[ '' != '' ]]
I also tried to integrate your tips for parameter substitution, but I've done something wrong (and can't seem to find out what), so it's not working atm.

Last edited by mainstream; 12-28-2012 at 11:56 AM.
 
Old 12-28-2012, 11:59 AM   #18
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Ah! i found the problem. It didn't break down the files in the variables filepath, newname and mp3.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash scripting Ronayn Programming 11 03-20-2012 05:46 AM
bash scripting ninjafairy Programming 11 05-18-2011 11:00 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Bash scripting help arturhawkwing Linux - General 1 08-10-2006 12:54 PM
BASH Scripting ? eroica Programming 3 06-07-2004 08:51 PM


All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration