Ever want to list all the Linux commands (including bash shell aliases and functions) you could run on the server / workstation? Look now further. Try compgen command.
compgen is bash built-in command and it will show all available commands, aliases, and functions for you. The syntax is:
ADVERTISEMENTS
compgen option
compgen command examples
To list all the commands available to you, enter:
compgen -c
Sample outputs:
ls if then else elif fi .... mahjongg sol gtali sl-h gnobots2 gnotravex iagno fortune gnect gnome-sudoku LS glchess gnuchess gnuchessx
You can search or count the commands:
compgen -c | grep find compgen -c | wc -l echo "$USER user can run $(compgen -c | wc -l) commands on $HOSTNAME."
Sample outputs:
vivek user can run 3436 commands on wks01.
To list all the bash shell aliases available to you, enter:
compgen -a
Sample outputs:
.. ... .... ..... .4 .5 bc cd.. chgrp chmod chown cp dnstop egrep ethtool fastping fgrep grep iftop l. ll ln ls mcdflush mcdshow mcdstats mount mv pscpu pscpu10 psmem psmem10 rm tcpdump update updatey vnstat wget which
See 30 awesome handy bash aliases for more information. Other options are as follows:
######################################## # Task: show all the bash built-ins ######################################## compgen -b ######################################## # Task: show all the bash keywords ######################################## compgen -k ######################################## # Task: show all the bash functions ######################################## compgen -A function
Putting it all together:
compgen -abckA function ## It doesnt get much better than this compgen -abckA function | less compgen -abckA function | grep -i --color searchStringHere
🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 10 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Here’s a pointless script! If you have `whatis` enabled, you can run this script to tell you what each command is (when that info is available, of course):
Last edited by Admin: pre tags added.
Curtis, FYI when you used commands=`compgen -c`, it loaded commands with a string (albeit a very long one). So in this instance, you could have used:
The ${commands[@]} format is generally reserved for use with arrays. It worked in this instance but is not a good habit. If the output contained spaces the aforementioned would fail. You’d probably want to do this:
Load output of compgen into an array named commands (the -t options strips trailing newline)
Note the quotes to prevent word splitting. The for loop will then run through ${commands[0]}, ${commands[1]}, ${commands[2]}, etc…
In my endless quest for Bash excellence, here is your script cleaned up a bit. It will ignore commands without any whatis output. For commands with multiple whatis entries, it will only display the first. Using $((++count)) allows for increment and output at the same time.
Excellent!!!…..
GR8! Hidden gem. Lovin’ it.
Awesome bozz…… Thnx. :)
LOL !
whatis man
man whatis
try “man man” :)
Or if you’re in a gnu bash shell, pressing tab twice will list all available commands
Awesome!! never heard before! Thanks for the Tip
Thanks for sharing!! This will come in handy for sure.