How do I get a list of all available shell command

2020-07-09 09:30发布

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.

标签: bash shell
10条回答
我想做一个坏孩纸
2楼-- · 2020-07-09 09:36

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.

查看更多
成全新的幸福
3楼-- · 2020-07-09 09:38

"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楼-- · 2020-07-09 09:44

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)
查看更多
霸刀☆藐视天下
5楼-- · 2020-07-09 09:45
(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename {} \; | sort | uniq)

It doesn't include shell builtins though.

查看更多
欢心
6楼-- · 2020-07-09 09:47

tabtaby

查看更多
祖国的老花朵
7楼-- · 2020-07-09 09:48

Similar to @ghoti, but using find:

#!/bin/sh
for d in ${PATH//:/ }; do
    find $d -maxdepth 1 -type f -executable
done
查看更多
登录 后发表回答