List all binaries from $PATH
This is not an answer, but it's showing binary, a command which you could run
compgen -c
(assuming bash
)
Other useful commands
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.
With zsh:
whence -pm '*'
Or:
print -rC1 -- $commands
(note that for commands that appear in more than one component of $PATH
, they will list only the first one).
If you want the commands without the full paths, and sorted for good measure:
print -rC1 -- ${(ko)commands}
(that is, get the keys of that associative array instead of the values).
In any POSIX shell, without using any external command (assuming printf
is built in, if not fall back to echo
) except for the final sorting, and assuming that no executable name contains a newline:
{ set -f; IFS=:; for d in $PATH; do set +f; [ -n "$d" ] || d=.; for f in "$d"/.[!.]* "$d"/..?* "$d"/*; do [ -f "$f" ] && [ -x "$f" ] && printf '%s\n' "${x##*/}"; done; done; } | sort
If you have no empty component in $PATH
(use .
instead) nor components beginning with -
, nor wildcard characters \[?*
in either PATH components or executable names, and no executables beginning with .
, you can simplify this to:
{ IFS=:; for d in $PATH; do for f in $d/*; do [ -f $f ] && [ -x $f ] && echo ${x##*/}; done; done; } | sort
Using POSIX find
and sed
:
{ IFS=:; set -f; find -H $PATH -prune -type f -perm -100 -print; } | sed 's!.*/!!' | sort
If you're willing to list the rare non-executable file or non-regular file in the path, there's a much simpler way:
{ IFS=:; ls -H $PATH; } | sort
This skips dot files; if you need them, add the -A
flag to ls
if yours has it, or if you want to stick to POSIX: ls -aH $PATH | grep -Fxv -e . -e ..
There are simpler solutions in bash and in zsh.