How do I get a list of all available shell command

2020-07-09 09:41发布

问题:

In a typical Linux shell (bash) it is possible to to hit tab twice, to get a list of all available shell commands.

Is there a command which has the same behaviour? I want to pipe it into grep and search it.

回答1:

You could use compgen. For example:

compgen -c

You also could grep it, like this:

compgen -c | grep top$

Source: http://www.cyberciti.biz/open-source/command-line-hacks/compgen-linux-command/



回答2:

You can list the directories straight from $PATH if you tweak the field separator first. The parens limit the effect to the one command, so use: (...) | grep ...

(IFS=': '; ls -1 $PATH)


回答3:

"tab" twice & "y" prints all files in the paths of $PATH. So just printing all files in PATH is sufficient.

Just type this in the shell:

 # printf "%s\n" ${PATH//:/\/* } > my_commands

This redirect all the commands to a file "my_commands".



回答4:

List all the files in your PATH variable (ls all the directories in the PATH). The default user and system commands will be in /bin and /sbin respectively but on installing some software we will add them to some directory and link it using PATH variable.



回答5:

There may be things on your path which aren't actually executable.

#!/bin/sh
for d in ${PATH//:/ }; do
  for f in "$d"/*; do
    test -x "$f" && echo -n "$f "
  done
done
echo ""

This will also print paths, of course. If you only want unqualified filenames, it should be easy to adapt this.

Funny, StackOverflow doesn't know how to handle syntax highlighting for this. :-)



回答6:

tabtaby



回答7:

Similar to @ghoti, but using find:

#!/bin/sh
for d in ${PATH//:/ }; do
    find $d -maxdepth 1 -type f -executable
done


回答8:

Bash uses a builtin command named 'complete' to implement the tab feature.

I don't have the details to hand, but the should tell you all you need to know:

help complete 


回答9:

(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename {} \; | sort | uniq)

It doesn't include shell builtins though.



回答10:

An answer got deleted, I liked it most, so I'm trying to repost it:

compgen is of course better

echo $PATH | tr ':' '\n' | xargs -n 1 ls -1

I found this to be the most typical shell thing, I think it works also with other shells (which I doubt with things like IFS=':' )

Clearly, there maybe problems, if the file is not an executable, but I think for my question, that is enough - I just want to grep my output - which means searching for some commands.



标签: bash shell