Sometimes when working in Linux, we’d like to know all the commands and aliases supported by the system.
An alias, as we know, is a custom shortcut used to refer to the actual command. It can help us save time by customizing long commands into short strings.
In this tutorial, we’ll see three approaches for listing all the available commands and aliases in Linux – using the compgen command, using the alias command, and by writing a Bash script.
2. Using the compgen Command
Using the compgen command, we can list commands, aliases, built-ins, keywords, and functions using different options.
2.1. List Commands
We use the compgen -c command to list all available commands:
$ compgen -c alert egrep fgrep grep l la ll ls ...
Here, the -c option tells compgen to list all the commands that we can execute on our system.
2.2. List Aliases
We use the compgen -a command to list all the available aliases:
$ compgen -a alert egrep fgrep grep l la ll ls ...
Here, the -a option tells compgen to list all the aliases.
3. Using the alias Command
Using the alias command, we can list the defined aliases:
$ alias -p | cut -d= -f1 | cut -d' ' -f2 alert egrep fgrep grep l la ll ls ...
Here, the -p option tells the alias command to print all the defined aliases. Then, we pipe the output to the first cut command. The cut command uses the = sign as the delimiter to divide a line into fields and the -f1 means we take the first field.
We pipe the output of the first cut command to the second cut command. It uses space as a delimiter and we select the second field and display it on the terminal.
4. Using Bash Script
We can write a Bash script to list all the available commands on our system:
#!/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
Let’s break down the above script. First, we get all the directory paths that contain executables using the $PATH environment variable. Then, we pipe the output to the tr command. The tr command translates the : from the input to a newline and pipes the output to the while loop.
The while loop uses the read command to read each line and stores the content of each step in $e. Using the for loop, we iterate over each directory and check if each file is an executable using the -x option. The -f option checks if a file exists and if it’s a regular file.
Once the filename passes both tests, its path is displayed on the terminal using the echo command.
Now, let’s run the script and check its output:
$ bash commands.sh /usr/sbin/aa-remove-unknown /usr/sbin/aa-status /usr/sbin/aa-teardown /usr/sbin/accessdb /usr/sbin/add-shell /usr/sbin/addgnupghome /usr/sbin/addgroup /usr/sbin/adduser /usr/sbin/agetty ...
Here, we can see the absolute paths of all the commands.
In this article, we saw how to use the compgen command to list all the available commands and aliases in Linux. We also learned how to list all the available aliases using the alias command and the available commands using a Bash script.