How do you use files with spaces in a (Linux)for?
12:45 27 Jan 2026

I use Cygwin under Windows. Under windows I have created files with spaces in them. I would like to remove the spaces. There are enough files so that doing them individually is impracticable. I have tried:

find . -type f | grep ' ' > file
for i in $(cat file); do mv $i `echo $i | tr ' ' '_'`;done

and
find . -type f | grep ' ' | sed -e 's/ /\\ /g' > file
for i in $(cat file); do mv $i `echo $i | tr ' ' '_'`;done

and
find . -type f | grep ' ' | sed -e 's/ /qq/g' > file
for i in $(cat file); do mv `echo $i | sed -e 's#qq#\\ #g'` `echo $i | sed -e 's#qq#_#g';done

The last variant fails with an error message from mv: "target ... No such file or directory" with the target correctly specified.

The for loop treats blanks as a separator and ignores the forward slash (\), the single quote (') and the double quote ("). But, when I put in a specific file, such as:

for i in 'a b c'; do echo $i;done

it recognizes 'a b c' as a single entity.

The failure on the mv command I don't understand. mv is complaining that the file I want to create doesn't exist.

I can't think of any other variant to try.

linux unix