compgen: An Awesome Command To List All Linux Commands

by on July 19, 2012 · 9 comments· LAST UPDATED July 19, 2012

in Command Line Hacks

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:

 
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
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
  • Curtis

    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):

    #!/bin/bash
    commands=`compgen -c`
    let count=1
    for i in ${commands[@]}
    do
            what=`whatis $i`
            echo -e "\n33[38;5;148m$count. $i: 33[39m"
            echo -e "$what"
            ((count++))
    done
    

    Last edited by Admin: pre tags added.

  • Rajkumar.M

    Excellent!!!…..

  • LinuxRawkstar

    GR8! Hidden gem. Lovin’ it.

  • Rajsekhar G

    Awesome bozz…… Thnx. :)

  • samspade

    LOL !

    whatis man
    man whatis

  • Ashok

    try “man man” :)

  • tintam

    Or if you’re in a gnu bash shell, pressing tab twice will list all available commands

  • Patrick

    Awesome!! never heard before! Thanks for the Tip

  • Ramon

    Thanks for sharing!! This will come in handy for sure.

Previous post:

Next post: