Newbie help with bash script to search and replace a filename

Joined
Sep 25, 2007
Messages
12
Reaction score
0
Points
1
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."
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's 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/'
 
OP
G
Joined
Sep 25, 2007
Messages
12
Reaction score
0
Points
1
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."
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's 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."
 
OP
G
Joined
Sep 25, 2007
Messages
12
Reaction score
0
Points
1
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/'`
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's 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.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top