LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] ILUG] Disadvantages of find -exec?

[ILUG] ILUG] Disadvantages of find -exec?

Brian Foster blf at utvinternet.ie
Tue Jan 13 16:26:20 GMT 2009


  | From: Niall O Broin niall at linux.ie
  | Date: Tue Jan 13 15:52:21 GMT 2009
  |
  | On 13 Jan 2009, at 15:42, Pádraig Brady wrote:
  | > Braun Brelin wrote:
  | >> A few days ago I saw a thread regarding find where someone
  | >> [me! -blf] stated that using find with -exec was a bad idea
  | >> and to use [xargs(1)] instead.   Can someone give me some
  | >> opinions as to what specifically is the drawback of -exec
  | >> with find?  Just curious.
  | >
  | > Note there are actually 2 `find -exec` variants.
  | >
  | >    find -exec command {} ';'
  | >    find -exec command {} '+'
  | >
  | > The later is new and specified by POSIX, and is
  | > equivalent to `find -print0 | xargs -r0`
  |
  | Well, I sit corrected - I assumed that that was such a useful addon
  | to find, it must have been GNU inspired, as very many useful addons
  | to old Unix standards are.

 "-exec ... {} +" was added to POSIX in 2001 (i.e., it's
 relatively new and wasn't in the original 1992 version).
 according to
   http://www.opengroup.org/onlinepubs/009695399/utilities/find.html
 it apparently came from SVr4 (albeit I don't recall it).

 GNU added it quite recently (in v4.2.12 according to the
 manual page), which is probably why many people (including
 myself, I must admit) either don't know about it or are
 not in the habit of using it.

 GNU's -print0 has been in GNU's 'find' for as long as I
 can remember (quite possibly since the first-ish version?),
 but as the above page points out, requires changes to many
 other commands to be useful.  and to this day, I sometimes
 forget the -0 (or whatever) on the command which reads the
 find's output.   ;-(

 there's other arguments against using -exec besides the
 number-of-processes one.  here's two of them:

 1st, it's simply awkward to type (esp.(?) the older
     "-exec ... {} ... \;" form);
  and
 2nd, it tricks people into trying to write what I call
     "find scripts" instead of proper shell scripts.
     there's an extremely useful trick which should be
     in your toolbox:

        find ... -print | while read filename; do
           ...stuff using "$filename"...
        done

     the above can process the output of 'find' however you
     want, easily.  very easily (albeit it breaks on some
     "bad" filenames, such as those containing newlines).
     but people often try to use -exec:

        find ... -exec bash -c '...stuff using "$1"...' -- {} \;

     (or similar) which can be made to work, but is harder
     than it looks.

 b.t.w., GNU 'find' also has -execdir, which is similar to
 -exec except the working directory is first changed to that
 containing the found file before the command's run.  see the
 GNU find(1) manual page for details.

cheers!
	-blf-

-- 
"How many surrealists does it take to    |  Brian Foster
 change a lightbulb?  Three.  One calms  |  somewhere in south of France
 the warthog, and two fill the bathtub   |     Stop E$$o (ExxonMobil)!
 with brightly-coloured machine tools."  |       http://www.stopesso.com


More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell