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
  • 28,789
  • 5
  • 20
  • 47
Zeth
  • 155
  • 6
  • 2
    practice, practice, practice. and more practice. – cas Apr 29 '22 at 11:39
  • BTW, `find` is a bit of an odd case, its options and option ordering are like no other program. It's complicated and difficult to get used to but that's because the job it does is very complex with many variables. The args for most other programs generally follow the pattern of: options & option args, then source/input files/dirs, then (if applicable) a target/destination/output filename or dir. – cas Apr 29 '22 at 11:46
  • 1
    As for quoting, you quote things when you don't want them to be subjected to shell's word splitting and expansion, or when you want a string containing whitespace to be treated as a single item, or when you want to suppress shell's special handling of characters that have special meaning to the shell - like `;`, `&`, `(`, `*`, `[`, and so on. You use single-quotes when you don't want the shell to expand variable names etc, and you use double-quotes when you do want that. See – cas Apr 29 '22 at 11:51
  • See [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters), [$VAR vs ${VAR} and to quote or not to quote](https://unix.stackexchange.com/questions/4899/var-vs-var-and-to-quote-or-not-to-quote), [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells), and related Q&A – cas Apr 29 '22 at 11:52

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
  • 31,679
  • 3
  • 40
  • 73
  • 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