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.

4
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 · 475 weeks and 6 days ago
I don't see how the "-n 40" is needed nor relevant. Any clarification on why you use that?
linuxrawkstar · 475 weeks and 5 days ago
the xargs man page *specifically* recommends using the -n with -P, and explains why...
eichin · 475 weeks 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 · 450 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: