grep (or anything else) many files with multiprocessor power

find . -type f -print0 | xargs -0 -P 4 -n 40 grep -i foobar
xargs -P N spawns up to N worker processes. -n 40 means each grep command gets up to 40 file names each on the command line.

By: ketil
2009-08-05 23:18:44

1 Alternatives + Submit Alt

What Others Think

Since grep is mostly IO-bound, I doubt it will benefit much from running in parallel.
spatz · 679 weeks and 5 days ago
I don't see how the "-n 40" is needed nor relevant. Any clarification on why you use that?
linuxrawkstar · 679 weeks and 4 days ago
the xargs man page *specifically* recommends using the -n with -P, and explains why...
eichin · 678 weeks and 6 days ago
Yes, I guess the output could be interleaved in some cases (perhaps lines longer than BUFSIZ). It seems parallel does some input/output trickery to avoid this. It can also force output order to be the same as the input order, despite tasks finishing out of order. But this is only an issue if the result of the executed program is output to stdout. If your program writes somewhere else, or does something else entirely, xargs (which is probably more common) does the trick.
ketil · 653 weeks and 6 days ago
basically, any movie base on something that is called story and their character that how good they are performing with written story so you can learn this script writing with Top British Essays who are professionals also can write on any given topic with perfections. you can be sure that how good this is for many peoples who have guts to do with no error at all
Falk1993 · 90 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: