SlackBuilds.org buildscript templates use find | xargs, not find -exec. Why?
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
SlackBuilds.org buildscript templates use find | xargs, not find -exec. Why?
I am not sure who to address this question, but using find -exec removes the necessity to use -print 0, also saves one pipe, and is generally considered more robust.
Is there a particular reason why the template suggests find | xargs ?
I assume the specific reason is that it's what Slackware's build scripts used before SBo became active.
I also suppose it's because of this...
Quote:
Originally Posted by Lockywolf
I am not sure who to address this question, but using find -exec removes the necessity to use -print 0, also saves one pipe, and is generally considered more robust.
Is there a particular reason why the template suggests find | xargs ?
Where did you find any mentions of the execute flag?
Quote:
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
The "find ... -exec something '{}' \;" idiom runs the "something" command once for every item found. Yet another gotcha has to do with arcane quoting rules concerning braces and the semicolon, which always have meanings for shell scripts/commands.
Using the "find ... -print0 | xargs -0 something" idiom is much more considerate to the system. The "xargs" command knows the maximum (safe) length of a command line, so it can execute the "something" command, via fork()/exec(), the minimum number of times to process all the results from the "find" command. Put simply, is it more efficient to delete a hundred files one at a time, or 20 at a time? One at a time involves 100 invocations of fork()/exec(); 20 at a time involves just 5.
--------
Astute readers may note that the "find" command has an option to use a plus-sign instead of a semicolon in the command line passed to the "-exec" option. That is, "find ... -exec something '{}' \+". It's supposed to build up a compatible list of command arguments, in the style of xargs. Besides the arcane quoting rules cited above, there's also the fact that the "\+" option was added to "-exec" in 2005. The implementation was imperfect; AFAICT, it hasn't been fixed yet. (See findutils-4.6.0-exec-args.patch.gz in the Slackware-current source tree.)
Other astute readers may point out that "xargs permits similar brace-pair substitutions!". But "xargs" builds a command's argument list by default, which is its whole raison d'être; the "-exec" option to "find" is exactly that, an option.
--------
Lockywolf, the example you quote in your original post is a good demonstration of Unix philosophy's benefits. "Do one thing, and do it well." That pipe sequence of six commands clearly defines each of the six steps for finding the files containing executable object code, then peeling off the unneeded ELF stuff, in order to build a usable (but terse) Slackware package. It saves space, in both installed Slackware systems, and Slackware package mirrors. The pipe sequence builds up the list of files to pass to "strip --strip-unneeded", in the most efficient way currently possible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.