Results 1 to 5 of 5
I have hundreds of mp3s with spaces in the names. I want to replace those spaces with underscores. I figured I could easily write a script to do this, but ...
- 12-31-2008 #1Linux Newbie
- Join Date
- Aug 2005
- Location
- Sterling, VA
- Posts
- 100
For Loop, items w/ spaces
I have hundreds of mp3s with spaces in the names. I want to replace those spaces with underscores. I figured I could easily write a script to do this, but was quickly proven wrong. I am failing at my first step: obtaining a list of files.
I realize that for loops iterate over a list of words, not lines. But it was my understanding that by placing the items in double quotes, they would be treated as a word. This is not the case. The find/awk statement gives output such as this:Code:#!/bin/bash for file in `find "$1" -iname "*mp3" | awk '{print "\""$0"\""}'`; do echo "$file" done
However, the results of my script look like this, which is what you would expect if the for loop is using spaces as delimiters, regardless of double quotes.Code:"Music/Riley Lee/Music For Zen Meditation (Disc 02)/Riley Lee - Music For Zen Meditation (Disc 02) - 11 - Breathe The Fragrance Of Forever.mp3" "Music/Riley Lee/Music For Zen Meditation (Disc 02)/Riley Lee - Music For Zen Meditation (Disc 02) - 12 - Deep Night Blues.mp3" "Music/Riley Lee/Music For Zen Meditation (Disc 02)/Riley Lee - Music For Zen Meditation (Disc 02) - 06 - Soaring With The Eagles.mp3"
Does anybody have any ideas how to solve this? I am quite new to bash scripting, though not at all new to programming.Code:"Music/Riley Lee/Music For Zen Meditation (Disc 02)/Riley Lee - Music For Zen Meditation (Disc 02) - 11 - Breathe The Fragrance Of Forever.mp3" "Music/Riley Lee/Music For Zen Meditation (Disc 02)/Riley Lee - Music For Zen Meditation (Disc 02) - 12 - Deep Night Blues.mp3"
Thanks,
Dave- EndianX -
- 01-01-2009 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
Set the Internal Field Separator (IFS) to just newline
Code:#!/bin/bash IFS=$'\012' for file in `find "$1" -iname "*mp3"`; do echo "$file" done
- 01-02-2009 #3Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Alternatively, you can just use the -exec option to get the list of mp3's
find . -type f -iname "*mp3" -exec basename {} \;
and if you want to do more, try this:
find . -type f -iname "*mp3" | while read line; do echo mv $line `echo $line | sed 's/ /_/g'`; done
Remove the "echo" in the previous command if the output shows what you intended to do:
find . -type f -iname "*mp3" | while read line; do mv $line `echo $line | sed 's/ /_/g'`; done
- 01-02-2009 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
another way without meddling with IFS, use the while loop instead
of course, use quotes around variables whenever possibleCode:find ..... | while read FILE do # do something with $FILE done
- 01-03-2009 #5Linux Newbie
- Join Date
- Aug 2005
- Location
- Sterling, VA
- Posts
- 100
Thank you all very much.
- EndianX -


Reply With Quote