Chris's Wiki :: blog/unix/FindWithoutXargsToday Commentshttps://utcc.utoronto.ca/~cks/space/blog/unix/FindWithoutXargsToday?atomcommentsDWiki2021-02-20T11:10:08ZRecent comments in Chris's Wiki :: blog/unix/FindWithoutXargsToday.By Reuben Thomas on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:78696b070ffe82a199f6c4e3a837a6c2fdaf8a0bReuben Thomas<div class="wikitext"><p>GNU find's manual has documented these tricks since 2007, with the workaround for argument position being fixed, I see, in 2010. The `+` option was implemented in 2005.</p>
<p>And I only found out about it thanks to this article; many thanks!</p>
</div>2021-02-20T11:10:08ZBy Anonymous on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:0f891341668252775f1510bfa6fb018bbd522bd0Anonymous<div class="wikitext"><p>You can get around the limitation of {} needing to be at the end of the command by using sh:
find . -exec sh -c 'exec mv -- "$@" destination/' sh {} +</p>
</div>2021-02-08T16:57:47ZBy Icarus Sparry on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:2d9eff1704718246af5be9c09bc4fac0861d0774Icarus Sparry<div class="wikitext"><p>The "find ... -exec ... {} +" was introduced by David Korn 30 or so years ago.</p>
</div>2021-01-31T21:29:03ZBy Anonymous on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:83bb59532b93b8a01e802d321b06c4b51a6e6f39Anonymous<div class="wikitext"><blockquote><pre>
Both the specification and actual find implementations require the '{}' to be at the end of the command, instead of anywhere in it. This means you can't do something like 'find ... -exec mv {} /some/dir +'.
</pre>
</blockquote>
<p>Odd. On at least Fedora 33 / find 4.7.0, this (not having '{}' at the end) works as expected when I use exec with ';'. Only when I use the '+', I get an error 'find: missing argument to `-exec''. If I recall correctly, not having '{}' at the end in combination with ';' also worked on AIX as far back as 4.3.3.</p>
<p>In other words, this works for me :</p>
<blockquote><p>find . -type f -exec mv {} /somedir \;</p>
</blockquote>
</div>2021-01-31T17:45:05ZBy Aristotle Pagaltzis on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:77222f1466ee881507617a71dfe29779512f5d0dAristotle Pagaltzishttp://plasmasturm.org/<div class="wikitext"><p>Note too that modern <code>find</code> also has <code>-delete</code>, which removes the need to spawn <code>rm</code> (whether via <code>xargs</code> or even just <code>-exec … +</code>).</p>
<p>This isn’t just a cleaner shortcut, it also takes away all the overhead that even <code>find | xargs</code> still has, which means the basic Unix toolbox can get you out of something-created-50-million-files-in-one-directory messes. Previously this level of (non)overhead was only accessible via something like a <code>perl -e 'unlink while readdir'</code>-type oneliner.</p>
<p>(This exists in <code>find</code> in the libre BSDs and thus also on macOS as well.)</p>
</div>2021-01-28T06:29:19ZBy Barry on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:99a798c50f92acca9d80acb6dbe39fa86e28ef82Barry<div class="wikitext"><p>It seems to work on Solaris 9 and AIX 7.1, neither of which is exactly new.</p>
<p>I wish I'd found out about this feature years ago!</p>
</div>2021-01-28T02:32:13ZBy Johannes on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:27398728e9f67fff014d6dd0795291bced86e454Johanneshttps://comam.es<div class="wikitext"><p>Additionally, xargs has the -P argument that allows paralellization and it's a gain in modern systems with more than one core. As far as I know find itself doesn't allow nothing similar.</p>
</div>2021-01-27T20:55:07ZBy sapphirepaw on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:d72343868d1c19ddd061ffbcf1d2f4ccf26aef88sapphirepawhttps://www.sapphirepaw.org/<div class="wikitext"><p>I can confirm that macOS Catalina supports <code>find … -exec … +</code> form. I didn't really think about it; I've been assuming find will complain if it <em>doesn't</em> support it.</p>
<p>No complaints so far :)</p>
</div>2021-01-27T20:55:06ZBy Stephen Kitt on /blog/unix/FindWithoutXargsTodaytag:CSpace:blog/unix/FindWithoutXargsToday:8d12ab735a52681e22d0fa25fffad4a28b8911faStephen Kitt<div class="wikitext"><p>GNU mv has a useful "mv -t" variant which works around the "-exec ... {} +" limitation (and avoids various sources of ambiguity); same goes for cp.</p>
<p>You might like "-execdir" on find implementations which have it; it avoids some race conditions on the paths involved by running commands from the directory containing the files to be processed.</p>
</div>2021-01-27T06:01:01Z