(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"?)
See section 9.1.5 http://www.gnu.org/software/findutils/manual/html_node/find_...
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.-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.
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.
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?
-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.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
find . -name '.py' -exec grep -H import '{}' \;Thanks for the laugh though.
I don't think that works. That'll grep for files named '\import\',
grep -r -l import *.py
grep 'import' `find -name '* .py'`
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...
$ 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.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? :)
$ 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)Thanks for the link, that's more interesting than the submission. :)
$ grep -R --include=\*.py import .
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.