I have a directory with executables and I want to search for a word in the output of the help messages of these executables, i.e. the text that is printed in the console after using the command ./executable1 --help. I want to output both the executable name and the occurrence of the searched text just like grep would do for a text file. How do I do this?

I managed something close with:

find -name "exec*" -executable -exec {} --help \; | grep "stringToBeSearchedFor" --

where find should have some criteria to find all the executables (in this example they all start with "exec" so this is appropriate).

However, this doesn't print the name of the executable with the match.

1 Answer 1


I would probably make use of grep's -H and --label options:

          Display  input  actually  coming  from  standard  input as input
          coming from file LABEL.  This can be useful  for  commands  that
          transform  a  file's  contents  before searching, e.g., gzip -cd
          foo.gz | grep --label=foo -H 'some pattern'.  See  also  the  -H


find . -type f -name 'exec*' -executable -exec sh -c '
  for f; do "$f" --help | grep -H --label="$f" -- "stringToBeSearchedFor"; done
' sh {} +

If the help messages may be printed to the standard error stream rather than the standard output stream, change "$f" --help | grep ... to "$f" --help 2>&1 | grep ...

  • This assumes the help messages will be output to stdout rather than stderr. Dec 19, 2022 at 16:29
  • @MartinThornton indeed it does - I assumed that was appropriate here based on the OP's statement that their version "comes close" but "doesn't print the name" Dec 19, 2022 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .