Google

looping through files with spaces in the filenames in bash

I sometimes have to transfer files with spaces in the names, I like using a for loop, but the usual way doesn’t work. Usual way:
for file in `find . -type f|grep .ext$`
do
   /do/something/to $file
done
To get around this I use a while loop with a read instead. Using the read will read to the end of the line, enclosing within quotes escapes the spaces. Unusual way:
find . -type f|grep .ext$ |while read file
do
   /do/something/to "$file"
done
Or if you don’t like using more than one line…and enjoy the unreadable (I’m transferring all my avi files to another computer in this example)
find . |grep .avi$ |while read file; do scp "$file" 172.16.1.1:; done

13 Responses to “looping through files with spaces in the filenames in bash”

  1. Kyle Says:

    Thank you, thank you, thank you. I’ve been racking my brain trying to get this to work with a “for file in …” loop with no success. This helps a lot.

  2. snovak web development Says:

    This really really helps.. thank you brotha.

  3. Ian Reissmann Says:

    Many thanks for this. I have been writing shell scripts for 20 years and got stuck. I tried many different ways (xargs etc) and was about to resort to a Tcl script.

  4. kolortv Says:

    Thank you so much!

  5. jimjimper Says:

    Thanks for that. Just what the doctor ordered.

  6. Bill Hernandez Says:

    I have struggled with spaces in pathnames forever using bash. This is like on of the best tips ever…

    Thanks a million !

    Solved the BBEdit backup folder problem..

    find /BACKUP_MAIN/BBEdit_Backups -type f \( -name ‘*.txt’ -o -name ‘*.applescript’ \)|while read file
    do
    # echo “$file”
    mdimport -d 1 /Library/Spotlight/SourceCode.mdimporter “$file”
    # mdimport -d 1 /Library/Spotlight/OSAImporter.mdimporter “$file”
    done

  7. Bill Hernandez Says:

    I had the wrong path in my previous message.

    I have struggled with spaces in pathnames forever using bash.

    This is like one of the best tips ever…

    Thanks a million !

    Solved the BBEdit backup folder problem..

    find ~/Documents/BBEdit\ Backups -type f \( -name ‘*.txt’ -o -name ‘*.applescript’ \)|while read file
    do
    # echo “$file”
    mdimport -d 1 /Library/Spotlight/SourceCode.mdimporter “$file”
    # mdimport -d 1 /Library/Spotlight/OSAImporter.mdimporter “$file”
    done

  8. Chad Crabtree Says:

    Interstingly this method still fails with spaces at the end of the file. FYI, however I just did the changes maually since it reduced the number of misses from hundreds to 4. Thanks this is better than the environemtn variable that you can change to get the for loop to work.

  9. Richard Says:

    I’ve searched before for a solution like this. It’s relatively simple, elegant, and I’m pained that it doesn’t seem to be more well known. Thanks.

  10. Edward Says:

    I love you. This has bothered me for ages!

  11. Barton Says:

    Thanks. In my case, I had a list of filenames, so:

    cat list | while read file
    do
    doSomething $file
    done

  12. Unix Commands « Burgundywall.com Says:

    [...] To loop through files that have spaces in the name, try the following example from narrabilis.com [...]

  13. Tom Says:

    Awesome — HUGE thanks!

Leave a Reply