有没有一个Linux命令可以列出此终端会话中所有可用的命令和别名?
就像您键入'a'并按Tab键一样,但适用于字母表中的每个字母。 或者运行“alias”,但也返回命令。
为什么?我想运行以下命令,以查看命令是否可用:
ListAllCommands | grep searchstr
有没有一个Linux命令可以列出此终端会话中所有可用的命令和别名?
就像您键入'a'并按Tab键一样,但适用于字母表中的每个字母。 或者运行“alias”,但也返回命令。
为什么?我想运行以下命令,以查看命令是否可用:
ListAllCommands | grep searchstr
compgen
命令:
compgen -c
将列出您可以运行的所有命令。compgen -a
将列出您可以运行的所有别名。compgen -b
将列出您可以运行的所有内置命令。compgen -k
将列出您可以运行的所有关键字。compgen -A function
将列出您可以运行的所有函数。compgen -A function -abck
会一次性列出上述所有信息。compgen -ac | grep searchstr
应该能够满足你的需求。
compgen -c | sort | uniq | less
,可以按字母顺序打印出所有可用的命令,并去除重复行。 - Fabiánsh
没有它(假设是Bourne shell - 我不知道/system/bin/sh
是什么 - 那是一个相当非标准的路径)。 - camh添加到 .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
}
-L
选项来跟随符号链接到其目标。注意:-L
是一个选项而不是匹配表达式的一部分,因此必须在命令行路径之前放置。在此情况下,使用find -L {}
。 - Adaephonecho -n $PATH | xargs -d : -I {} find {} -maxdepth 1 -executable -type f -printf '%P\n' 2> /dev/null | sort -u
(+1 用于 zsh 兼容性) - TheLonelyGhost这里有
type -a mycommand
查看在$PATH中使用mycommand命令的所有别名和命令的命令。可以用来检查该命令是否存在于多个变体中。除此之外...可能有一些脚本解析$PATH和所有别名,但不知道是否有这样的脚本。
由于嵌入式系统需要bash或更完整的xargs版本(busybox受限),因此其他命令对我无效。
以下命令应适用于任何类Unix系统。
按文件夹列出:
ls $(echo $PATH | tr ':' ' ')
列出所有命令的名称ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort
ls $(echo ${PATH//:/ })
,而还要依赖于 tr
呢?请帮忙翻译。 - mschilli使用"which searchstr"命令。如果是别名,则返回别名设置;如果是二进制文件则返回路径。
编辑: 如果你正在寻找别名列表,可以使用:
alias -p | cut -d= -f1 | cut -d' ' -f2
或者,您可以获取一个方便的命令列表,其中包含快速描述(只要该命令有手册页面,大部分都有):
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
结果似乎没有排序,因此如果你正在寻找一个长列表,你可以在中间加上 | sort | ,然后将其传递到像 less/more/most 这样的翻页器中。例如:
apropos -s 1 '' | sort | grep zip | less
这将返回一个已排序的命令列表,其中所有命令都包含“zip”在其名称或简短描述中,并使用“less”分页器。 (您还可以将“less”替换为$PAGER并使用默认分页器。)
试试这个脚本:
#!/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'
#!/bin/bash
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u | grep "$@"
用法:myscript.sh [grep选项] 模式
,例如要查找所有以ls
开头的命令(不区分大小写),请执行:
myscript -i ^ls
根据命令的关键字列出命令是非常有用的。
使用:man -k "关键字"
可以随意与| grep "另一个单词"
结合使用
例如,查找文本编辑器:
man -k 编辑器 | grep 文本
compgen
的此回答。 - Matthias Braun