[CLUE-Tech] Re: removing spaces from filenames

Angelo Bertolli angelo at freeshell.org
Mon May 17 21:22:28 MDT 2004


Yeah, I think I might need it.

>>Right now I"m thinking about piping find through sed and using mv to 
>>rename all the files without spaces, but I wanted to see if anyone else 
>>had something better...
>>    
>>
>
>I have a Perl script which does this. Let me know if you want it.
>
>jed
>  
>

>>The for loop in bash treats the spaces in file names as different 
>>"elements" for the loop.  This is really annoying as I want to process a 
>>bunch of files with spaces in them.  Is there any way I can get around 
>>this?  Putting " " around the names doesn't seem to help.
>>    
>>
>
>No, double quotes wouldn't work. Have you tried single quotes?
>
>  
>
Yes, but it doesn't help.  It either uses the variable literally, or it 
creates insanely long lines of all the files in the in the directory.

> and there you go.  The way to get the expected behavior is:
>
> for file in * ; do
>   touch "$file".bak
> done
>
> The quotes make $file expand into one word for the touch command. 
> Single quotes won't work as they'll prevent the expansion of $file.
>
Yes, I have tested this out with echo.  In fact the problem is with the 
for line because each time through the loop it parses based on spaces!  
It's quite annoying.  I've tried every combination of putting quotes in 
the for part and in the body.  You'd think that spaces in file names are 
common enough to be handled, but it seems like if you do for file in *, 
it splits the loop on spaces, but I don't exactly understand the 
behavior of for file in "*" which doesn't seem to work either.

> If you're doing something besides file globbing in the for line things 
> might be different.  If you want to list files rather than use *, you 
> have to quote their names on the for line and also quote the variable 
> expansion.

Maybe I'll keep trying.





More information about the clue-tech mailing list