Find the answer to your Linux question:
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 ...
  1. #1
    Linux 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.
    Code:
    #!/bin/bash
    for file in `find "$1" -iname "*mp3" | awk '{print "\""$0"\""}'`; do
    	echo "$file"
    done
    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:
    "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"
    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"
    Does anybody have any ideas how to solve this? I am quite new to bash scripting, though not at all new to programming.

    Thanks,

    Dave
    - EndianX -

  2. #2
    Linux 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

  3. #3
    Linux 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

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    another way without meddling with IFS, use the while loop instead
    Code:
    find ..... | while read FILE
    do
     # do something with $FILE
    done
    of course, use quotes around variables whenever possible

  5. #5
    Linux Newbie
    Join Date
    Aug 2005
    Location
    Sterling, VA
    Posts
    100
    Thank you all very much.
    - EndianX -

Posting Permissions

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