Jul 232012
 

Inspired from a post by Concepcion Claudio Certad, today I’ve taken a look at a command that I did not known: compgen.

The compgen builtin command expands a list of arguments to generate completion matches, so it allows us to display the commands, functions, directories and aliases available for the current user. In short compgen is a command that displays commands.



So compgen is a command builtin in the bash shell, so if you use this shell you have this command available in the terminal,
Let’s see how we can use it directly from the command line, as first thing let’s say that in general compgen has different options that you can use to specify which kind of thing you are looking for, so the basic usage is:

compgen [option] [word]

This generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output let’s see some examples:

Working with directories

The option for directories is -d so compgen -d will give as output the list of all the directory in the current path, or you can ask for a specific path such as:

xubuntu-home:~$ compgen -d /etc/s
/etc/sudoers.d
/etc/sysctl.d
/etc/sensors.d
/etc/skel
/etc/samba
/etc/sane.d
/etc/ssh
/etc/speech-dispatcher
/etc/systemd
/etc/sgml
/etc/ssl
/etc/sound
/etc/smartmontools
/etc/snmp
/etc/security

In this case I’ve asked compgen to show all the directory in /etc/ that start with the letter s, using just /etc/ as parameter it will show all the directories in /etc/.

Working with commands

In the same way compgen can also show all the available commands for an user with the flag -c : compgen -c if you run this command probably you’ll get a list of some thousands of commands so it’s better to use it with a | less or to show only some commands use an additional parameter that represents a part of the word you are looking for:

xubuntu-home:~$ compgen -c ss
sshd
ssgrep
ssh-vulnkey
ssindex
ssconvert
ssh-import-id
ssh
ssh-copy-id
ssh-argv0
ssh-keygen
ssh-askpass
ssh-add
ssh-keyscan
ssh-agent
ss

In this example I can see all the commands that i can use that start with ss

Other Functions

This is a short list of options that you can use with compgen:

Show all the bash built-ins commands.

compgen -b

Show all the files and directories in current path (or specify a path like in the directory)

compgen -f

Show all the bash keywords

xubuntu-home:~# compgen -k
if
then
else
elif
fi
case
esac
for
select
while
until
do
done
in
function
time
{
}
!
[[
]]
coproc

Show all the bash functions
compgen -A function

Show all the alias available for the current user

xubuntu-home:~$ compgen -a
vps
egrep
fgrep
grep
ls

Show all the usernames defined in the server:

xubuntu-home:~# compgen -u s
sys
sync
syslog
speech-dispatcher
saned
sshd
sslh

Show all the groups defined in the server:

xubuntu-home:~# compgen -g p
proxy
plugdev
pulse
pulse-access
postfix
postdrop


Popular Posts:

Flattr this!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*