Rename files to no longer have spaces in names

I was having a problem where the for loop split files with spaces into multiple lines.

jamess@greenscar Ace_Of_Base_-_Star_Mark_Greatest_Hits_(2008)_-_flac $ for i in `find . -type f -name "* *"`; do  echo "$i"; rename 's/\ /_/g' "$i"; done;
./CD_1/01.Happy
Nation.flac
./CD_1/02.All
That
She
Wants.flac
./CD_1/03.The
Sign.flac
./CD_1/04.Wheel
Of
Fortune.flac
...

Then I found the IFS

IFS=$'\n'; for i in `find . -type f -name '* *.flac'`; do  echo "$i"; rename 's/\ /_/g' "$i"; done; unset IFS

And she works.

jamess@greenscar sorted $ IFS=$'\n'; for i in `find . -type f -name '* *.flac'`; do  echo "$i"; rename 's/\ /_/g' "$i"; done; unset IFS
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/01 - Thunderstruck.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/02 - Shoot To Thrill.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/03 - Big Gun.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/04 - Fire Your Guns.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/05 - You Shook Me All Night Long.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/06 - Heatseeker.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/07 - Back In Black.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/08 - Who Made Who.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/09 - Hard As A Rock.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/10 - Hells Bells.flac
./ACDC/2008_-_Warning!_High_Voltage_-_Greatest_Hits/CD1_-_Brian_Johnson/11 - Moneytalks.flac
...

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/hncom/public_html/sandlininc.com/wp-includes/formatting.php on line 4803
This entry was posted in Linux / Unix by james. Bookmark the permalink.