列出所有可用命令和别名的 Linux 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/948008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux command to list all available commands and aliases
提问by ack
Is there a Linux command that will list all available commands and aliases for this terminal session?
是否有一个 Linux 命令可以列出此终端会话的所有可用命令和别名?
As if you typed 'a' and pressed tab, but for every letter of the alphabet. Or running 'alias' but also returning commands.
就像您输入了“a”并按下了 Tab 键一样,但对于字母表中的每个字母。或者运行“别名”但也返回命令。
Why? I'd like to run the following and see if a command is available:
为什么?我想运行以下命令并查看命令是否可用:
ListAllCommands | grep searchstr
采纳答案by camh
You can use the bash(1) built-in compgen
您可以使用内置的 bash(1) 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.
compgen -c
将列出您可以运行的所有命令。compgen -a
将列出您可以运行的所有别名。compgen -b
将列出您可以运行的所有内置程序。compgen -k
将列出您可以运行的所有关键字。compgen -A function
将列出您可以运行的所有功能。compgen -A function -abck
将一口气列出以上所有内容。
Check the man page for other completions you can generate.
检查您可以生成的其他完成的手册页。
To directly answer your question:
直接回答你的问题:
compgen -ac | grep searchstr
should do what yout want.
应该做你想做的。
回答by OscarRyz
Why don't you just type:
你为什么不直接输入:
seachstr
In the terminal.
在终端。
The shell will say somehing like
外壳会说类似的东西
seacrhstr: command not found
EDIT:
编辑:
Ok, I take the downvote, because the answer is stupid, I just want to know: What's wrong with this answer!!! The asker said:
好吧,我投反对票,因为答案很愚蠢,我只想知道:这个答案有什么问题!!!提问者说:
and see if a command is available.
并查看命令是否可用。
Typing the command will tell you if it is available!.
输入命令会告诉你它是否可用!。
Probably he/she meant "with out executing the command"or "to include it in a script"but I cannot read his mind ( is not that I can't regularly it is just that he's wearing a mind reading deflector)
可能他/她的意思是“不执行命令”或“将其包含在脚本中”,但我无法读懂他的想法(不是我不能经常这样做,只是他戴着 读心仪)
回答by sunny256
There is the
有
type -a mycommand
command which lists all aliases and commands in $PATH where mycommandis 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.
列出 $PATH 中使用mycommand 的所有别名和命令的命令。可用于检查命令是否存在于多个变体中。除此之外......可能有一些脚本可以解析 $PATH 和所有别名,但不知道任何这样的脚本。
回答by Gabriel Sosa
in debian: ls /bin/ | grep "whatImSearchingFor"
在 debian 中: ls /bin/ | grep "whatImSearchingFor"
回答by OscarRyz
You can always to the following:
您始终可以执行以下操作:
1. Hold the $PATH environment variable value.
2. Split by ":"
3. For earch entry:
ls * $entry
4. grep your command in that output.
The shell will execute command only if they are listed in the path env var anyway.
只有当它们在路径 env var 中列出时,shell 才会执行命令。
回答by Aaron
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..
将其添加到您喜欢的任何 PATH 搜索答案中。假设您正在使用 bash ..
回答by nikudesu
The problem is that the tab-completion is searching your path, but all commands are not in your path.
问题是制表符完成正在搜索您的路径,但所有命令都不在您的路径中。
To find the commands in your path using bash you could do something like :
要使用 bash 查找路径中的命令,您可以执行以下操作:
for x in echo $PATH | cut -d":" -f1
; do ls $x; done
对于 x echo $PATH | cut -d":" -f1
;做 ls $x; 完毕
回答by Igor Krivokon
Try to press ALT-? (alt and question mark at the same time). Give it a second or two to build the list. It should work in bash.
尝试按ALT-?(同时使用 alt 和问号)。给它一两秒钟来构建列表。它应该在 bash 中工作。
回答by victor hugo
Try this script:
试试这个脚本:
#!/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
回答by Craig Wright
Here's a function you can put in your bashrc file:
这是您可以放入 bashrc 文件的函数:
function command-search { oldIFS=${IFS} IFS=":" for p in ${PATH} do ls $p | grep done export IFS=${oldIFS} }
Example usage:
用法示例:
$ command-search gnome gnome-audio-profiles-properties* gnome-eject@ gnome-keyring* gnome-keyring-daemon* gnome-mount* gnome-open* gnome-sound-recorder* gnome-text-editor@ gnome-umount@ gnome-volume-control* polkit-gnome-authorization* vim.gnome* $
FYI: IFS is a variable that bash uses to split strings.
仅供参考:IFS 是 bash 用于拆分字符串的变量。
Certainly there could be some better ways to do this.
当然可以有一些更好的方法来做到这一点。
回答by LB40
maybe i'm misunderstanding but what if you press Escape until you got the Display All X possibilities ?
也许我误解了,但是如果您按 Escape 直到获得 Display All X 可能性怎么办?