Results 1 to 6 of 6
  1. #1


    Member Since
    Sep 25, 2007
    Posts
    12
    Question Newbie help with bash script to search and replace a filename
    I'm a complete newbie to shell/bash scripting (only been going a couple of days) and am trying to get to grips with it.

    I'm trying to create a script that loops through a folder finding any sub folders that are called Season 1-9 and appends a 0 to the beginning of the number.

    E.g Season 1 -> Season 01
    Season 2 -> Season 02
    Season 11 -> Season 11

    I think this code is right for looping through find only folders and leaving files alone. Just can't seem to work out how to use sed and regular expressions to replace the text in the file names.

    Here's my code so far and would love some pointers.

    Code:
    #!/bin/bash
    # Script to pad a 0 to Season numbers
    
    TV_DIR=~/Desktop/TV
    
    cd "$TV_DIR"
    
    for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders 
    	
    	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
    	echo "$dir"
    
    	
    	done
    
    echo "Job Done."

  2. #2

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    Here is the easiest sample I could think of.

    Code:
    echo "Season 11" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

  3. #3


    Member Since
    Sep 25, 2007
    Posts
    12
    Unhappy
    Thanks for that xstep.

    It seems to be half working. It's certainly replacing the text as I want and even echoing it back to the screen but I've tried to apply the code to a variable and then use this with a mv command to change the the filename. E.g.

    Code:
    mv $dir $newfilename
    Code:
    usage: mv [-f | -i | -n] [-v] source target
           mv [-f | -i | -n] [-v] source ... directory
    Job Done.
    But it doesn't seem to be working and instead only outputting an error on how to use the mv command. When I try to echo the $newfilename variable this just shows up as empty.

    Here's my script now and would be interesting to know what the sed stuff does that you suggested as I don't fully understand the line of code you've written.

    Code:
    #!/bin/bash
    # Script to pad a 0 to Season numbers
    
    TV_DIR=~/Desktop/TV
    
    cd "$TV_DIR"
    
    for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders 
    	
    	newname=$("$dir" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/')
    	echo "This is the new folder name $NEWNAME"
    	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
    	
    	mv $dir $newname 	
    	
    	done
    
    echo "Job Done."

  4. #4

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    Here is my version...

    Code:
    #!/bin/bash
    # Script to pad a 0 to Season numbers
    
    TV_DIR=~/Desktop/TV
    
    cd "$TV_DIR"
    
    #loop through each entry in the TV_DIR finding folders 
    #remove spaces because for loop uses them as name delimeters.
    for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do
    
            #remove my special characters.
            dir=`echo $dir | sed 's/~~/ /'`
            #change single digit to two digits.
    	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`
    	echo "This is the new folder name $newname"
    	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
    
    #mv folderA folderA seems to have special meaning in mv command so bypass those same name cases.
    	if [ "$newname" != "$dir" ]; then
    	    mv "$dir" "$newname"
    	fi
    
    	done
    
    echo "Job Done."
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

  5. #5


    Member Since
    Sep 25, 2007
    Posts
    12
    Quote Originally Posted by xstep View Post
    Here is my version...

    Code:
    #!/bin/bash
    # Script to pad a 0 to Season numbers
    
    TV_DIR=~/Desktop/TV
    
    cd "$TV_DIR"
    
    #loop through each entry in the TV_DIR finding folders 
    #remove spaces because for loop uses them as name delimeters.
    for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do
    
            #remove my special characters.
            dir=`echo $dir | sed 's/~~/ /'`
            #change single digit to two digits.
    	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`
    	echo "This is the new folder name $newname"
    	#here I want to search all folders in this format Season 1-9 and change to Season 01 etc
    
    #mv folderA folderA seems to have special meaning in mv command so bypass those same name cases.
    	if [ "$newname" != "$dir" ]; then
    	    mv "$dir" "$newname"
    	fi
    
    	done
    
    echo "Job Done."
    AMAZING xstep. That works exactly as I intended/wanted.

    But what do these two lines do exactly?

    Code:
    dir=`echo $dir | sed 's/~~/ /'`
    	newname=`echo $dir | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/'`

  6. #6

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    The echo command pipes $dir to the sed command which does it's magic. You had "$dir" by it self which for some reason was messing me up.

    The lines are separated to make it easier for me to understand. The first one undoes what I did in the find statement. The second does what your original intent was.

    Do a 'man sed' to see how the -e helps us.
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. rSync commandline bash script
    By scar1 in forum OS X - Operating System
    Replies: 2
    Last Post: 03-19-2015, 10:49 PM
  2. Run bash script via button in interface builder?
    By Bwoe in forum OS X - Development and Darwin
    Replies: 5
    Last Post: 03-23-2011, 09:58 PM
  3. Help with the bash...alias/script...?
    By Antares in forum OS X - Development and Darwin
    Replies: 2
    Last Post: 10-26-2009, 01:32 PM
  4. How Do I Get Finder to Execute a Bash Script?
    By mac57 in forum OS X - Development and Darwin
    Replies: 7
    Last Post: 06-08-2006, 04:01 PM
  5. Bash Script
    By ftjogoh in forum Apple Notebooks
    Replies: 1
    Last Post: 11-04-2004, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •