News for hackers. Stuff that matters.
Re:Things you (probably) didn't know about xargs by sabat on Sunday June 26, 2011 @01:12PM (#2698441)
Recursively find all Emacs backup files and remove them find . -name '~' | xargs rm*

Recursively find all Python files and search them for the word ‘import’ find . -name '.py' | xargs grep 'import'*

Hmm. I don't mean to be a tweak, but you don't need xargs to do either of those things. Just:

find . -name '~' -delete

find . -name '.py' | grep 'import'

Note: I can't figure out how to get an asterisk to show up, and don't have time to look it up.

Re:Things you (probably) didn't know about xargs by nuxi on Sunday June 26, 2011 @02:36PM (#2698686)
The -delete option is not available on all UNIX systems, as it's not part of the POSIX spec (http://pubs.opengroup.org/onlinepubs/009604599/utilities/fin...).

(Also, rant rant, I really don't understand why find was extended with -delete in the first place. What's next, "ls --delete" or maybe "cat --grep"?)

Re:Things you (probably) didn't know about xargs by jfb on Sunday June 26, 2011 @03:01PM (#2698751)
The orthogonality ship has sailed with Unix, for better or (IMHO) for worse. The Unix Haters were right about this, among other things, and the sections of the handbook about the shell are still valid, even as time and Moore have rendered others quaint.
Re:Things you (probably) didn't know about xargs by nuxi on Sunday June 26, 2011 @04:55PM (#2699050)
For worse, I concur. I still fire up my SGI boxes once in a while to remind myself things didn't use to/don't have to be as binary as they mostly are today (Linux vs BSD, iOS vs Android, Intel vs AMD etc.). That, and to run electropaint, of course. =)
Re:Things you (probably) didn't know about xargs by ars on Sunday June 26, 2011 @05:11PM (#2699098)
-delete was added to find because of the race condition with doing it using xargs.

See section 9.1.5 http://www.gnu.org/software/findutils/manual/html_node/find_...

Re:Things you (probably) didn't know about xargs by mturmon on Sunday June 26, 2011 @11:00PM (#2699918)
Thanks for that link.

It walks through many of the same issues as the OP, but with more sophistication.

It also explains "+", which is used in place of the traditional ";" to essentially get xargs type argument accumulation, but within find.

That is,

  find . -name '*~' -exec rm {} \+
I did not know about that. It's in Mac OS 10.6 find, for one.
Re:Things you (probably) didn't know about xargs by nuxi on Sunday June 26, 2011 @06:16PM (#2699271)
That was already fixed with -print0 | xargs -0, but then this solution is dismissed with "The problem is that this is not a portable construct;...". The -delete isn't either, so this is a straw man argument, although it probably is the most efficient and secure of all.
Re:Things you (probably) didn't know about xargs by ars on Sunday June 26, 2011 @06:30PM (#2699310)
Read it again.

-print-0 | xargs -0 does not fix the race condition.

The problem is someone can swap in a symlink after the find, and before the xargs.

Re:Things you (probably) didn't know about xargs by nuxi on Monday June 27, 2011 @04:25AM (#2700592)
I'll have to run some more tests, but I can't see how -delete would help in that case .
Re:Things you (probably) didn't know about xargs by ars on Monday June 27, 2011 @05:13AM (#2700676)
Because find changes to the directory first (carefully not following symlinks), and then deletes the file from there.

It does not delete the file using the entire path (which may contain a sudden symlink).

It's not possible to do this safely using xargs.

Take a look also at -execdir which does the same thing - changes to the directory first, and runs things from there. -exec is not safe and should not be used.

xargs is not safe if you are running against a directory not your own. You should use find and -execdir instead.

Yes, the original authors of posix made a mistake here.

> Also, rant rant, I really don't understand why find was extended with -delete in the first place.

I'm hoping you understand it now.

Re:Things you (probably) didn't know about xargs by nuxi on Tuesday June 28, 2011 @06:07AM (#2704788)
Yes, thanks for the extensive info.
Re:Things you (probably) didn't know about xargs by uriel on Sunday June 26, 2011 @08:08PM (#2699537)
Rob Pike and Brian Kernighan warned about this trend in their seminal paper "Program Design in the UNIX Environment" (also known as "cat -v considered harmful" which describes how proper unix programs should be designed:

http://harmful.cat-v.org/cat-v/

Re:Things you (probably) didn't know about xargs by RexRollman on Sunday June 26, 2011 @02:49PM (#2698717)
I did a quick look online to see who has it. GNU, DragonFly BSD, NetBSD, and FreeBSD all have a -delete option. OpenBSD seems to be the only one that doesn't (although its online manpage does give examples on how to do it with -exec and piping to xargs).
Re:Things you (probably) didn't know about xargs by nuxi on Sunday June 26, 2011 @04:34PM (#2699009)
That's basically two flavours of UNIX (Linux and BSD), but that's OK - others are unfortunately either dead or dying. The UNIX family tree (http://www.levenez.com/unix) has been shrinking so rapidly in the last few years, that the current situation looks like the early years.
Re:Things you (probably) didn't know about xargs by RexRollman on Sunday June 26, 2011 @09:43PM (#2699740)
Since the BSDs are all independently developed, I don't consider them to be one Unix, even though they share ancestry.
Re:Things you (probably) didn't know about xargs by bradleyland on Sunday June 26, 2011 @05:13PM (#2699104)
I'm not an expert by any means, so please provide correction/feedback if I'm wrong, but I've read that xargs is more efficient because it can send parameters to the cmd in batches. I've read this in contrast to `-exec {}` not `-delete`, however.

For example:

    find . -name '*~' -exec 'rm {}'
The statement above executes `rm result` for every result. By contrast:

    find . -name '*~' | xargs rm
The example above would group the results and pass them to rm like this:

    rm result1 result2 result3 result4
Because I'm not an expert, I don't know how many parmeters it will pass, or if initiation of many new processes has a significant performance impact on newer systems. I would suspect that for anything involving the disk, I/O will be the bottle neck, not process turn-up time.

Anyone have an opinion/insight?

Re:Things you (probably) didn't know about xargs by LukeShu on Sunday June 26, 2011 @07:18PM (#2699412)
-delete will be the fastest because find can do everything, and it already has the file loaded, and doesn't have to do a second lookup.

-exec will be slower because for each file it has to spawn a new process (this is where most of the time goes), and then that new process has to look up the file.

xargs will be faster than -exec because it will collect a few hundred, or thousand, filenames, and pass them all to one command (the article claims 4096 is the default, on some systems it may be lower). This means that typically, only 3 processes need to spawn: `find', `xargs', and one `rm'; instead of find, and many, many `rm's.

Now, xargs is still slower than -delete because it will buffer the filenames, either waiting for the list to end, or 4000 filenames to pass to `rm'. Then, rm must look up the file from the filename.

To make my point I just set up a test situation with 1110 files ending with `~', among a total of 2221 files. I tested how fast it is to delete all the files ending with `~' using the 3 following commands:

   $ find . -name '*~' -delete
   $ find . -name '*~' -exec rm {} \;
   $ find . -name '*~' -print0 | xargs -0 rm

         | real   | user   | sys
 -delete | 0.024s | 0.003s | 0.020s
 -exec   | 0.819s | 0.007s | 0.107s
 xargs   | 0.073s | 0.007s | 0.017s
I probably should have run those tests multiple times, and taken the average, but meh.
Re:Things you (probably) didn't know about xargs by bradleyland on Monday June 27, 2011 @09:23AM (#2701188)
Very cool. Thanks for the insight.

Mind if I ask how you timed execution?

The take aways for me are:

* Use find's built-in options where possible

* Use xargs where a built-in method isn't available

* Use -exec only when nothing else will do

Re:Things you (probably) didn't know about xargs by LukeShu on Friday July 01, 2011 @02:45PM (#2719623)
I timed it with the `time' command :P
Re:Things you (probably) didn't know about xargs by planckscnst on Monday June 27, 2011 @09:22AM (#2701185)
Most systems have a find command that supports the "-exec command {} +" option, which does not spawn the command for each result, but instead puts as many arguments as the shell will allow on the command line.
Re:Things you (probably) didn't know about xargs by yason on Sunday June 26, 2011 @02:06PM (#2698590)
Nobody remembers the various command line switches of find or grep. I use xargs quite often because it's a very simple concept and I know how to mix and merge any commands together with it and that's something I've only had to to go ahead and learn once.
Re:Things you (probably) didn't know about xargs by baddox on Sunday June 26, 2011 @04:49PM (#2699041)
The grep one didn't involve a switch on grep. It was just the xargs example with xargs removed.
Re:Things you (probably) didn't know about xargs by cstejerean on Sunday June 26, 2011 @09:00PM (#2699627)
The grep one is also wrong as it does an entirely different thing without xargs.
Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @01:13PM (#2698447)
No. You're looking for files having import in their path here, not in their contents.
Re:Things you (probably) didn't know about xargs by sjs on Sunday June 26, 2011 @03:35PM (#2698857)
He got the command wrong but his point stands:

    find . -name '.py' -exec grep -H import '{}' \;
Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @05:21PM (#2699125)
No he doesn't. That's a completely stupid compared to xargs as soon as you have more than a few files. You're starting one grep process per file.

Thanks for the laugh though.

Re:Things you (probably) didn't know about xargs by sjs on Sunday June 26, 2011 @07:21PM (#2699421)
All of this is stupid compared to just using a good shell and doing grep import * * /*.py.
Re:Things you (probably) didn't know about xargs by ori_b on Sunday June 26, 2011 @04:06PM (#2698951)
find . -name '.py' | grep 'import'

I don't think that works. That'll grep for files named '\import\',

Re:Things you (probably) didn't know about xargs by beambot on Sunday June 26, 2011 @04:39PM (#2699017)
I use xargs for the find + rm cases he mentions. However, if you just want the file names containing the words import, I rely exclusively on grep:

grep -r -l import *.py

Re:Things you (probably) didn't know about xargs by cstejerean on Sunday June 26, 2011 @01:33PM (#2698491)
You are correct about -delete. You could also use -exec to execute arbitrary commands for each file returned by find. I needed some examples however, and I often see "find | xargs" because people don't know, or forget, the options to find. I should write a follow up post on find.
Re:Things you (probably) didn't know about xargs by Jach on Sunday June 26, 2011 @01:17PM (#2698458)
In addition to ptramo, you really want:

grep 'import' `find -name '* .py'`

Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @01:23PM (#2698469)
Well, you want xargs as soon as your project is big, because you'll reach the limits on arguments fairly quickly.

I won't go too deep on the issues this will trigger if your filenames contain special characters. We often think of spaces, but you could have some nastier stuff. If you want to sound smart at your next geeks reunion, simply read http://www.dwheeler.com/essays/fixing-unix-linux-filenames.h...

Re:Things you (probably) didn't know about xargs by derobert on Sunday June 26, 2011 @02:41PM (#2698697)
On (modern, i.e. 2.6.23 and later) Linux at least, you won't hit them quickly:

    $ getconf ARG_MAX
    2097152
and if you do hit them, they're very easy to raise:

    $ ulimit -s 32768
    $ getconf ARG_MAX
    8388608
xargs is still useful for its other features, of course.
Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @05:20PM (#2699120)
# echo "2097152/$(echo "/my/still/quite/reasonable/path/to/a/file"|wc -c)"|bc 49932

linux-2.6 # git ls-files|wc -l 36747

Getting close! :)

And I would guesstimate (Linux, kernel >= 2.6.23) to still be a fairly small amount of the machines people interact with professionally through a command line.

And if in a script/snippet, you often want to cover a vast majority of the systems you _could_ end up with. Won't be System III, at least for me, but there has to be a RHEL5 system in a closet right? :)

Re:Things you (probably) didn't know about xargs by LukeShu on Sunday June 26, 2011 @07:29PM (#2699443)
That's the kernel's limit, Bash (3) has its own limit, which is significantly lower, I've hit it before. I think Bash 4 can do an arbitrary number of arguments, within the kernel's limit.
Re:Things you (probably) didn't know about xargs by derobert on Monday June 27, 2011 @06:44PM (#2703278)
No, bash doesn't and didn't have its own limit. On an ancient system (Debian Sarge), ARG_MAX=131072:

    $ bash --version
    GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
    Copyright (C) 2002 Free Software Foundation, Inc.
    $ strace bash -c '/bin/echo `seq 1 30000`' 2>&1 | grep exec
    execve("/bin/bash", ["bash", "-c", "/bin/echo `seq 1 30000`"], …) = 0
    execve("/bin/echo", ["/bin/echo", "1", …) = -1 E2BIG (Argument list too long)
As you can see, the argument list too long error came back from the execve syscall, i.e., from the kernel. (Note that I shortened the strace output to make it fit the page)
Re:Things you (probably) didn't know about xargs by Jach on Sunday June 26, 2011 @01:41PM (#2698508)
Of course, I meant my version as an alternative to xargs. And I think you have about 128k of space for the filenames, but yeah with large projects that can be a problem.

Thanks for the link, that's more interesting than the submission. :)

Re:Things you (probably) didn't know about xargs by ptramo on Sunday June 26, 2011 @05:09PM (#2699092)
Fair point, I just submitted it :)
Re:Things you (probably) didn't know about xargs by MostAwesomeDude on Sunday June 26, 2011 @01:45PM (#2698516)
Or maybe, if you have GNU grep:

    $ grep -R --include=\*.py import .