Please use [code][/code]
tags around your code, to preserve formatting and to improve readability.
As rikxik has demonstrated, the secret is to not use a for
loop, but a while+read
loop instead, possibly while also changing the delimiter to null.
Also, you could just do the whole deletion operation with find. GNU find even has a -delete
option built-in. But you can also use the -exec
option to call rm, or pipe the command into xargs
(again, using the null separator to handle files with spaces).
Here are a couple of links about find:
The move operation can likely be made cleaner too, using parameter expansion
for file in *;
mv "$file" "$newfile"
A for loop can be used here, since globbing expansion happens after word-splitting is finished.
I'm not 100% I understand the purpose of the sed expression 's/ */_/g
' here though. Are you trying to replace a literal space+asterix
string (as I set it for above)? Because as written, sed treats it as a regex "zero or more spaces", and thus inserts underscores between every character. If that's what you want, then unfortunately the parameter expansion alone wouldn't work, because it uses the asterix as a globbing character, rather than a regex operator.