Print all commands with pathnames

81 views Asked by At

I am trying to print all of the program file names on a system accessable from bash. (eg the path) I have been using 'which $(compgen -c)' but that does not seem efficient. Is there a better way?

1

There are 1 answers

0
konsolebox On BEST ANSWER

This seems faster:

IFS=: read -ra __ <<< "$PATH"
find "${__[@]}" -mindepth 1 -maxdepth 1 -xtype f -executable

And this isn't faster but it prints accurately the commands found in the filesystem and only printing one path for every name depending on which would get executed first, not twice e.g. if /bin/echo and /usr/bin/echo both exist would only print either of the two. It could happen since compgen -c doesn't only print the echo found in the filesystem but the echo builtin command as well. The command above would print all executables found in $PATH.

type -P $(compgen -c | awk '!a[$0]++')

If your commands has spaces, use IFS=$'\n' in a subshell:

( IFS=$'\n'; type -P $(compgen -c | awk '!a[$0]++'); )

Disabling pathname expansion could also be safer:

( IFS=$'\n'; set -o noglob; type -P $(compgen -c | awk '!a[$0]++'); )

Saving to an array as well:

IFS=$'\n' read -rd '' -a __ < <(compgen -c | awk '!a[$0]++')
type -P "${__[@]}"