1

I've searched around for this but I couldn't find anything on it.

I've always wanted to know, why is it that most other built in bash commands have the format of command -flag arg1 arg2, i.e. cp -r dir1/ dir2/ but here the find command somehow puts the flag in the middle between two arguments?

find path/ -name pattern

It's inconsistent to me and it makes it hard to remember. Is there a good reason for it? Some convention I'm not aware of?

4

The commands came first, consistency was added later.

The earliest manpage you're likely to find shows it as

find pathname expression

find dates back to the 1970s, and the assumptions of ordering and even syntax (whether a dash is needed for options) were added to various commands later (say during the later 1980s and early 1990s) to help users remember the syntax of various commands. In the case of find, for example, the developers were aware of some inconsistencies:

BUGS
There is no way to check device type.
Syntax should be reconciled with if.

So the proper answer is that the command made sense to the original developers, and no significant change was made.

  • Also, there's not really any difference between arguments and flags, they're all just arguments. – EightBitTony Apr 26 '16 at 8:34
  • Sure: but the same information could be passed in different ways. What you have in find is the original design with minor adjustments. – Thomas Dickey Apr 26 '16 at 8:36
  • I agree, I'm just suggesting the OP is placing too much emphasis on the difference between flags and arguments. – EightBitTony Apr 26 '16 at 8:36
  • I'd hazard a guess that the order was originally picked to put the simple mandatory path argument at the start and have the complex variable-length pattern after it. That'd also avoid things like figuring out if "find -name somestring" was missing the path argument or the pattern associated with -name. – Todd Knarr Apr 26 '16 at 8:42
2

You seem to have confused a few things. Neither find nor cp are shell builtins. The only argument in the find command is the path, the rest are options and their values, but that's just semantics, the distinction here is not very important. More importantly, there are two classes of option flags. Those that take arguments and those that don't. For those that do, the most common format is:

command -flag flagValue

While many programs take single letter flags (command -f) many take both single and long options (pretty much all the GNU tools, for example). You mentioned cp, this is a valid cp command:

cp -dR --preserve=all -i --dereference foo bar baz/

In general, the "standard" format, the one that is most common anyway, is actually:

command -flag1 -flag2 value1 -flag3 value2 ARGUMENTS

In other words, many flags are not simple booleans and also take values. Find is no exception there. Even cp which you cite does this:

cp -t /foo -u file1 file2 fileN

So it's not true that most commands have the format command -flag arg1 arg2, quite the contrary in fact. It just depends on the flags used. As for why the find devs chose this particular format, I'm afraid you'll have to ask them.

  • Thanks, very helpful! Unfortunately I can't upvote you but your answer put the other answers into context! Did you figure that out from reading the manpages? – user2193268 Apr 26 '16 at 20:22
  • @user2193268 not as such, no. You pick things up :) – terdon Apr 26 '16 at 21:01
2

find and cpio were created by Dick Haight and not by the people who wrote most of the orginal Unix utilities. There was no commandline argument parsing library at the time that you could link as a library, and that would enforce/stimulate some consistency (in the mid 80s I had source code for getopt on several systems )

Once people use commands and their arguments in scripts it is difficult to change the syntax of existing functionality.

0

find is not a simple command that is controlled by options but a command that implements an own control language.

The find CLI looks like:

find [options] path1..pathn [expression]

Where options are e.g. -H, -L, -P and expression is a script written in the find control language.

The parameters like -name are called: primaries, because they are primary operators and e complete expression can be seen as a mathematical description of a filter and actions like -print, -exec.

The find command language is a very powerful but still easy to learn language. Programs that traverse directory trees and try to implement their behavior via the classical UNIX command line options only are usually either less powerful or hard to learn. See e.g. GNU tar vs. star (the latter supports to use the find command language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.