Linux command to list all available commands and aliases
Is there a Linux command that will list all available commands and aliases for this terminal session? As if you typed ‘a’ and pressed tab, but for every letter of the alphabet. Or running ‘alias’ but also returning commands. Why? I’d like to run the following and see if a command is available:
ListAllCommands | grep searchstr
20 Answers 20
You can use the bash(1) built-in compgen
- compgen -c will list all the commands you could run.
- compgen -a will list all the aliases you could run.
- compgen -b will list all the built-ins you could run.
- compgen -k will list all the keywords you could run.
- compgen -A function will list all the functions you could run.
- compgen -A function -abck will list all the above in one go.
Check the man page for other completions you can generate.
To directly answer your question:
compgen -ac | grep searchstr
Is there an equivalent to this for csh/tcsh? Those terminals also have some sort of autocompleting function used on tab, so maybe something exists?
Instead of compgen | grep , it can be more efficient to pass the string as argument to compgen itself (if it’s known to be a prefix, as implied in the question). In this case, that would be compgen -ac searchstr .
@MarAvFe: That’s because it is a bash built-in, not a separate command with its own man page. You’ll need to read the bash(1) man page, or run help compgen at a bash command line.
By extension, doing compgen -c | sort | uniq | less will print all commands available without duplicated lines and sorted alphabetically.
@endolith It’s a bash built-in. sh won’t have it (assuming bourne — I have no idea what /system/bin/sh is — that’s a rather non-standard path)
function ListAllCommands < echo -n $PATH | xargs -d : -I <>find <> -maxdepth 1 \ -executable -type f -printf '%P\n' | sort -u >
If you also want aliases, then:
function ListAllCommands < COMMANDS=`echo -n $PATH | xargs -d : -I <>find <> -maxdepth 1 \ -executable -type f -printf '%P\n'` ALIASES=`alias | cut -d '=' -f 1` echo "$COMMANDS"$'\n'"$ALIASES" | sort -u >
This is very close but it’s not including aliases. How can I append alias | cut -f1 to the results but before the sort?
Why bother sorting if the only purpose is to put the output through grep anyway? Unix philosophy is to make simple tools and then chain them together if required, so leave sort out of ListAllCommands and if the user wants the output sorted they can do that.
This does not find commands that are symlinks to executables. Use the option -L on to follow symlinks to their destination. Note: -L is an option and not part of the matching expression, as such it has to be placed before the path on the command line. In this case find -L <>
Might want to redirect STDERR to /dev/null to suppress nonexistent directory warnings. echo -n $PATH | xargs -d : -I <> find <> -maxdepth 1 -executable -type f -printf ‘%P\n’ 2> /dev/null | sort -u (+1 for zsh compatibility)
command which lists all aliases and commands in $PATH where mycommand is used. Can be used to check if the command exists in several variants. Other than that. There’s probably some script around that parses $PATH and all aliases, but don’t know about any such script.
Even if it is not an answer to the question I think it is a better solution to the problem then the call to grep. So you can do type -a foo and if foo isn’t available it returns command not found or something like that. So you are able to check for a command without calling the command itself.
Actually it is an answer to the question, as the OP asked «I’d like to run the following and see if a command is available», so the purpose is to see if a command is available and this answer clearly works.
@lothar, what if the command you’re looking for is, uh, what was it, «startserver»?, «serverstart»?, «server-something-or-other»?. I know, I’ll just «grep -i» for server and see if it’s there. Oops. Bzzz, not with this solution. matey 🙂 I’m not going to vote this answer down (since it’s useful even if in a limited way) but a full blown solution would take into account that grep is for regular expressions, not just fixed strings.
The others command didn’t work for me on embedded systems, because they require bash or a more complete version of xargs (busybox was limited).
The following commands should work on any Unix-like system.
List all commands by name
ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort
Use «which searchstr». Returns either the path of the binary or the alias setup if it’s an alias
Edit: If you’re looking for a list of aliases, you can use:
alias -p | cut -d= -f1 | cut -d' ' -f2
Add that in to whichever PATH searching answer you like. Assumes you’re using bash..
#!/bin/bash echo $PATH | tr : '\n' | while read e; do for i in $e/*; do if [[ -x "$i" && -f "$i" ]]; then echo $i fi done done
This is the only code solution so far that does it for all commands, not just to see if a given known command exists. +1.
Alternatively, you can get a convenient list of commands coupled with quick descriptions (as long as the command has a man page, which most do):
apropos -s 1 '' -s 1 returns only "section 1" manpages which are entries for executable programs. '' is a search for anything. (If you use an asterisk, on my system, bash throws in a search for all the files and folders in your current working directory.)
Then you just grep it like you want.
xdg-desktop-icon (1) - command line tool for (un)installing icons to the desktop xdg-desktop-menu (1) - command line tool for (un)installing desktop menu items xdg-email (1) - command line tool for sending mail using the user's preferred e-mail composer xdg-icon-resource (1) - command line tool for (un)installing icon resources xdg-mime (1) - command line tool for querying information about file type handling and adding descriptions for new file types xdg-open (1) - opens a file or URL in the user's preferred application xdg-screensaver (1) - command line tool for controlling the screensaver xdg-settings (1) - get various settings from the desktop environment xdg-user-dir (1) - Find an XDG user dir xdg-user-dirs-update (1) - Update XDG user dir configuration
The results don’t appear to be sorted, so if you’re looking for a long list, you can throw a | sort | into the middle, and then pipe that to a pager like less/more/most. ala:
apropos -s 1 '' | sort | grep zip | less
Which returns a sorted list of all commands that have «zip» in their name or their short description, and pumps that the «less» pager. (You could also replace «less» with $PAGER and use the default pager.)
How to get a list of all the commands available for Ubuntu?
I want to start using the terminal more often, but I don’t know what are the different commands available to me. Is there a way to list all the different commands that I can make use of?
7 Answers 7
First Method
NB: Thanks to @Rmano. This method doesn’t work with zsh shell.
This will list all commands in your $PATH environment variable.
To store the result in a file you can redirect the output to a file.
Note that this will return an error if any directory names in your $PATH contain spaces. In that case, use this instead:
while read -d ':' dir; do echo "$dir"; done
Second Method
compgen -c | sort -u > commands && less commands
Third Method
Another method is a double Tab click.
Fourth Method
Another method using find command:
Notice that the first command works in bash but not in zsh , which has word split disabled by default. refining-linux.org/archives/38/…
If you are using bash, which is the default shell in all official Ubuntu flavors, run compgen -c to see the available commands including aliases.
Even the commands for gui-based programs are included. So if you do compgen -c | grep thunar and you have the Thunar file manager installed, you'll see commands related to Thunar as well.
@vasa1: Could this answer be more general? I mean it only provides the solution for bash, but as Braiam noted it doesn't work for zsh. If possible could you please expand the answer to cater to a larger audience - obviously only if you know the answer 🙂
Open terminal Ctrl + Alt + t and run this command:
This will list all commands and a simple description of each command.
If you want to save the list you can redirect the result into an output file
whatis `compgen -c` > listOfCommands.txt
So why I used whatis command. The command man whatis gives:
Each manual page has a short description available within it.
whatis searches the manual page names and displays the manual page descrip‐ tions of any name matched.
so in easy words whatis give a general. description of each command
+1 for additional info. whatis `compgen -c` | sort > listOfCommands.txt will help go get in sort list.
Another useful command: apropos
Open up a terminal and press the Tab key twice.
@LorenzoAncora why is it not standard? Does not all Ubuntu have the autocompletion with double Tab as standard behaviour?
Default != standard: in a future maybe we'll have a console that does not support that mechanism, because it's not standard; more, the TAB standard may serve a different function. A standard procedure is listing the content of 'bin', because is part of the official FHS standard and any Linux/Unix system has that directory. The correct functionality (and the ability of the community to help the users) of Ubuntu is ensured by the respect of the standards.
Ok, thanks for the explanation 🙂 I thought that the standard behaviout of the double Tab on Ubuntu was to list the content of PATH 🙂
@LorenzoAncora Listing the contents of any one directory will not show anywhere close to all executable commands. Listing the contents of all PATH directories will not show shell builtins with no corresponding executable (such as cd ). Pressing Tab twice overcomes both these severe limitations. If someone had asked how to show all commands on an arbitrary GNU/Linux system, one might argue that Tab completion is not an adequate solution. Of course anything might change in Ubuntu in the future but the likelihood of tab completion in the default interactive shell going away is minuscule.
A list of command depends greatly on what you have installed, but there are cheats to list all commands. The following works on most bourne-like shells:
- Press Tab twice.
- Use find to find all executables:
ls /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin
Open a terminal window (GNOME terminal is OK, also a configured xTerm).
Your options are:
- By pressing the TAB key ("-><-") twice, you'll complete any command in the console and, if the line is empty, you'll get the number and the names of all available commands. Please note that it may require some time and may list semi-administrative utilities. NOTE: this isn't a standard, for a "cross-shell" way see the other options.
- Use man -k NAME to search for a command (or part of it) and man COMMAND to obtain the manual for that command. Not al commands have a system manual; reading the man before using any administrative utility is always a good idea; trust me.
- Use Midnight Commander ( mc ) to have a nice console (curses) GUI to manage the system and the file system. You may have to install it from your package manager. Don't worry; it is safe and extremely common software.
NOTE: It's made for when you have confusion or difficulty in using the file system. - Use ls /bin | more to know all exential administrative executables; ls /sbin | more for common administrative executables.
- Use ls /usr/sbin | more to know all user executables; ls /usr/sbin | more will give a very huge list of user executables and libraries.
NOTE: If the output from more exceeds one page (screenful), you'll have to scroll py pressing "Page Up" and "Page Down" or spacebar.
You can use COMMAND | grep TEXT to filter the output.
If you have more questions comment under here and don't forget to check the tick next to the answer if I helped you.
Have a nice experience.
Usually, most executables are in /usr/bin , which you haven't mentioned here. Also there's /sbin , which contains executables often used for system administration, such as usermod and ifconfig . And many systems have other binary directories as well, like /usr/games and /usr/local/bin . See Filesystem hierarchy standard and man 7 hier . You might want to expand this to mention important directories for executables besides /bin and /usr/sbin .
This is a bit old, but can be still relevant
And information on using the Ubuntu terminal
the above page has more links at the end which will help you finding more commands for Ubuntu.