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

就好像你输入“a”,然后按下tab键,不过是针对字母表中的每个字母。 或者运行'alias',但也返回命令。

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

ListAllCommands | grep searchstr

当前回答

下面是一个你可以放在bashrc文件中的函数:

function command-search
{
   oldIFS=${IFS}
   IFS=":"

   for p in ${PATH}
   do
      ls $p | grep $1
   done

   export IFS=${oldIFS}
}

使用示例:

$ 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*
$

供您参考:IFS是bash用来拆分字符串的变量。

当然,有一些更好的方法可以做到这一点。

其他回答

添加到.bashrc

function ListAllCommands
{
    echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n' | sort -u
}

如果你还需要别名,那么:

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
}

你可以随时进行以下操作:

1. Hold the $PATH environment variable value.
2. Split by ":"
3. For earch entry: 
    ls * $entry 
4. grep your command in that output.

只有当它们被列在env var路径中时,shell才会执行命令。

在到达debian: ls / bin / | grep whatImSearchingFor "

试试这个脚本:

#!/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

尝试按ALT-?(alt和问号同时输入)。给它一两秒钟的时间来构建列表。它应该在bash中工作。