Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a script and I'm stuck, even with the help of the google.

Here's my code:

for FOLDER in `find . -type d | sed "s#^.#$(pwd)#" | sed 's/ /\ /g'`
    echo "$FOLDER"

This will replace only the first instance of " " with "\ " but not the rest.

Any ideas?

share|improve this question
up vote 3 down vote accepted

Forget about structuring your command that way and use find -execdir instead:

find . -type d -execdir sh -c 'pwd' \;

Or use zsh:

for d in **/*(/); do
  echo $d
share|improve this answer
The first command cut off a lot of paths in the output but the zsh command worked perfectly! Thank you so much! – MonkeyWrench32 Sep 3 '10 at 7:26

You can use xargs instead of a loop for executing the same command on a list of elements. There are options to take care of whitespaces.

share|improve this answer
Add -print0 to the find command, and specify -0 to xargs – Not Sure Jun 5 '11 at 22:39

I think that you are complicating everything.

Maybe you only need this:

find $(pwd) -type d | sed 's/ /\\ /g'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.