List All Available Commands and Aliases in Linux
Last Updated :
15 Sep, 2023
There might be sometimes a need to list all the commands and aliases supported by the current system. As we already know, an alias is a custom shortcut that references a command. It can be very useful to replace long commands with a small alternative command. While a command is a program that we can run in the terminal to produce some output or change the status of the system. In this article, we are going to talk about how we can list all the available commands and aliases in the Linux system.
Three methods for listing all the available commands and aliases in Linux:
- Method 1: Using compgen command
- Method 2: Using the alias command
- Method 3: Using bash script
Method 1: Using compgen command
We can list all the available commands and aliases supported by the system using the compgen command. Following is the excerpt from the help page about the compgen command
help compgen
1. List commands
To list all the commands supported by the system we can use the -c option
compgen -c
The following screenshot shows five of the commands supported:
2. List aliases
To list all the aliases, we can use the -a option.
compgen -a
The following screenshot lists all the aliases in my system:
Method 2: Using the alias command
We can use the alias command to list all the aliases in the system. Following is the excerpt from the help page about the alias command
help alias
We can use the -p command to print all the defined aliases like
alias -p
To get a better and clear output we can pipe this output from the alias command to the cut command like
alias -p | cut -d' ' -f2

Method 3: Using bash script
We can also write a bash script to list all the commands. The following will suffice to do the job:
#!/bin/bash
echo $PATH | tr : '\n' |
while read e; do
for i in $e/*; do
if [[ -x "$i" && -f "$i" ]]; then
echo $i
fi
done
done
The following output lists five of the commands listed using the bash script.

Now let us try to understand the above script. Using the $PATH environment variable, we have first obtained all of the directory paths that lead to executable files. The output is then piped to the tr command. Then, the while loop receives the output of the tr command, which converts the: from the input to a new line. The while loop puts the contents of each step in $e after reading each line using the read command. We traverse through each directory using the for loop, and we use the -x option to determine whether each file is executable. The -f option verifies a file's existence and its regularity. Once the filename passes both checks, the echo command is used to show the path on the console.
Conclusion:
In this article, we learned how to list every command and alias available in Linux by using the compgen function. We also learned how to use a Bash script to list all the available commands and aliases using the alias command.
Similar Reads
How to Create and Use Alias Command in Linux Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making thin
6 min read
How to Create and Use Alias Command in Linux Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making thin
6 min read
Basic CentOS Linux Commands in linux CentOS is a free and open-source operating system that aims to provide a stable reliable, and community-supported platform for servers and other enterprise applications. In this article, we will be covering CentOS Linux basics commands and functions of CentOS and also we will look into the advanced
4 min read
Chaining Commands in Linux Chaining commands in Linux is a powerful technique that allows you to execute multiple commands sequentially or simultaneously through the terminal. This approach resembles writing short shell scripts that can be directly executed in the terminal, enhancing the efficiency of your workflow. By using
7 min read
bind command in Linux with Examples bind command is a Bash shell builtin command. It is used to set Readline key bindings and variables. The keybindings are the keyboard actions that are bound to a function. So it can be used to change how the bash will react to keys or combinations of keys, being pressed on the keyboard. Here, weâll
4 min read
Basic Shell Commands in Linux: Complete List Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively.The Linux shell serves as an interface for us
5 min read