How to use grep to search the output of –help to find out the CLI parameters starting with Dash

Have you ever seen a command that contains a long list of ambiguous commands, an alphabetic parameter starting with a dash/hyphen, and want a quick way to find out what each command-line parameter does? This article explains how to do this.

use grep search for --help This output fails:

$ rm --help | grep -r

Usage: grep [OPTION]... PATTERNS [FILE]...

Try 'grep --help' for more information.

that is because grep treat -r As its own parameter, not as a search mode.So what is the correct way to use it grep search for --help Output parameters starting with a dash?

Let’s start with a simple example that only contains a command line parameter starting with a dash/hyphen.Say what we want to find out -r The argument refers to rm -r command.search for rm --help use grep as follows:

rm --help | grep -w -- -r

Output the following:

  -r, -R, --recursive   remove directories and their contents recursively
By default, rm does not remove directories.  Use the --recursive (-r or -R)

-- It is very important here. Double dash (--) Indicates the end of the option of the specific command (the end of the command line flag), after which only positional parameters are accepted. this road, grep No attempt will be made to interpret the content after the double dash as an option/flag.

grep -w The parameter is only used to match whole words, so for example only -r, Not -rSOMETHING.

The alternative method you can use instead of the double dash is -e. E.g, grep -w -e -r (or grep -we -r) Results with grep -w -- -r In the example above. -e Allows you to specify the search mode, it can be used to protect with a dash (-).The other way to achieve this is to use \ (E.g \-r), use "-r", and many more.

This also applies to command-line parameters that start with 2 dashes, but I did not use it in the example because these parameters are usually self-explanatory (e.g. --help).

You may also like: Cod: Bash and Zsh command line auto-completion daemon for detecting –help usage

Find out the role of multiple one-letter command line parameters by searching the letters of the program --help

For the second example, we will use grep search for rm --help For what -r with -f Choices made in one breath:

rm --help | grep -w -- '-[rf]'

Has the following output:

  -f, --force           ignore nonexistent files and arguments, never prompt
  -r, -R, --recursive   remove directories and their contents recursively
  By default, rm does not remove directories.  Use the --recursive (-r or -R)

-[rf] Is a regular expression for simultaneous search -r with -f argument.You can add any number of command line parameters in between [], Not just 2.

To make it easier to use, you can add the following simple function I just created to your ~/.bashrc or ~/.zsh:

function argshelp() {   ARGS="${@:2}"  $1 --help | grep -w -- "^  -[$ARGS]"}

Then use (after getting the file, for example source ~/.bashrc), the following content is used to search the help of COMMAND to view the description of ARGUMENTS:

argshelp COMMAND ARGUMENTS

example:

argshelp rm rf

or:

argshelp ls l a

As you can see, when using this function, the command parameter must not start with a dash. You can specify two parameters, separated by a space or used together. In addition, you can rename this function to any name you like (just make sure the commands, aliases, etc. have not used it yet).

You may also like: How to use find to find files modified in the last N days or minutes

Additional features

It’s worth noting that in some cases, even using -w (Only match whole words) grep Parameters, you may still get unwanted matches.For example, you might just want -l Explanation of the argument from ls --help, But ls --help | grep -w -- -l Output:

      --author               with -l, print the author of each file
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               with -l: show ctime and sort by name;
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
  -l                         use a long listing format
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -o                         like -l, but do not list group information
                             with -l, WORD determines which time to show;
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
                               with -l: show access time and sort by name;

Due to most --help The page has parameters starting with two spaces. You can use this information to capture only the lines starting with that particular command line parameter and exclude other matches (e.g. ls command):

  • To search for a single command line parameter (-l):

ls --help | grep -w -- '^  -l'

Which output:

  -l                         use a long listing format
  • Search multiple command line parameters (-l with -a):
ls --help | grep -w -- '^  -[la]'

Has the following output:

  -a, --all                  do not ignore entries starting with .  -l                         use a long listing format

^ Indicates that only lines starting with the following will be matched ^ Symbol; here, ^ Followed by 2 spaces.

With the help of command-line tools, the role of parameters in multiple lines can be explained.In this case, you can know grep To list the extra lines below the matched lines, use -A (From grep help: -A NUM “Print NUM lines of trailing context”).For example, to search cp What does the command help -f Option, and print 2 lines of trailing context, please use:

cp --help | grep -w -A2 -- -f

Has the following output:

  -f, --force                  if an existing destination file cannot be                               opened, remove it and try again (this option                               is ignored when the -n option is also used)

I also have to mention explainshell.com Here, you can write commands on the website to view the help text that matches each parameter.

You might also like: How to repeat a command every X seconds on Linux

by @nixcraft



Source

Related Posts