4 ms·
I'm curious why you (or anyone) uses -exec? It's often painfully slower than piping through xargs and I find the syntax (the semicolon and braces and the requi
by derriz 9y ago
I'm curious why you (or anyone) uses -exec? It's often painfully slower than piping through xargs and I find the syntax (the semicolon and braces and the required quoting/escaping) uncomfortable. I haven't used -exec in 20 years.
- oconnore 9y agofind ... -ok ... {} \; is easier to type than find ... -print0 | xargs -0 -p -I{} ... {}
- derriz 9y agoInteresting but I was asking about -exec?
- ekns 9y agoIt's not even slower (and it's safe beyond the argv limit) if you use "+" instead of the semicolon. find -type f -exec sed -i s/foo/bar/ {} + This only invokes the command twice for 100k files (assuming we can pass in 64k arguments to sed)
- derriz 9y agoThanks! I never knew about the + terminator.
- pm215 9y agoYou need it if you want to modify the directory structure that find is traversing, so as to affect subsequent traversal, eg http://www.chiark.greenend.org.uk/~pmaydell/find/primes http://www.chiark.greenend.org.uk/~pmaydell/find/primes (cheatsheet here: http://www.chiark.greenend.org.uk/~pmaydell/find/primes-commented http://www.chiark.greenend.org.uk/~pmaydell/find/primes-comm...) Admittedly I wrote that nearly 20 years ago so I guess it doesn't invalidate your point :-)
- kec 9y agoWhat about commands which only take a single file as input? Is xargs -n1 noticeably faster than -exec? (And if it is, is that actually worth it for clarity's sake?)
- asicsp 9y agosee a detailed discussion here: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice https://unix.stackexchange.com/questions/321697/why-is-loopi... especially this quote: > find's business is evaluating expressions -- not locating files. Yes, find certainly locates files; but that's really just a side effect.
- josteink 9y ago> I'm curious why you (or anyone) uses -exec? I already know how to use it. It works. > It's often painfully slower than piping through xargs I've tried it. It has different semantics. First attempt always leads to errors and doing the wrong things. I always have to come up with some workaround to make things work as I expect they would (i.e. how -exec does things by default). This lack of confidence also means I would never dare using xargs for anything remotely destructive (like rm). What reason do I have not to use -exec? Why swap a perfectly perfect hammer for a broken screwdriver? Besides: IMO the timesaving aspects of find is not strictly tied to how fast find runs. It's tied to what I can automate.
- marcosdumay 9y agoIn my opinion, xargs has really niche use cases, and -exec is way more fit for general use. - There's the issue of command line length, pipe too many files and xargs will just fail. - There's predictibility. With -exec you get to see the entire command line structure, what is escaped, what is not, what names go where. With xargs, a couple of badly named files and you are out of luck. - Find's syntax is just more general. Xarg is limited to pushing parameters into a script, while -exec can run whatever scrit you want, with single or multiple parameters, or flags, or whatever. Performance is almost never an issue.
- mmjaa 9y agoYMMV? Having used find -exec for 20+ years, I find it much more difficult to convince myself to switch to tools like this, and just simpler to use -exec. That, and the extra spawn - and potential for disaster - that putting a | into the mix can incur ..