# How to list down all the available commands and aliases on Linux?

LinuxOperating SystemOpen Source

#### RedHat Linux: Essentials

Best Seller

23 Lectures 5 hours

#### Linux Command Line Essentials - Become a Linux Power User!

16 Lectures 1 hours

#### Linux Mastery - Complete Linux Course

Best Seller

122 Lectures 4.5 hours

Linux provides us with a huge amount of commands along with their aliases that we can make use of. Although these commands serve different purposes we can still make use of all these commands anywhere we like in the terminal.

There are different ways with which we can interact with Linux commands. When it comes to listing all the available commands to the terminal we also have different approaches, either we can write a shell script by ourselves or we can use a shell library function that does that for us.

Let’s consider the first approaches where I'll make use of a shell library keyword named compgen, which is a bash builtin command that can be used to list all the available commands.

## Syntax

compgen -flag

In the above syntax, the flag is a placeholder that can be replaced according to our needs.

The flag can have all these different values −

• -c − used to list all the commands you could run.

• -a − used to list all the aliases you could run.

• -k − used to list all the keywords you could run.

• -b − used to list all the built-ins you could run.

• -A function − used to list all the functions you could run

• -A function -abck − used to list everything shown above in one go.

Since, we only need to list the commands and their aliases we will make use of the -c and -a flags only.

In order to use that just create a shell file with the command shown below −

touch sample.sh

Now insert the code shown below −

compgen -c

And then give permission to the script before running it −

chmod 777 sample.sh
./sample.sh

## Output

immukul@192 Downloads % ./sample.sh
if
then
else
elif
fi
case
esac
for
select
while
until
do
done
in
.
.
.

In order to print the aliases, we just need to replace the code inside the sample.sh file to the code shown below −

compgen -a | grep ls

## Output

ls is /bin/ls
Updated on 30-Jul-2021 09:01:18