Compgen Command on Linux with Examples

Linux TLDR
Last Updated:
Reading time: 2 minutes

The compgen command is a very handy tool available in major Linux distributions that can help you find your system aliases, shell built-ins, commands, directories, groups, jobs, users, etc.

In this article, you will learn how to use the compgen command with practical examples.

Tutorial Details

DescriptionCompgen
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescompgen
Internet RequiredNo

Listing All Aliases

Execute the following command to get the list of all aliases that refer to an existing command.

$ compgen -a

Output:

Listing all aliases

Listing the Names of Shell Built-In

The following command will list the shell’s built-in commands.

$ compgen -b

Output:

Listing all shell built-in names

Listing the Names of All Commands

The following command will display a list of all commands that are executable from the command line or can be used in shell scripts.

$ compgen -c

Output:

Listing all the commands

Listing the Names of Directories

It will output all the visible and hidden directories (except files) in your current working directory.

$ compgen -d

Output:

Listing all the directories in the current working directory

Listing the Names of Exported Shell Variables

The following command will display the list of all shell environment variables for the logged-in user.

$ compgen -e

Output:

Listing all the shell variables

Listing All the Files and Directories

Unlike the β€œ-d” flag, the following command will list all the files and directories in your current working directory.

$ compgen -f

Output:

Listing all the files and directories in the current working directory

Listing the Names of Groups

The following command will read the β€œ/etc/group” file to give you the list of all groups in your system.

$ compgen -g

Output:

Listing all the user or system created groups

Listing the Names of Jobs

Check all the background jobs or suspended programs using the β€œCtrl+z” shortcut key by executing the following command.

$ compgen -j

Output:

Listing the suspended jobs

Listing the Names of Shell Reserved Words

The Linux shell reserves a few keywords that you are prohibited from using while creating functions, environment variables, files, or directories.

Execute the following command to get the list of all reserved keywords by shell:

$ compgen -k

Output:

Listing the shell reserved keywords

Listing the Names of Service

The following command will display the list of system services that you can manage with the systemctl command.

$ compgen -s

Output:

Listing all the system services

Listing the Names of Users in Your System

The following command will read the β€œ/etc/passwd” file to give you the list of usernames created by you or by the system services.

$ compgen -u

Output:

Listing all users created by you or by the system

That’s all for now. We’ll talk to you in the next article.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.