Linux命令列出所有可用的命令和别名

ack*_*ack 264 linux terminal command-line

是否有Linux命令列出此终端会话的所有可用命令和别名?

好像你输入'a'并按下了标签,但是对于字母表中的每个字母.或运行'别名'但也返回命令.

为什么?我想运行以下命令,看看命令是否可用:

ListAllCommands | grep searchstr
Run Code Online (Sandbox Code Playgroud)

cam*_*amh 577

您可以使用内置的bash(1) compgen

  • compgen -c 将列出您可以运行的所有命令.
  • compgen -a 将列出您可以运行的所有别名.
  • compgen -b 将列出您可以运行的所有内置插件.
  • compgen -k 将列出您可以运行的所有关键字.
  • compgen -A function 将列出您可以运行的所有功能.
  • compgen -A function -abck 将一次性列出以上所有内容.

检查手册页以了解您可以生成的其他完成.

直接回答你的问题:

compgen -ac | grep searchstr
Run Code Online (Sandbox Code Playgroud)

应该做你想要的.

  • csh/tcsh 有等效的吗?这些终端还具有某种在选项卡上使用的自动完成功能,所以也许存在某些东西? (2认同)
  • 代替`compgen | grep`,将字符串作为参数传递给`compgen`本身会更有效(如果已知为前缀,则暗示该问题)。在这种情况下,它将是`compgen -ac searchstr`。 (2认同)
  • 通过扩展,执行 `compgen -c | 排序| 优衣库 | less` 将打印所有可用的命令,没有重复的行并按字母顺序排序。 (2认同)

Ant*_*sma 39

添加到.bashrc

function ListAllCommands
{
    echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n' | sort -u
}
Run Code Online (Sandbox Code Playgroud)

如果您还想要别名,那么:

function ListAllCommands
{
    COMMANDS=`echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n'`
    ALIASES=`alias | cut -d '=' -f 1`
    echo "$COMMANDS"$'\n'"$ALIASES" | sort -u
}
Run Code Online (Sandbox Code Playgroud)

  • 排序是删除重复. (4认同)

sun*_*256 25

type -a mycommand
Run Code Online (Sandbox Code Playgroud)

命令列出$ PATH中使用mycommand的所有别名和命令.可用于检查命令是否存在于多个变体中.除此之外...可能有一些脚本解析$ PATH和所有别名,但不知道任何这样的脚本.

  • @lothar,如果您正在寻找的命令是,呃,它是什么,"startserver"?,"serverstart"?,"server-something-or-other"?我知道,我只是为服务器"grep -i",看看它是否存在.哎呀.Bzzz,不是这个解决方案.matey :-)我不打算把这个答案投下来(因为它有用,即使是有限的方式)但是一个完整的解决方案会考虑grep是*正则表达式*,而不仅仅是固定字符串. (2认同)

Aar*_*ron 6

使用"which searchstr".如果它是别名,则返回二进制路径或别名设置

编辑:如果您正在寻找别名列表,您可以使用:

alias -p | cut -d= -f1 | cut -d' ' -f2
Run Code Online (Sandbox Code Playgroud)

将其添加到您喜欢的任何PATH搜索答案中.假设你正在使用bash ..


Oli*_*sne 6

others命令在嵌入式系统上对我不起作用,因为它们需要bash或更完整的xargs版本(busybox受到限制)。

以下命令应在任何类似Unix的系统上运行。

按文件夹列出:

ls $(echo $PATH | tr ':' ' ')
Run Code Online (Sandbox Code Playgroud)

按名称列出所有命令

ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort
Run Code Online (Sandbox Code Playgroud)

  • 这仍然依赖于`tr`。为什么不简单地“ls $(echo ${PATH//:/ })”呢? (2认同)
  • 这就是我想要的。简单明了 (2认同)

Kat*_*age 6

或者,您可以获得一个方便的命令列表以及快速描述(只要该命令有一个手册页,大多数命令都有):

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.)
Run Code Online (Sandbox Code Playgroud)

然后你只需按照你想要的方式 grep 即可。

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.)
Run Code Online (Sandbox Code Playgroud)

产量:

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
Run Code Online (Sandbox Code Playgroud)

结果似乎没有排序,因此如果您正在寻找长列表,您可以抛出 | 排序| 到中间,然后将其通过管道传输到寻呼机,例如 less/more/most。翼:

apropos -s 1 '' | grep xdg
Run Code Online (Sandbox Code Playgroud)

它返回名称或简短描述中包含“zip”的所有命令的排序列表,并泵送“less”寻呼机。(您也可以用 $PAGER 替换“less”并使用默认寻呼机。)


vic*_*ugo 5

试试这个脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)

  • 这是迄今为止唯一对所有命令执行此操作的代码解决方案,而不仅仅是查看给定的已知命令是否存在。+1。 (2认同)