1

Every time I use grep and find I'm stumbling through, which order I should type the commands: When I should write the path and when I should write the flags, if it needs double quotations or not, etc. etc.

I've Googled it many more times than I'm proud of.

For grep

# Searching all files in the directory (recursively)
grep -rni "string" *

Disecting it

command flags parameter path

grep -rni "string" *

For find

# Searching for a file by name
find . -type f -name "file-name*"

Disecting it

command path flag parameter flag parameter

find . -type f -name "file-name*"

Are there a good way of remembering, when the path should come and when the flags should come etc. (without simply Googling it every time)?


Solution attempt 1: man pages

I love the man-pages, but maybe I'm using them incorrectly. They are detailed (which is good), but if I want to get a quick example with, how to use the given command, then I struggle with the man-pages.

If I write man find, I can see this close to the top:

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

Solution attempt 2: Save snippet-examples that I use a lot

I can also simply save snippets that I use a lot, so I quickly can load a working example.

But what do other people do here? Is this intuitive for everyone else?

Ed Morton
  • 31,617
Zeth
  • 155

1 Answers1

2

grep and find I'm stumbling through, which order I should type the commands: When I should write the path and when I should write the flags, if it needs double quotations or not, etc. etc.

The general rule for commands is: First the options, then "fixed" parameters that occur only once, then "variable" parameters that can occur many times, like file names.

grep has one pattern and can search many files. So it's "options, pattern, files". No surprises here.

find is a bit odd. The only "real" argument is the path that describes where to start the recursive search. It is followed by a sort of mini-language that describes what things to match via different options, which can be combined by juxtaposition (or more options), followed by actions described by other options. So it's "directory, match options, action options".

Remember this, and you shouldn't need to look it up. At least that's how I remember them (and I still occasionally do man find to read up on some matching options I forgot).

dirkt
  • 32,309
  • 1
    Note that grep could take many patterns when using -e '...' -e '...'. Also, find does have options, at least -H and -L. The thing with find is that its "expression" is more complex, which you allude to. It's a set of tests, basically, made up by "predicates", some of which take arguments. – Kusalananda Apr 29 '22 at 13:25
  • The conditions that find takes (like -name), aren't options. They're more like part of or another group of those arguments, different from the pathnames it takes (you can give it more than one). Just that they start with a dash like options. Something like parallel can also take multiple sets of arguments, separated by ::, or ::: or so. But it's a bit special too. – ilkkachu Apr 29 '22 at 14:12
  • also in addition to grep possibly taking multiple patterns with -e options, they would then be part of the options, not so the format would then be "options, files". How the first non-option argument is treated depends on if -e or -f is given. – ilkkachu Apr 29 '22 at 14:16