for name in `ls` and filenames with spaces

Yola picture Yola · Dec 27, 2011 · Viewed 30.5k times · Source

next code doesnt work because of spaces in file names, How to fix?

IFS = '\n'
for name in `ls `
do
    number=`echo "$name" | grep -o "[0-9]\{1,2\}"`
    if [[ ! -z "$number" ]]; then
        mv "$name" "./$number"
    fi
done

Answer

fge picture fge · Dec 27, 2011

Just don't use command substitution: use for name in *.