Linux command to list all available commands and aliases

前端 未结 20 2306
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 14:11

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

相关标签:
20条回答
  • 2020-11-29 14:53

    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. it is shell specific or your going off into the kernel, /proc/pid/enviorn and friends do not have enough information

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

    1. dumps path
    2. dumps alias names
    3. dumps functions that are in the env
    4. sorts them

    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.

    0 讨论(0)
  • 2020-11-29 14:57

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

    0 讨论(0)
  • 2020-11-29 14:59

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

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

    List by folder :

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

    List all commands by name

    ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort
    
    0 讨论(0)
  • 2020-11-29 15:01

    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
    }
    
    0 讨论(0)
  • 2020-11-29 15:01

    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
    
    0 讨论(0)
  • 2020-11-29 15:02

    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.

    #!/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 -i ^ls
    
    0 讨论(0)
提交回复
热议问题