Linux command to list all available commands and a

2020-01-26 12:40发布

Is there a Linux command that will list all available commands and aliases for this terminal session?

As if you typed 'a' and pressed tab, but for every letter of the alphabet. Or running 'alias' but also returning commands.

Why? I'd like to run the following and see if a command is available:

ListAllCommands | grep searchstr

20条回答
走好不送
2楼-- · 2020-01-26 12:43

You can use the bash(1) built-in 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.

Check the man page for other completions you can generate.

To directly answer your question:

compgen -ac | grep searchstr

should do what yout want.

查看更多
别忘想泡老子
3楼-- · 2020-01-26 12:44

Add to .bashrc

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

If you also want aliases, then:

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
}
查看更多
爷、活的狠高调
4楼-- · 2020-01-26 12:44

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.

查看更多
等我变得足够好
5楼-- · 2020-01-26 12:45

maybe i'm misunderstanding but what if you press Escape until you got the Display All X possibilities ?

查看更多
放我归山
6楼-- · 2020-01-26 12:49

There is the

type -a mycommand

command which lists all aliases and commands in $PATH where mycommand is 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.

查看更多
The star\"
7楼-- · 2020-01-26 12:49

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
查看更多
登录 后发表回答