Hacker News new | past | comments | ask | show | jobs | submit login

I've struggled to understand 'find' for a while. It doesn't seem to fit *nix conventions in its syntax. For one, the path argument comes before any options or flags. Also, what is the point of the '-print' option? Pretty much every other utility prints results to stdout by default. This is expected and necessary for piping commands together.

I'm not arguing about the value of 'find' (indeed, this recent article well explains its usefulness: http://news.ycombinator.com/item?id=2698180). I just fumble every time I try to use it because it seems to contravene conventions. I welcome any historical context or clarification if my expectations are in fact flawed.




I've had the same problem. find and dd two *nix commands which betray the Principle of Least Surprise. I still use them, but I always find myself having to think twice before I do.

(Though you should probably always think twice before using 'destroy disk' anyway!)


> For one, the path argument comes before any options or flags.

Actually, it does conform to unix conventions:

   find [OPTIONS] [PATHS] [EXPRESSION]
Where options are "-PLHDO" in the GNU version. The elements of EXPRESSION may be introduced by a dash, but they're not really options. Together, they specify a little program to evaluate on every path. The program could have easily taken the form 'type f a name foo' instead of '-type f -a -name foo'. The only issue would be recognizing the end of PATHS arguments and the beginning of EXPRESSION arguments...perhaps that's why the dash is used? It does seem like OPTIONS EXPRESSION [--] PATHS would be easier and less ambiguous to parse.

> Also, what is the point of the '-print' option? Pretty much every other utility prints results to stdout by default.

find does print to stdout by default. Perhaps -print was added later to distinguish the default mode from -print0, which is common when pairing with xargs.

> I welcome any historical context or clarification if my expectations are in fact flawed.

I was kind of hoping the recently rehabilitated UNIX 1972 sources would shed some light on your questions. Unfortunately, find is present but only as a PDP-11 executable [1].

[1] http://code.google.com/p/unix-jun72/source/browse/#svn%2Ftru...


> find does print to stdout by default. Perhaps -print was

> added later to distinguish the default mode from -print0,

> which is common when pairing with xargs.

  find ... -print0 | xargs -0 $cmd 
is essential if you don't want all sorts of terrible things happening because a filename happens to contain a space or some sort of shell metachar.

See http://en.wikipedia.org/wiki/Xargs#The_separator_problem for the details.

Personally, I use

  find ... -exec $cmd '{}' \; # calls $cmd once for each match

  find ... -exec $cmd '{}' + # calls $cmd with as matches as possible - minimising number of invocations of $cmd.
I think this is either reasonably new, or not entirely standard, since I've used versions that don't have it.

  -ok $cmd \;
is also pretty neat - same as exec, but asks for confirmation first.


It's a _very_ old command, predating many of the command conventions. As the OpenBSD man page puts it:

    HISTORY
         A find command appeared in Version 1 AT&T UNIX.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: