×


Methods to List All Available Commands and Aliases in Linux



In Linux, commands are the key things that are very interesting to run and execute the respective programs. Executing such commands and their aliases lets the user run many important tasks. There are many ways to list them out of which one is to write the shell script. But Linux makes it easy with the keyword of the shell library which is compgen.

Here at LinuxAPT, we will look into different methods to list all the available commands and aliases in Linux.


1. How to use the .bashrc to List All Available Commands and Aliases in Linux ?

One way is to write the shell script by adding it on .bashrc. Let us add a few lines of shell scripts so it can list the command and aliases.


i. To list all the command

Add these lines of shell script to list all the available commands:

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

ii. To list all the aliases

Add these lines of shell script to list all the available aliases:

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
}

To list all the available commands and aliases in $PATH that is using mycommand, run the below command:

$ type -a mycommand


2. How to use the built in shell library function to List All Available Commands and Aliases in Linux ?

There is a built-in keyword of the shell library which is compgen that is very easy to use and execute to list all the commands and the aliases in Linux:

$ Compgen -flag

You are allowed to use any of the listed flags as per your requirement:

  • compgen -c → list all the commands that we can run.
  • compgen -a → list all the aliases that we can run.
  • compgen -b → list all the built-ins that we can run.
  • compgen -k → list all the keywords that we can run.
  • compgen -A function → list all the functions that we can run.
  • compgen -A function -abck → list all the above flags can do at once.

Here, we are going to list all the commands that we can run and aliases. So the -c and -a flag is used in such cases.

To list all the commands that we can run, let's run the command:

$ compgen -c > commands.txt

To list the files on commands.txt, let's print the contents with the cat command:

$ cat commands.txt

Here, we have successfully printed the available commands that we can run and there are many more commands which we could not capture on the screenshot.

To list all the aliases that we can run, let's run the command:

$ compgen -a > aliases.txt

To list the files on aliases.txt, let's print the contents with the cat command:

$ cat aliases.txt

Here, we have successfully printed the available aliases that we can run.


Creating a script with the use of compgen command is also one way to list the commands and aliases that we could run. Check the example below for further details regarding the script:

$ echo "compgen -c" > commands.sh

Here, we have created a list.sh script file with content "compgen -c" in it.

Let's give the execute permission to the script with the command :

$ chmod +x commands.sh

Now, run the script with the command:

$ ./commands.sh

Here, all the available commands are listed with the above script by using the compgen command.


[Need Linux system support ? We can help you. ]


Conclusion

This article covers how to list all the available commands and aliases that we can run on Linux. In fact, The compgen is bash built-in command and it will show all available commands, aliases, and functions for you. This command works under Linux, macOS, *BSD and Unix-like system when Bash shell is installed. 


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com