Search man Pages – Find Entries Containing ‘foo’, ‘bar’, and ‘baz’

mansearch

I'd like to search for man pages which contain ALL of the words 'foo', 'bar' and 'baz'.

If possible, I'd like to search through all text (not just name & description) of all man pages.

I'm guessing something like

man -K foo AND bar AND baz

Best Answer

I implemented a script that does exactly this.

if [ $# -eq 0 ]; then
  PATTERNS=(NAME AUTHOR EXAMPLES FILES)
else
  PATTERNS=( "$@" )
fi

[ ${#PATTERNS[@]} -lt 1 ] && echo "Needs at least 1 pattern to search for" && exit 1

for i in $(find /usr/share/man/ -type f); do
  TMPOUT=$(zgrep -l "${PATTERNS[0]}" "$i")
  [ -z "$TMPOUT" ] && continue

  for c in `seq 1 $((${#PATTERNS[@]}-1))`; do
    TMPOUT=$(echo "$TMPOUT" | xargs zgrep -l "${PATTERNS[$c]}")
    [ -z "$TMPOUT" ] && break
  done

  if [ ! -z "$TMPOUT" ]; then
    #echo "$TMPOUT" # Prints the whole path
    MANNAME="$(basename "$TMPOUT")"
    man "${MANNAME%%.*}"
  fi
done

Guess it was a waste of time :(

Edit: Seems like

man -K expr1 expr2 expr3

didn't work?

Edit: You can pass the scripts now your search terms via ./script foo bar

Related Question