News for hackers. Stuff that matters.
Re:Things you (probably) didn't know about xargs by z_ on Sunday June 26, 2011 @12:19PM (#2698315)
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.

Re:Things you (probably) didn't know about xargs by bellaire on Sunday June 26, 2011 @12:30PM (#2698339)
Using 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.

Re:Things you (probably) didn't know about xargs by ibejoeb on Sunday June 26, 2011 @01:00PM (#2698405)
I 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"?

Re:Things you (probably) didn't know about xargs by prodigal_erik on Sunday June 26, 2011 @05:10PM (#2699096)
I 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.
Re:Things you (probably) didn't know about xargs by cstejerean on Sunday June 26, 2011 @01:46PM (#2698521)
I 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.
Re:Things you (probably) didn't know about xargs by ibejoeb on Sunday June 26, 2011 @02:14PM (#2698614)
Don'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.

Re:Things you (probably) didn't know about xargs by kaens on Sunday June 26, 2011 @11:04PM (#2699925)
I guess the question then becomes "why is xargs with a pipe easier?", or "why do you view xargs with a pipe as easier?".
Re:Things you (probably) didn't know about xargs by albemuth on Sunday June 26, 2011 @02:01PM (#2698571)
Not sure if this is a zsh thing only, but I do this:

    grep import **/*.py
Re:Things you (probably) didn't know about xargs by sjs on Sunday June 26, 2011 @03:41PM (#2698882)
I 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 $
Re:Things you (probably) didn't know about xargs by DanielRibeiro on Sunday June 26, 2011 @12:37PM (#2698355)
Ack-grep[1] is also really nice for making such find/grep operations a simple command.

[1] http://betterthangrep.com/

Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @01:10PM (#2698435)
Please 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.
Re:Things you (probably) didn't know about xargs by dfc on Sunday June 26, 2011 @04:43PM (#2699030)
"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.

Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @05:28PM (#2699143)
I 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

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.

Re:Things you (probably) didn't know about xargs by skimbrel on Sunday June 26, 2011 @12:45PM (#2698382)
Ack 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.

Re:Things you (probably) didn't know about xargs by rubinelli on Sunday June 26, 2011 @01:23PM (#2698467)
"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.

Re:Things you (probably) didn't know about xargs by e40 on Sunday June 26, 2011 @02:02PM (#2698575)
It'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.)
Re:Things you (probably) didn't know about xargs by Jach on Sunday June 26, 2011 @01:20PM (#2698464)
One 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.
Re:Things you (probably) didn't know about xargs by vrotaru on Sunday June 26, 2011 @05:17PM (#2699114)
You 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.