是否有一个Linux命令将列出此终端会话的所有可用命令和别名?
就好像你输入“a”,然后按下tab键,不过是针对字母表中的每个字母。 或者运行'alias',但也返回命令。
为什么?我想运行以下命令,看看是否有可用的命令:
ListAllCommands | grep searchstr
是否有一个Linux命令将列出此终端会话的所有可用命令和别名?
就好像你输入“a”,然后按下tab键,不过是针对字母表中的每个字母。 或者运行'alias',但也返回命令。
为什么?我想运行以下命令,看看是否有可用的命令:
ListAllCommands | grep searchstr
当前回答
你可以使用bash(1)内置的compgen
Compgen -c将列出您可以运行的所有命令。 Compgen -a会列出所有你可以运行的别名。 Compgen -b会列出所有你可以运行的内置程序。 Compgen -k将列出所有你可以运行的关键字。 compgen -一个函数将列出所有你可以运行的函数。 compgen -A function -abck将一次性列出上述所有内容。
检查手册页,查看您可以生成的其他完成。
直接回答你的问题:
compgen -ac | grep searchstr
应该做你想做的事。
其他回答
这取决于,我的意思是这取决于你用的是什么外壳。以下是我看到的限制条件:
必须运行在与你的shell相同的进程中,以捕获别名、函数和变量,这些会影响你可以找到的命令,考虑PATH或EDITOR,尽管EDITOR可能超出范围。你可以有未导出的变量来影响事情。 它是特定于shell的,或者你进入内核,/proc/pid/enviorn和朋友没有足够的信息
我使用ZSH,所以这里是一个ZSH的答案,它做以下3件事:
转储文件路径 转储别名 dump环境中的函数 他们排序
下面就是:
feed_me() {
(alias | cut -f1 -d= ; hash -f; hash -v | cut -f 1 -d= ; typeset +f) | sort
}
如果你使用zsh,这应该做到。
根据与命令相关联的关键字列出命令是很有用的。
使用man -k“你的关键字”
随意结合:| grep“另一个词”
例如,要找到一个文本编辑器: Man -k编辑器| grep文本
试试这个脚本:
#!/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
对于Mac用户(find没有-executable, xargs没有-d):
echo $PATH | tr ':' '\n' | xargs -I {} find {} -maxdepth 1 -type f -perm '++x'
或者,你可以得到一个方便的命令列表,并附有快速描述(只要该命令有一个手册页,大多数都有):
apropos -s 1 ''
-s 1 returns only "section 1" manpages which are entries for executable programs.
'' is a search for anything. (If you use an asterisk, on my system, bash throws in a search for all the files and folders in your current working directory.)
然后你想怎么吃就怎么吃。
apropos -s 1 '' | grep xdg
收益率:
xdg-desktop-icon (1) - command line tool for (un)installing icons to the desktop
xdg-desktop-menu (1) - command line tool for (un)installing desktop menu items
xdg-email (1) - command line tool for sending mail using the user's preferred e-mail composer
xdg-icon-resource (1) - command line tool for (un)installing icon resources
xdg-mime (1) - command line tool for querying information about file type handling and adding descriptions for new file types
xdg-open (1) - opens a file or URL in the user's preferred application
xdg-screensaver (1) - command line tool for controlling the screensaver
xdg-settings (1) - get various settings from the desktop environment
xdg-user-dir (1) - Find an XDG user dir
xdg-user-dirs-update (1) - Update XDG user dir configuration
结果似乎没有排序,所以如果您正在寻找一个很长的列表,您可以在中间抛出一个|排序|,然后将其输送到一个寻呼机,如less/more/most。阿拉巴马州:
apropos -s 1 '' | sort | grep zip | less
它返回名称或简短描述中包含“zip”的所有命令的排序列表,并将其泵入“less”寻呼机。(你也可以用$PAGER替换“less”并使用默认的寻呼机。)