Have you ever mashed away at 70 to 120 words per minute (or more or less) on your linux box – OR as I like to call hacking – OR sometimes linuxing – and then of the blue… Brain Freeze! (and not from your ice-cream your eating)… But… You simply cant remember a command… On top of that your too lazy to open up a browser and google it (as it might require some keyboarding – unless your familiar with w3m or whatever cli browser is out there).

Well lucky for us there is a command, that I like to think of it like the “?” command with Netgear/HP/Cisco interfaces. Where it just lists all of the commands. Unlike the awesome “?” this command will not go further than the command name, for example the arguments  – you will not find those out with this command. You will need to use –help. OR use “man your-forgotten-command” (which you just remember because you used this helpful trick of a command).

Dun… dun… dun…

Ill show you the output of all that later. compgen -c literally lists all of the commands – bash and all executable files that are found in your echo $PATH 

 

EXERPT FROM STACKEXCHANGE

CITATION: This section is an exerpt from the top comment : http://unix.stackexchange.com/questions/151118/understand-compgen-builtin-command by user cuonglm

Options for compgen command are the same as complete, except -p and -r. From compgen man page:

For options [abcdefgjksuv]:

  • -a means Names of alias
  • -b means Names of shell builtins
  • -c means Names of all commands  <— this is what I use 🙂
  • -d means Names of directory
  • -e means Names of exported shell variables
  • -f means Names of file and functions
  • -g means Names of groups
  • -j means Names of job
  • -k means Names of Shell reserved words
  • -s means Names of service
  • -u means Names od userAlias names
  • -v means Names of shell variables

You can see complete man page here.

 

BACK TO MY OWN MATERIAL

Output of compgen -c (I usually like to sort it) – yah here is all my commands I dont care if you hack this box (as you dont know which box this is bwahaha)

NOTE: some commands appear twice as it shows all commands from PATH and all of the built in commands from the shell (/bin/bash) in my case

FINDING COMMANDS WITH COMPGEN

Wow thats alot. As you can imagine you can use it with grep.

I remember some command has the word disk in it.

Awesome now I remember my command was cfdisk. Cool now if I dont remember the arguments for cfdisk. I can just do cfdisk -h  or cfdisk --help  or man cfdisk  or google for “how I use cfdisk” or “cfdisk examples” or “cfdisk man page

Now compgen -c comes with a sort of find feature by it self. If you remember the beginning of the command. For example I remember my command starts with disk

Caveat of using it with the above method: Notice it didnt find any of the command’s where disk was in the middle. So I prefer to use disk

SUMMARY

If you cant think of a command run compgen -c  or compgen -c | grep what-you-remember-the-command-has

 

Leave a Reply

Your email address will not be published. Required fields are marked *