4

I did some research, but unfortunately my question was never answered. The closest question to mine is this: Search for bash command

What I need: A command to search for installed commands using wildcards. Let's say I want to unmount something but forgot the command. I know it contains "mount". So I would do something like this:

searchfor *mount*

To find that unmount is the command I need. I know man -k or yum whatprovides. But that's not what I'm looking for. I want to search for all installed commands (that can be found in the directories provided by the $PATH variable).

  • A bit more clarity is needed. Do you want to search for commands installed via .rpm packages or shell builtins? If so, apropos will do that and you don't need wildcards. – Nasir Riley Apr 1 '18 at 19:29
  • Commands installed by yum (CentOS) or apt-get (ubuntu). Okay, apropos is very close to my needs but not 100% what I asked for. Thanks anyway! – Daniel Hoop Apr 1 '18 at 19:42
10

My favorite way is to use compgen -c. For example, to find all commands that contain mount:

$ compgen -c | grep mount
gvfs-mount
mmount
ideviceimagemounter
grub-mount
humount
hmount
umount
mountpoint
mount
fusermount
umount
mount.ntfs
mount.lowntfs-3g
mount.cifs
umount.udisks
mount.nfs
umount.nfs
mount
mount.ntfs-3g
mount.fuse
showmount
rpc.mountd
mountesp
umount.udisks2
mountstats
automount

What's good about compgen -c is that it also finds aliases, user functions and Bash built-in commands, for example:

$ alias aoeuidhtn='echo hi'
$ compgen -c | grep aoeuidhtn
aoeuidhtn
$ my-great-function() { printf "Inside great function()\n"; }
$ compgen -c | grep great
my-great-function
$ compgen -c | grep '^cd$'
cd

Also, compgen is a part of Bash, so it's always available. As described by help compgen:

Display possible completions depending on the options.

Intended to be used from within a shell function generating possible completions.  If the optional WORD argument is supplied, matches against WORD are generated.

Exit Status:
Returns success unless an invalid option is supplied or an error occurs.

  • 1
    Great, thanks! That's exactely what I was looking for. P.S. Thanks also for all the other answers. You guys are amazing! :-) – Daniel Hoop Apr 1 '18 at 20:04
  • 1
    grep is a great tool, and it certainly can be useful in conjunction with compgen -c — for example, in the case of compgen -c | grep mount — but read the help text. If you know the beginning of the command name, you can say compgen -c aoeuidhtn or compgen -c aoeui. – Scott Apr 2 '18 at 5:52
  • When using the Z shell, you can use analogously hash | grep command, where hash prints the command hash table, which holds all executables inside $PATH. – mpy Apr 2 '18 at 10:59
2
#! /bin/bash
s=$1  # e.g. mount
IFS=:
for p in $PATH ; do
    ls "$p/"*"$s"* 2>/dev/null
done

Setting $IFS to : makes the for correctly iterate over the members of $PATH. Redirecting stderr to /dev/null hides error messages for directories that contain no matches.

1

It can be done in many different ways, look up in $PATH paths, use locate command etc..

 find $(echo $PATH|tr ':' ' ') -maxdepth 1 -name '*mount*' 
  • 3
    with -maxdepth 1 – Hauke Laging Apr 1 '18 at 20:18
  • And -mindepth 1 – wjandrea Apr 2 '18 at 4:49
  • If any of the dirs in your PATH are symlinks, you need to use find -H – wjandrea Apr 2 '18 at 5:13
1

For the record, in zsh:

type -m '*mount*'

Will find all matches and for each, report what (executable, function, alias...) and where they are.

$ type -m 'zc*'
zc is an alias for zcalc
zcalc is an autoload shell function
zcompile is a shell builtin
zcat is /bin/zcat
zcmp is /bin/zcmp
0

This is based choroba's answer, but only uses Bash features (written for GNU Bash 4.3.11). It returns success if any matches are found, failure otherwise.

#!/bin/bash
shopt -s nullglob
s="${1:?}"  # Error if argument is missing or null.
exit=1  # Return failure unless changed.
IFS=:
for p in $PATH; do
    for f in "$p/"*"$s"*; do
        if [[ -f $f ]]; then
            echo "$f"
            exit=0  # Return success since match found.
        fi
    done
done
exit $exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.