4

So I can enter systemc and press Tab and get systemctl.

But what if I want to list all commands that end in ctl?

How would I do that?

10

To list all available commands, including aliases, functions, bash builtins and bash keywords, use compgen -c. You may grep the resulting list with any pattern, for example:

compgen -c | grep 'ctl$'
6

If you use apropos ctl | less you will see all of the commands containing the string ctl which include man pages. It will also display a few others containing that string but there is surprisingly few of these unintended results so you may find it quite useful.

1

There's nothing stopping you searching the $PATH for matching files

( IFS=:; for p in $PATH; do ls -d "$p"/*ctl; done ) 2>/dev/null

I've used a subshell ( ... ) because I'm changing the shell variable IFS. An alternative would be to save it at the beginning (OIFS="$IFS" IFS=:) and restore it afterwards (IFS="$OIFS") but to me this feels slightly less robust.

1

I would do this using find :

( IFS=:; for dir in $PATH; do find $dir/ -type f -executable -iregex ".*ctl"; done )

To show cmd's only:

( IFS=:; for dir in $PATH; do find $dir/ -type f -executable -iregex ".*ctl" -printf '%f\n'; done ) 2>/dev/null

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.