[buug] Spaces within filenames in a bash script

Brian Dessent brian at dessent.net
Mon Jan 2 02:00:09 PST 2006

Joseph Zitt wrote:

> #!/bin/bash
> FILES=$(ls *.flac | cut -d '.' -f1)
> for i in $FILES; do
>          echo converting: $i.flac
>          flac -sdc $i.flac | lame - $i.mp3
> done

You should consider using find instead of ls.  And it looks like what
you have would die if you had a filename with more than one "." in it.

# untested
find . -name \*.flac | while read F; do
    echo "converting: $F"
    flac -sdc "$F" | lame - "$(basename "$F" .flac).mp3"

By the way, just for simple commands the best way to do this is:

find /path -name \*.whatever -print0 | xargs -0 command

...which will handle filenames with spaces, quotes, tabs, newlines, and
anything else.


More information about the buug mailing list