List All Available Commands and Aliases in Linux - GeeksforGeeks (2024)

Last Updated : 15 Sep, 2023

Summarize

Comments

Improve

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
List All Available Commands and Aliases in Linux - GeeksforGeeks (1)

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:

List All Available Commands and Aliases in Linux - GeeksforGeeks (2)

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:

List All Available Commands and Aliases in Linux - GeeksforGeeks (3)

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
List All Available Commands and Aliases in Linux - GeeksforGeeks (4)

We can use the -p command to print all the defined aliases like

alias -p
List All Available Commands and Aliases in Linux - GeeksforGeeks (5)

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

List All Available Commands and Aliases in Linux - GeeksforGeeks (6)

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/bashecho $PATH | tr : '\n' |while read e; do for i in $e/*; do if [[ -x "$i" && -f "$i" ]]; then echo $i fi donedone

The following output lists five of the commands listed using the bash script.

List All Available Commands and Aliases in Linux - GeeksforGeeks (7)

Now let us try to understand the above script. Using the $PATH environment variable, wehavefirst obtainedall 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.



Please Login to comment...

List All Available Commands and Aliases in Linux - GeeksforGeeks (2024)
Top Articles
Investment Property: How Much Can You Write Off on Your Taxes?
Linkedin Post Character Limit And See More Preview Tool - Poked
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5890

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.