Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

5 Answers 5

up vote 5 down vote accepted
find -name '*.ogg' -print0 | xargs -0 -L1 command
share|improve this answer
3  
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
 IFS="\n"
 for xxx in `cat /etc/hosts`
 do
    echo $xxx
 done
 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:// www.gnu.org/software/parallel/ installed you can do this:

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

Watch the intro video for GNU Parallel to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

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

Your Answer

 
discard

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.