Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command to list all available commands and aliases

Is there a Linux command that will list all available commands and aliases for this terminal session?

As if you typed 'a' and pressed tab, but for every letter of the alphabet. Or running 'alias' but also returning commands.

Why? I'd like to run the following and see if a command is available:

ListAllCommands | grep searchstr 
like image 640
ack Avatar asked Jun 04 '09 00:06

ack


People also ask

How do I see all aliases in Linux?

To see a list of aliases set up on your linux box, just type alias at the prompt. You can see there are a few already set up on a default Redhat 9 installation. To remove an alias, use the unalias command.

What is the command to list aliases?

Linux Alias Syntax[option] : Allows the command to list all current aliases. [name] : Defines the new shortcut that references a command. A name is a user-defined string, excluding special characters and 'alias' and 'unalias', which cannot be used as names.


2 Answers

You can use the bash(1) built-in compgen

  • compgen -c will list all the commands you could run.
  • compgen -a will list all the aliases you could run.
  • compgen -b will list all the built-ins you could run.
  • compgen -k will list all the keywords you could run.
  • compgen -A function will list all the functions you could run.
  • compgen -A function -abck will list all the above in one go.

Check the man page for other completions you can generate.

To directly answer your question:

compgen -ac | grep searchstr 

should do what you want.

like image 120
camh Avatar answered Oct 06 '22 00:10

camh


Add to .bashrc

function ListAllCommands {     echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \         -executable -type f -printf '%P\n' | sort -u } 

If you also want aliases, then:

function ListAllCommands {     COMMANDS=`echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \         -executable -type f -printf '%P\n'`     ALIASES=`alias | cut -d '=' -f 1`     echo "$COMMANDS"$'\n'"$ALIASES" | sort -u } 
like image 33
Ants Aasma Avatar answered Oct 05 '22 23:10

Ants Aasma