CentOS Debian Mint openSUSE Red Hat Ubuntu

Ways to List All Available Commands and Aliases in Linux

Ways 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.

If you are curious about how to check all the commands and their aliases in Linux then let us explain the ways to list them. 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.

In this article, we are going to explain the ways to list all the available commands and aliases in Linux.

Using the .bashrc

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.

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

}

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 command as shown below.

$ type -a mycommand

Using built in shell library function

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. Let us show you how to do it.

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 as shown below.

$ compgen -c > commands.txt

To list the files on commands.txt, let’s print the contents with the cat command as shown below.

$ 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 as shown below.

$ compgen -a > aliases.txt

To list the files on aliases.txt, let’s print the contents with the cat command as shown below.

$ 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 as shown below.

$ chmod +x commands.sh

Now, run the script with the command as shown below.

$ ./commands.sh

Output:

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

Conclusion

In this article, you have learnt how to list all the available commands and aliases that we can run on Linux. Now, you are able to take the reference of the commands as per your requirements to complete the certain tasks. Thank you!

Similar Posts