5 ms·
You can skip the pipe by using find's '-exec' option. It will save you the pipe. The example: "find . -name '* .py' | xargs grep 'import'" would become
by z_ 15y ago
You can skip the pipe by using find's '-exec' option. It will save you the pipe.
The example:
"find . -name '* .py' | xargs grep 'import'"
would become
"find . -name '* .py' -exec grep -H 'import' {} \;".
You need to include the -H in grep to get the filename in which the match occurs.
- bellaire 15y agoUsing xargs with a pipe is easier. I don't know any reason why I'd want to "save a pipe" when working at the command line. Also note that text surrounded by asterisks in your comment become italics. Indent text by two or more spaces to reproduce text verbatim, like for code.
- ibejoeb 15y agoI think it was more about the appropriateness of the examples. z_'s comment is right-on, and I was going to post the same thing. It's a good intro, but the examples are contrived because you just don't need it. It's not necessarily about saving a pipe, but also, when the tool provide first-class support for the function, it's typically less prone to error. For example, the -print0 becomes unnecessary, and I've been burnt by that. I also appreciate the writeups that don't teach poor examples. We all know how prolific copy&paste coding is. How many times have you seen "grep foo bar | wc -l" when you know it's just all-around better to "grep -c foo bar"?
- cstejerean 15y agoI felt bad using contrived examples, but I wanted a short post to cover the basics of xargs without getting detracted with discussions of the options of find. Based on the feedback I've seen here I'll go ahead and update the post though. If you can share some simple but less contrived examples please let me know, I'd love to update the post.
- ibejoeb 15y agoDon't get me wrong; it's a perfectly good and useful tutorial. The meat comes at the end when you talk about parallelism and argument batching. That can make a world of difference when you're working on real-world problems, like moving millions of files (mv * won't work unless you're on a system without ARG_MAX, and even then there are performance implications). I think a good intro to xargs starts with a list of things that you can't do without it. (Easy for me to say that, but of course I haven't written that piece...) It'd be great to know why to use it, not just how, you know what I mean? Anyway, this is just off-the-cuff commentary, not criticism. Thanks for writing it up.
- prodigal_erik 15y agoI would prefer we only teach "grep -c" as a special case optimization to people who already understand how "grep | wc -l" works, because the latter is more generally useful.
- kaens 15y agoI guess the question then becomes "why is xargs with a pipe easier?", or "why do you view xargs with a pipe as easier?".
- DanielRibeiro 15y agoAck-grep[1] is also really nice for making such find/grep operations a simple command. [1] http://betterthangrep.com/ http://betterthangrep.com/
- skimbrel 15y agoAck is awesome. It makes life as a programmer so much easier -- no more fiddling around trying to remember the useful flags for grep. Plus it has --thpppt.
- ptramo 15y agoPlease stop calling it ack-grep. Debian already had an ack package and didn't get their priorities the way I would have wished. That's their problem, not the official name.
- dfc 15y ago"didn't get their priorities the way I would have wished"??? They already had a package named ack. WTF? By "getting their priorities" correct you mean catering to you? The same problem is currently happening with node.js. Its unfortunate that node.js chose node to replace an equally ambiguous and generic name.
- ptramo 15y agoI didn't say "correct". I said "the way I would have wished". I expressed a personal preference, you're making me sound like more of a jerk than I already am. That being said, yes, I believe they're plain wrong on this one. They shouldn't cater to me, but to their users. Thank God they have stats. http://qa.debian.org/popcon.php?package=ack-grep http://qa.debian.org/popcon.php?package=ack-grep http://qa.debian.org/popcon.php?package=ack http://qa.debian.org/popcon.php?package=ack I count in both because I installed the ack package mistakingly. I'm probably not the only one. Still a very large WIN for ack, the one I care about.
- Jach 15y agoOne of the reasons I love xargs so much is the archaic syntax (and surprises based on your input) behind -exec. I think the pipe is cleaner, or using find in a set of backticks.
- rubinelli 15y ago"find . -name '* .py' -exec grep 'import' {} +" will work more like the xargs example, i.e. all filenames will be passed as parameters to a single grep process, so the filename is displayed by default. I usually don't bother with this kind of command line micro-optimization, but in my experience find and grep are the commands for which it's worth knowing and using every option.
- albemuth 15y agoNot sure if this is a zsh thing only, but I do this: grep import **/*.py
- sjs 15y agoI rarely use find because zsh has great globbing. I really wish zsh were the default shell everywhere, most people wouldn't even notice if you changed the prompt from % to $
- e40 15y agoIt's more efficient to use the pipe with xargs. If you're removing 10,000 files, the difference in time between -exec (and fork/exec of rm 10,000 times) and xargs rm is quite significant. (On my system getconf MAX_ARGS is 2180000.)
- vrotaru 15y agoYou save one pipe, but at expense of every grep will spawn its own process. So maybe a 1000 grep processes instead of one. But I've saved a pipe.