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
Starknet Documentation
Vykas Gate 1 Guide for Lost Ark - Mobalytics
55Th And Kedzie Elite Staffing
Login Page
Affidea ExpressCare - Affidea Ireland
Myhr North Memorial
Fusion
Arrests reported by Yuba County Sheriff
Vanadium Conan Exiles
J Prince Steps Over Takeoff
Tv Schedule Today No Cable
Hover Racer Drive Watchdocumentaries
Xm Tennis Channel
[PDF] INFORMATION BROCHURE - Free Download PDF
Otr Cross Reference
FAQ: Pressure-Treated Wood
Void Touched Curio
سریال رویای شیرین جوانی قسمت 338
Rhinotimes
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Pirates Of The Caribbean 1 123Movies
Booknet.com Contract Marriage 2
Churchill Downs Racing Entries
O'reilly's In Mathis Texas
Ascensionpress Com Login
Infinite Campus Asd20
Craigslist Auburn Al
Shoe Station Store Locator
Uky Linkblue Login
Ancestors The Humankind Odyssey Wikia
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Edward Walk In Clinic Plainfield Il
Devotion Showtimes Near Mjr Universal Grand Cinema 16
The Blackening Showtimes Near Regal Edwards Santa Maria & Rpx
Bianca Belair: Age, Husband, Height & More To Know
15 Best Things to Do in Roseville (CA) - The Crazy Tourist
Qlima© Petroleumofen Elektronischer Laserofen SRE 9046 TC mit 4,7 KW CO2 Wächter • EUR 425,95
Colorado Parks And Wildlife Reissue List
Restored Republic June 6 2023
Conan Exiles Armor Flexibility Kit
Umd Men's Basketball Duluth
Smite Builds Season 9
Fool's Paradise Showtimes Near Roxy Stadium 14
Mitchell Kronish Obituary
Thotsbook Com
BCLJ July 19 2019 HTML Shawn Day Andrea Day Butler Pa Divorce
Playboi Carti Heardle
Greatpeople.me Login Schedule
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.