Super User is a question and answer site for computer enthusiasts and power users. 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

When I run for i in $(find -name '*.ogg'); do echo '$i'; done I don't get one line per file as I expected. Instead, when a file name has a space in it, each word comes out in a separate line, so I can't run commands on the files.

share|improve this question
up vote 5 down vote accepted
find -name '*.ogg' -print0 | xargs -0 -L1 command
share|improve this answer
Yep, use -print0 to make find use the null character as a result separator instead of CR. – Ivan Petrushev Apr 24 '10 at 5:59
Awesome! Didn't know about xargs. It's like map for the command line :) Thanks! – obvio171 Apr 25 '10 at 17:14

Here is an example of maybe what you want to do:

 #! /bin/sh
 for xxx in `cat /etc/hosts`
    echo $xxx
 exit 0

I.E: Change the IFS (Inter Field Seperator) to \n instead of space,tab,\n

share|improve this answer

You should pipe find into a while loop instead:

find -name '*.ogg' | while read -r i; do echo "$i"; done

Also, the single quotes you had around $i would prevent the variable from being expanded to its value.

Another way is to use process substitution:

 while read -r i; do echo "$i"; done < <(find -name '*.ogg')

which has the advantage of not creating a subshell out of the while loop, so variables set (and other changes to the environment) inside the loop be available after the loop is finished.

share|improve this answer

If you want to run the command in parallel and if have GNU Parallel http:// installed you can do this:

find -name '*.ogg' | parallel do_stuff {} --options otherargs

Watch the intro video for GNU Parallel to learn more:

share|improve this answer

Also there is no need in find in this case. You may set globstar option and use bash globbing.

shopt -s globstar
for i in **/*.ogg; do echo '$i'; done
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .