Not only is a for
loop wrong -- sed
is also not the right tool for this job.
while IFS= read -r -d '' dir; do
cp "$dir" "${dir/foo/}"
done < <(find . -type d -name '*foo' -print0)
Using -print0
on the find
(and IFS= read -r -d ''
) ensures that filenames with newlines won't mess you up.
Using the < <(...)
construct ensures that if the inside of your loop sets variables, changes directory state, or does similar things, those changes will survive (the right-hand side of a pipeline is in a subshell in bash, so piping into a while loop would mean that any changes to the shell's state made inside that while loop would be discarded on its exit otherwise).
Using ${dir/foo/}
is vastly more efficient than invoking sed
, as it does the string replacement internal to bash.