2

I am trying to understand and read the man page. Yet everyday I find more inconsistent syntax and I would like some clarification to whether I am misunderstanding something.

Within the man page, it specifies the syntax for grep is grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]

I got a working example that recursively searches all files within a directory for a keyword.

grep -rnw . -e 'memes

Now this example works, but I find it very inconsistent with the man page. The directory (Which the man page has written as [FILE...] but specifies the use case for if file == directory in the man page) is located last. Yet in this example it is located after [OPTIONS] and before [-e PATTERN].... Why is this allowed, it does not follow the specified regex fule of using this command?

J.Doe
  • 21
  • 4

2 Answers2

1

Why is this allowed, it does not follow the specified regex fule of using this command?

The lines in the SYNOPSIS section of a manpage are not to be understood as strict regular expressions, but as a brief description of the syntax of a utility's arguments.

Depending on the particular application, the parser might be more or less flexible on how it accepts its options. After all, each program can implement whatever grammar they like for their arguments. Therefore, some might allow options at the beginning, at the end, or even in-between files (typically with ways to handle ambiguity that may arisa, e.g. reading from the standard input with -, filenames starting with -...).

Now, of course, there are some ways to do it that are common. For instance, POSIX.1-2017 12.1 Utility Argument Syntax says:

This section describes the argument syntax of the standard utilities and introduces terminology used throughout POSIX.1-2017 for describing the arguments processed by the utilities.

In your particular case, your implementation of grep (probably GNU's grep) allows to pass options in-between the file list, as you have discovered.

For more information, see:

Acorn
  • 24,970
  • 5
  • 40
  • 69
0

You can also leverage . grep ‘string’ * -lR

Ankur Srivastava
  • 855
  • 9
  • 10