Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

bash scripting and filenames with spaces

  • 17-12-2004 4:45pm
    #1
    Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭


    Riddle me this, one of ye bright young scholars:

    I've got a shell script that has a section something like this:
    for file in `ls -1A`; do
        cp $file newdir/
      done
    
    Problem is, if any filenames have spaces in them, it fails, treating the separate words as separate filenames.

    Any ideas how to work around this?


Comments

  • Registered Users, Registered Users 2 Posts: 1,865 ✭✭✭Syth


    Try enclosing the file name in "'s. ie
    cp "$file" newdir/
    

    That might work.


  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    Nope. :( Using a test filename "zz z":
    cp: cannot stat `zz': No such file or directory
    cp: cannot stat `z': No such file or directory
    


  • Registered Users, Registered Users 2 Posts: 1,193 ✭✭✭liamo


    How about :

    find . -type f -maxdepth 1 -exec cp {} newdir/ \;


    Liam


  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    Hm. I oversimplified - there's some more code in the do-loop. I'll see if I can work it in, ta.


  • Registered Users, Registered Users 2 Posts: 1,569 ✭✭✭maxheadroom


    oscarBravo wrote:
    Nope. :( Using a test filename "zz z":
    cp: cannot stat `zz': No such file or directory
    cp: cannot stat `z': No such file or directory
    
    Try a single quote - '$file'


  • Advertisement
  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    Tried that - it tries to copy a file called '$file'.


  • Registered Users, Registered Users 2 Posts: 1,569 ✭✭✭maxheadroom


    oscarBravo wrote:
    Tried that - it tries to copy a file called '$file'.

    Hmm - not sure of the exact syntax in bash but have you tried something like:
    $file2 = "\'".$file."\'"
    cp $file2 newdir/
    


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    should work in bash with the above explanations, assuming the directory doesn't have a space and/or you're putting quotes around $dir

    Is it in fact a bash shell and does the script have:
    #!/usr/bin/bash
    
    as the first line?

    Is the first line returning the right result or is the output being parsed already at that stage?


  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    It is a bash shell, the first line is "#!/bin/bash" ('cos that's where my bash is) and it doesn't work.

    Not sure what you mean about the first line returning the right result. In the case of my test file called 'zz z' it's trying to copy zz and z separately to newdir; in effect it's doing this:
    cp zz z newdir/
    
    That's what I'm trying to avoid.


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    OscarBravo wrote:
    Not sure what you mean about the first line returning the right result.

    make a tmp directory and some empty files:
    touch this that "the other"
    then list them with ls -l

    now try:
    for i in `ls -1`
    do
    echo $i
    done


    you should see the problem already at this stage.

    now try this:
    ls -1|while read i
    do
    echo $i
    done


    combine that with the tips above and it should work


  • Advertisement
  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    The boy's a genius!!!

    Cheers pickarooney, it's times like this I miss green apples. :)


Advertisement