3

So, I have been trying to get into the habit of first looking at man pages before starting to google when I forget how to run a certain command.

I was looking at the man page for the find command today and in the synopsis section it specifies the format of the command as:

SYNOPSIS
       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

Doesn't this show that the options must come before the path and then the expression? Or does a synopsis not specify order?

When I use find though I have to specify the path before the options like so:

find . -cmin -10 -ls
6

-cmin and -ls are predicates that are part of the expression, not options.

Note that you can mark the end of options with --, but predicates are still allowed after it.

With GNU find, which allows omitting paths:

find -- -L

Would complain about the unknown -L predicate (even though -L is a valid option which actually has a -follow equivalent predicate).

That's why while

find "$file"

like

wc "$file"

doesn't work correctly when $file starts with -.

Doing wc -- "$file" fixes it for wc (except in the special case of file='-') but not for find -- "$file". FreeBSD find has find -f "$file" for that.

3

The find command is very old, dating back to Unix V5. At the time, the command line syntax conventions weren't as firmly established as they are now. A few old programs deviate from these conventions.

Originally, find didn't have options. It expected the first argument to be a directory to traverse, and the remaining arguments to be an expression. Evaluating the expression for each file indicates whether this file should be printed (or more generally indicates what to do with the file). The expression is made from operators and their operands. Find operators begin with - (except for a few punctuation symbols) for the same reason options begin with -: because it doesn't have any special meaning in the shell so it's easy to type, yet it is distinguishable from file names because reasonable people don't start a file name with -.

Operators aren't options. Why? Because they don't have the syntax of options… They have the spelling of options (beginning with -) but they're used in a different context.

Later find acquired a few things that could be considered options — global settings like -depth that don't really have any business being in the expression. These were put in the expression for consistency, because find didn't have options.

Still later, find acquired a few options, with the usual option syntax: options before non-options. This was done to be consistent with many other programs that support the same options (-H and -L to configure symlink behavior).

The synopsis tells you that -H, -L and the like can come first, then the path(s), then the expression. The synopsis syntax can't always express all possibilities. If it was complete, it would be unreadable, so all you know is that it gives one possibility. You need to read further if you want to know exactly what is possible and what isn't. For example, -H, -L, etc. can come in any order, as long as they're before the path.

  • I beleive -L (replacing -follow) and -H (also added to several other utilities) is actually a POSIX invention. – Stéphane Chazelas Aug 3 '17 at 8:36

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.