Shell - How to find directory of some command?

前端 未结 7 857
既然无缘
既然无缘 2020-12-04 06:56

I know that when you are on shell, the only commands that can be used are the ones that can be found on some directory set on PATH. Even I don\'t know how to see what dirs a

相关标签:
7条回答
  • 2020-12-04 07:05

    An alternative to type -a is command -V

    Since most of the times I am interested in the first result only, I also pipe from head. This way the screen will not flood with code in case of a bash function.

    command -V lshw | head -n1
    
    0 讨论(0)
  • 2020-12-04 07:06
    ~$ echo $PATH
    /home/jack/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    ~$ whereis lshw
    lshw: /usr/bin/lshw /usr/share/man/man1/lshw.1.gz
    
    0 讨论(0)
  • 2020-12-04 07:12

    If you're using Bash or zsh, use this:

    type -a lshw
    

    This will show whether the target is a builtin, a function, an alias or an external executable. If the latter, it will show each place it appears in your PATH.

    bash$ type -a lshw
    lshw is /usr/bin/lshw
    bash$ type -a ls
    ls is aliased to `ls --color=auto'
    ls is /bin/ls
    bash$ zsh
    zsh% type -a which
    which is a shell builtin
    which is /usr/bin/which
    

    In Bash, for functions type -a will also display the function definition. You can use declare -f functionname to do the same thing (you have to use that for zsh, since type -a doesn't).

    0 讨论(0)
  • 2020-12-04 07:18

    Like this:

    which lshw
    

    To see all of the commands that match in your path:

    which -a lshw 
    
    0 讨论(0)
  • 2020-12-04 07:21

    PATH is an environment variable, and can be displayed with the echo command:

    echo $PATH
    

    It's a list of paths separated by the colon character ':'

    The which command tells you which file gets executed when you run a command:

    which lshw
    

    sometimes what you get is a path to a symlink; if you want to trace that link to where the actual executable lives, you can use readlink and feed it the output of which:

    readlink -f $(which lshw)
    

    The -f parameter instructs readlink to keep following the symlink recursively.

    Here's an example from my machine:

    $ which firefox
    /usr/bin/firefox
    
    $ readlink -f $(which firefox)
    /usr/lib/firefox-3.6.3/firefox.sh
    
    0 讨论(0)
  • 2020-12-04 07:21

    The Korn shell, ksh, offers the whence built-in, which identifies other shell built-ins, macros, etc. The which command is more portable, however.

    0 讨论(0)
提交回复
热议问题