#1
  1. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486

    Bash question: Dealing with spaces in filenames


    As I have no Bash exerience, a 'friend' conjured up this script to perform an operation on all the files in a directory. However, the script does not work on files with spaces in the filename (it returns an error that the file was not found). How can I modify the script so that it will support files with spaces:
    Code:
    #!/bin/bash
    PWD=`pwd`
    for file in `find $PWD -name "*.mp3"`
    do
      eyeD3 --force-update --set-encoding=utf8  $file
    done
    Converting the spaces to underscores is not an option, because that would disturb delicate the wife/linux balance in our household.

    If the Linux forum would be a better place for a Bash question, then tell me and I will request to have the thread moved there (or I will start a new one). Thank you.

    Comments on this post

    • ishnid agrees : lol "delicate wife/linux balance" - that's won my "quote of the week"
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  2. #2
  3. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    I'll report the post so a friendly mod can move it.

    let's use sed to escape spaces

    sed -e 's/\ /\\\ /g' should do that

    for file in `find $PWD -name "*.mp3" | sed -e 's/\ /\\\ /g'`
    do
    eyeD3 --force-update --set-encoding=utf8 $file
    done
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    hmm looking at this post made me get back to bash... heh im falling a sleep again
  6. #4
  7. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    Originally Posted by xlordt
    hmm looking at this post made me get back to bash... heh im falling a sleep again
    sod bash, sister, perl can do it all and more
  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by LinuxPenguin
    sod bash, sister, perl can do it all and more
    fine... i will see what i can do with stinky perl
  10. #6
  11. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    Originally Posted by xlordt
    fine... i will see what i can do with stinky perl
    far more than you can imagine
  12. #7
  13. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486
    I got it moving with this:

    Code:
    find . -name "*.mp3" -exec eyeD3 --force-update --set-encoding=utf8 '{}' \;
    I didn't write that! It was suggested to me on the mailing list.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  14. #8
  15. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    The key is the quotes around the filename (denoted by {} in the above post). If you put those quotes around $file in your original script, it'd work fine too.
  16. #9
  17. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    Originally Posted by ishnid
    The key is the quotes around the filename (denoted by {} in the above post). If you put those quotes around $file in your original script, it'd work fine too.
    Disagree, because i had the exact same problem, and quoting around it did bugger all (much to my confusion)

    Comments on this post

    • ishnid agrees
  18. #10
  19. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Right you are:- silly me.

    The quoting isn't the problem at all it seems. It's to do with the format ``find'' produces its output in.

IMN logo majestic logo threadwatch logo seochat tools logo