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

时间:2022-03-08 00:11:49

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

21 个解决方案

#1


510  

You can use the bash(1) built-in compgen

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

  • compgen -c will list all the commands you could run.
  • compgen -c将列出您可以运行的所有命令。
  • compgen -a will list all the aliases you could run.
  • compgen -a将列出所有可以运行的别名。
  • compgen -b will list all the built-ins you could run.
  • compgen -b将列出你可以运行的所有内置程序。
  • compgen -k will list all the keywords you could run.
  • compgen -k将列出所有可以运行的关键字。
  • compgen -A function will list all the functions you could run.
  • compgen -A函数将列出你可以运行的所有函数。
  • compgen -A function -abck will list all the above in one go.
  • compgen -A函数-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.

应该做你想要的。

#2


37  

Add to .bashrc

添加到. 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
}

#3


21  

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.

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

#4


6  

Use "which searchstr". Returns either the path of the binary or the alias setup if it's an alias

使用“searchstr”。如果是别名,则返回二进制或别名设置的路径。

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..

把它添加到你喜欢的路径搜索中。假设您正在使用bash . .

#5


5  

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

#6


3  

It's useful to list the commands based on the keywords associated with the command.

根据与命令关联的关键字列出命令是有用的。

Use: man -k "your keyword"

使用:man -k“你的关键字”

feel free to combine with:| grep "another word"

可以结合使用:| grep“另一个单词”

for example, to find a text editor: man -k editor | grep text

例如,要找到一个文本编辑器:man -k编辑器| grep文本。

#7


2  

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中工作。

#8


2  

Here's a solution that gives you a list of all executables and aliases. It's also portable to systems without xargs -d (e.g. Mac OS X), and properly handles paths with spaces in them.

这里有一个解决方案,它给出了所有可执行文件和别名的列表。它还可以移植到没有xargs -d(例如Mac OS X)的系统中,并适当地处理带有空格的路径。

#!/bin/bash
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u | grep "$@"

Usage: myscript.sh [grep-options] pattern, e.g. to find all commands that begin with ls, case-insensitive, do:

用法:myscript。sh [grep-options]模式,例如:查找以ls开头的所有命令,不区分大小写,do:

myscript -i ^ls

#9


2  

For Mac users (find doesn't have -executable and xargs doesn't have -d):

对于Mac用户(find没有-可执行和xargs没有-d):

echo $PATH | tr ':' '\n' | xargs -I {} find {} -maxdepth 1 -type f -perm '++x'

#10


2  

shortcut method to list out all commands. Open terminal and press two times "tab" button. Thats show all commands in terminal

列出所有命令的快捷方式。打开终端,按两次“tab”键。这显示了终端的所有命令。

#11


1  

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才会执行命令。

#12


1  

it depends, by that I mean it depends on what shell you are using. here are the constraints I see:

这取决于,我的意思是这取决于你使用的是什么外壳。这里是我看到的约束:

  1. must run in the same process as your shell, to catch aliases and functions and variables that would effect the commands you can find, think PATH or EDITOR although EDITOR might be out of scope. You can have unexported variables that can effect things.
  2. 必须在与您的shell相同的进程中运行,以捕获别名、函数和变量,这些命令将影响您可以找到的命令、路径或编辑器,尽管编辑器可能超出了范围。您可以拥有可以影响事物的未导出变量。
  3. it is shell specific or your going off into the kernel, /proc/pid/enviorn and friends do not have enough information
  4. 它是shell特定的,或者您将进入内核,/proc/pid/enviorn和朋友没有足够的信息。

I use ZSH so here is a zsh answer, it does the following 3 things:

我用ZSH,这是ZSH的答案,它做了以下三件事:

  1. dumps path
  2. 转储文件路径
  3. dumps alias names
  4. 转储别名
  5. dumps functions that are in the env
  6. 转储在env中的函数。
  7. sorts them
  8. 他们排序

here it is:

这里是:

feed_me() {
    (alias | cut -f1 -d= ; hash -f; hash -v | cut -f 1 -d= ; typeset +f) | sort
}

If you use zsh this should do it.

如果你用zsh,就应该这样做。

#13


1  

The others command didn't work for me on embedded systems, because they require bash or a more complete version of xargs.

其他命令在嵌入式系统上不适合我,因为它们需要bash或更完整的xargs版本。

The following commands should work on any Unix-like system.

下面的命令应该适用于任何类unix系统。

List by folder :

文件夹列表:

ls $(echo $PATH | tr ':' ' ')

List all commands by name

按名称列出所有命令。

ls $(echo $PATH | tr ':' ' ') | grep -v '/' | sort

#14


0  

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

在echo $PATH中,x在$PATH中剪切-d“:”-f1;ls $ x;完成

#15


0  

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 $1
   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.

FYI: IFS是bash用来分割字符串的变量。

Certainly there could be some better ways to do this.

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

#16


0  

Alternatively, you can get a convenient list of commands coupled with quick descriptions (as long as the command has a man page, which most do):

另外,您还可以获得一个方便的命令列表,加上快速的描述(只要命令有一个人页面,大多数都是这样):

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.)

Then you just grep it like you want.

那你就像你想要的那样。

apropos -s 1 '' | grep xdg

yields:

收益率:

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

The results don't appear to be sorted, so if you're looking for a long list, you can throw a | sort | into the middle, and then pipe that to a pager like less/more/most. ala:

结果看起来没有排序,所以如果你在寻找一个长的列表,你可以把|的|放到中间,然后再把它发送到一个更少/更多/更多的传呼机。阿拉巴马州:

apropos -s 1 '' | sort | grep zip | less

Which returns a sorted list of all commands that have "zip" in their name or their short description, and pumps that the "less" pager. (You could also replace "less" with $PAGER and use the default pager.)

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

#17


-1  

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

也许我误解了,但如果你按Escape,直到你得到了所有X的可能性?

#18


-1  

Basic commands:

基本命令:

$ touch :- user for create empty file

$ touch:-用户创建空文件。

Syn:- touch filename

Syn:触摸文件名

Ex: touch rama

例:触摸罗摩

$ls list of files and directories

$ls文件和目录列表。

$ ls –l Long listing

ls -l长清单。

File type, permissions, link files, user(or)owner name, group name, file size, time stamp, file or dir name.

文件类型、权限、链接文件、用户(或)所有者名称、组名称、文件大小、时间戳、文件或dir名称。

– regular (or) normal file

-正常(或)正常文件。

d directory

d目录

l link file

l链接文件

ls –a : show the all (including hidden files)

ls -a:显示所有(包括隐藏文件)

Hidden files and directories start with . (dot)

隐藏文件和目录以。(点)

find more commands @ http://k2schools.com/linux-commands/

找到更多的命令@ http://k2schools.com/linux-commands/。

#19


-2  

compgen -c > list.txt && wc list.txt

#20


-3  

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 )

可能他/她的意思是“执行命令”或“将其包含在脚本中”,但我无法读懂他的思想(不是我不能经常这样做,只是他带着一个读心术的偏转器)

#21


-5  

in debian: ls /bin/ | grep "whatImSearchingFor"

在debian: ls / bin/| grep“whatImSearchingFor”

#1


510  

You can use the bash(1) built-in compgen

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

  • compgen -c will list all the commands you could run.
  • compgen -c将列出您可以运行的所有命令。
  • compgen -a will list all the aliases you could run.
  • compgen -a将列出所有可以运行的别名。
  • compgen -b will list all the built-ins you could run.
  • compgen -b将列出你可以运行的所有内置程序。
  • compgen -k will list all the keywords you could run.
  • compgen -k将列出所有可以运行的关键字。
  • compgen -A function will list all the functions you could run.
  • compgen -A函数将列出你可以运行的所有函数。
  • compgen -A function -abck will list all the above in one go.
  • compgen -A函数-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.

应该做你想要的。

#2


37  

Add to .bashrc

添加到. 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
}

#3


21  

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.

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

#4


6  

Use "which searchstr". Returns either the path of the binary or the alias setup if it's an alias

使用“searchstr”。如果是别名,则返回二进制或别名设置的路径。

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..

把它添加到你喜欢的路径搜索中。假设您正在使用bash . .

#5


5  

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

#6


3  

It's useful to list the commands based on the keywords associated with the command.

根据与命令关联的关键字列出命令是有用的。

Use: man -k "your keyword"

使用:man -k“你的关键字”

feel free to combine with:| grep "another word"

可以结合使用:| grep“另一个单词”

for example, to find a text editor: man -k editor | grep text

例如,要找到一个文本编辑器:man -k编辑器| grep文本。

#7


2  

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中工作。

#8


2  

Here's a solution that gives you a list of all executables and aliases. It's also portable to systems without xargs -d (e.g. Mac OS X), and properly handles paths with spaces in them.

这里有一个解决方案,它给出了所有可执行文件和别名的列表。它还可以移植到没有xargs -d(例如Mac OS X)的系统中,并适当地处理带有空格的路径。

#!/bin/bash
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u | grep "$@"

Usage: myscript.sh [grep-options] pattern, e.g. to find all commands that begin with ls, case-insensitive, do:

用法:myscript。sh [grep-options]模式,例如:查找以ls开头的所有命令,不区分大小写,do:

myscript -i ^ls

#9


2  

For Mac users (find doesn't have -executable and xargs doesn't have -d):

对于Mac用户(find没有-可执行和xargs没有-d):

echo $PATH | tr ':' '\n' | xargs -I {} find {} -maxdepth 1 -type f -perm '++x'

#10


2  

shortcut method to list out all commands. Open terminal and press two times "tab" button. Thats show all commands in terminal

列出所有命令的快捷方式。打开终端,按两次“tab”键。这显示了终端的所有命令。

#11


1  

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才会执行命令。

#12


1  

it depends, by that I mean it depends on what shell you are using. here are the constraints I see:

这取决于,我的意思是这取决于你使用的是什么外壳。这里是我看到的约束:

  1. must run in the same process as your shell, to catch aliases and functions and variables that would effect the commands you can find, think PATH or EDITOR although EDITOR might be out of scope. You can have unexported variables that can effect things.
  2. 必须在与您的shell相同的进程中运行,以捕获别名、函数和变量,这些命令将影响您可以找到的命令、路径或编辑器,尽管编辑器可能超出了范围。您可以拥有可以影响事物的未导出变量。
  3. it is shell specific or your going off into the kernel, /proc/pid/enviorn and friends do not have enough information
  4. 它是shell特定的,或者您将进入内核,/proc/pid/enviorn和朋友没有足够的信息。

I use ZSH so here is a zsh answer, it does the following 3 things:

我用ZSH,这是ZSH的答案,它做了以下三件事:

  1. dumps path
  2. 转储文件路径
  3. dumps alias names
  4. 转储别名
  5. dumps functions that are in the env
  6. 转储在env中的函数。
  7. sorts them
  8. 他们排序

here it is:

这里是:

feed_me() {
    (alias | cut -f1 -d= ; hash -f; hash -v | cut -f 1 -d= ; typeset +f) | sort
}

If you use zsh this should do it.

如果你用zsh,就应该这样做。

#13


1  

The others command didn't work for me on embedded systems, because they require bash or a more complete version of xargs.

其他命令在嵌入式系统上不适合我,因为它们需要bash或更完整的xargs版本。

The following commands should work on any Unix-like system.

下面的命令应该适用于任何类unix系统。

List by folder :

文件夹列表:

ls $(echo $PATH | tr ':' ' ')

List all commands by name

按名称列出所有命令。

ls $(echo $PATH | tr ':' ' ') | grep -v '/' | sort

#14


0  

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

在echo $PATH中,x在$PATH中剪切-d“:”-f1;ls $ x;完成

#15


0  

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 $1
   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.

FYI: IFS是bash用来分割字符串的变量。

Certainly there could be some better ways to do this.

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

#16


0  

Alternatively, you can get a convenient list of commands coupled with quick descriptions (as long as the command has a man page, which most do):

另外,您还可以获得一个方便的命令列表,加上快速的描述(只要命令有一个人页面,大多数都是这样):

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.)

Then you just grep it like you want.

那你就像你想要的那样。

apropos -s 1 '' | grep xdg

yields:

收益率:

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

The results don't appear to be sorted, so if you're looking for a long list, you can throw a | sort | into the middle, and then pipe that to a pager like less/more/most. ala:

结果看起来没有排序,所以如果你在寻找一个长的列表,你可以把|的|放到中间,然后再把它发送到一个更少/更多/更多的传呼机。阿拉巴马州:

apropos -s 1 '' | sort | grep zip | less

Which returns a sorted list of all commands that have "zip" in their name or their short description, and pumps that the "less" pager. (You could also replace "less" with $PAGER and use the default pager.)

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

#17


-1  

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

也许我误解了,但如果你按Escape,直到你得到了所有X的可能性?

#18


-1  

Basic commands:

基本命令:

$ touch :- user for create empty file

$ touch:-用户创建空文件。

Syn:- touch filename

Syn:触摸文件名

Ex: touch rama

例:触摸罗摩

$ls list of files and directories

$ls文件和目录列表。

$ ls –l Long listing

ls -l长清单。

File type, permissions, link files, user(or)owner name, group name, file size, time stamp, file or dir name.

文件类型、权限、链接文件、用户(或)所有者名称、组名称、文件大小、时间戳、文件或dir名称。

– regular (or) normal file

-正常(或)正常文件。

d directory

d目录

l link file

l链接文件

ls –a : show the all (including hidden files)

ls -a:显示所有(包括隐藏文件)

Hidden files and directories start with . (dot)

隐藏文件和目录以。(点)

find more commands @ http://k2schools.com/linux-commands/

找到更多的命令@ http://k2schools.com/linux-commands/。

#19


-2  

compgen -c > list.txt && wc list.txt

#20


-3  

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 )

可能他/她的意思是“执行命令”或“将其包含在脚本中”,但我无法读懂他的思想(不是我不能经常这样做,只是他带着一个读心术的偏转器)

#21


-5  

in debian: ls /bin/ | grep "whatImSearchingFor"

在debian: ls / bin/| grep“whatImSearchingFor”