Search for command with wildcard
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I did some searches, 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).
bash
add a comment |Â
up vote
4
down vote
favorite
I did some searches, 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).
bash
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 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 at 19:42
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I did some searches, 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).
bash
I did some searches, 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).
bash
edited Apr 2 at 9:17
asked Apr 1 at 19:17
Daniel Hoop
233
233
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 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 at 19:42
add a comment |Â
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 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 at 19:42
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 at 19:29
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 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 at 19:42
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 at 19:42
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
10
down vote
accepted
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 at 20:04
1
grep
is a great tool, and it certainly can be useful in conjunction withcompgenâ¯-c
â for example, in the case ofcompgenâ¯-câ¯|â¯grepâ¯mount
â but read the help text.âÂÂIf you know the beginning of the command name, you can saycompgenâ¯-câ¯aoeuidhtn
orcompgenâ¯-câ¯aoeui
.
â Scott
Apr 2 at 5:52
When using the Z shell, you can use analogouslyhash | grep command
, wherehash
prints the command hash table, which holds all executables inside$PATH
.
â mpy
Apr 2 at 10:59
add a comment |Â
up vote
2
down vote
#! /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.
add a comment |Â
up vote
1
down vote
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 at 20:18
And-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to usefind -H
â wjandrea
Apr 2 at 5:13
add a comment |Â
up vote
0
down vote
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
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
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 at 20:04
1
grep
is a great tool, and it certainly can be useful in conjunction withcompgenâ¯-c
â for example, in the case ofcompgenâ¯-câ¯|â¯grepâ¯mount
â but read the help text.âÂÂIf you know the beginning of the command name, you can saycompgenâ¯-câ¯aoeuidhtn
orcompgenâ¯-câ¯aoeui
.
â Scott
Apr 2 at 5:52
When using the Z shell, you can use analogouslyhash | grep command
, wherehash
prints the command hash table, which holds all executables inside$PATH
.
â mpy
Apr 2 at 10:59
add a comment |Â
up vote
10
down vote
accepted
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 at 20:04
1
grep
is a great tool, and it certainly can be useful in conjunction withcompgenâ¯-c
â for example, in the case ofcompgenâ¯-câ¯|â¯grepâ¯mount
â but read the help text.âÂÂIf you know the beginning of the command name, you can saycompgenâ¯-câ¯aoeuidhtn
orcompgenâ¯-câ¯aoeui
.
â Scott
Apr 2 at 5:52
When using the Z shell, you can use analogouslyhash | grep command
, wherehash
prints the command hash table, which holds all executables inside$PATH
.
â mpy
Apr 2 at 10:59
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
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.
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.
edited Apr 2 at 5:42
Scott
6,24332347
6,24332347
answered Apr 1 at 19:54
Arkadiusz Drabczyk
7,18521532
7,18521532
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 at 20:04
1
grep
is a great tool, and it certainly can be useful in conjunction withcompgenâ¯-c
â for example, in the case ofcompgenâ¯-câ¯|â¯grepâ¯mount
â but read the help text.âÂÂIf you know the beginning of the command name, you can saycompgenâ¯-câ¯aoeuidhtn
orcompgenâ¯-câ¯aoeui
.
â Scott
Apr 2 at 5:52
When using the Z shell, you can use analogouslyhash | grep command
, wherehash
prints the command hash table, which holds all executables inside$PATH
.
â mpy
Apr 2 at 10:59
add a comment |Â
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 at 20:04
1
grep
is a great tool, and it certainly can be useful in conjunction withcompgenâ¯-c
â for example, in the case ofcompgenâ¯-câ¯|â¯grepâ¯mount
â but read the help text.âÂÂIf you know the beginning of the command name, you can saycompgenâ¯-câ¯aoeuidhtn
orcompgenâ¯-câ¯aoeui
.
â Scott
Apr 2 at 5:52
When using the Z shell, you can use analogouslyhash | grep command
, wherehash
prints the command hash table, which holds all executables inside$PATH
.
â mpy
Apr 2 at 10:59
1
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 at 20:04
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 at 20:04
1
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 at 5:52
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 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 at 10:59
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 at 10:59
add a comment |Â
up vote
2
down vote
#! /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.
add a comment |Â
up vote
2
down vote
#! /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.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
#! /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.
#! /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.
answered Apr 1 at 19:25
choroba
24.3k33967
24.3k33967
add a comment |Â
add a comment |Â
up vote
1
down vote
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 at 20:18
And-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to usefind -H
â wjandrea
Apr 2 at 5:13
add a comment |Â
up vote
1
down vote
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 at 20:18
And-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to usefind -H
â wjandrea
Apr 2 at 5:13
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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*'
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*'
edited Apr 5 at 10:20
answered Apr 1 at 19:41
Bharat
3058
3058
3
with-maxdepth 1
â Hauke Laging
Apr 1 at 20:18
And-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to usefind -H
â wjandrea
Apr 2 at 5:13
add a comment |Â
3
with-maxdepth 1
â Hauke Laging
Apr 1 at 20:18
And-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to usefind -H
â wjandrea
Apr 2 at 5:13
3
3
with
-maxdepth 1
â Hauke Laging
Apr 1 at 20:18
with
-maxdepth 1
â Hauke Laging
Apr 1 at 20:18
And
-mindepth 1
â wjandrea
Apr 2 at 4:49
And
-mindepth 1
â wjandrea
Apr 2 at 4:49
If any of the dirs in your PATH are symlinks, you need to use
find -H
â wjandrea
Apr 2 at 5:13
If any of the dirs in your PATH are symlinks, you need to use
find -H
â wjandrea
Apr 2 at 5:13
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
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
answered Apr 2 at 4:48
wjandrea
446312
446312
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434884%2fsearch-for-command-with-wildcard%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 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 at 19:42