How to find a path of a Linux command like a pro nixCraft

One of the most common questions I get is how can I find a Linux command path that I just installed on Linux using a package manager such as apt/dnf command. We have many new developers coming from the Windows world. Many are first-time Linux users. Some are using Linux from WSL, and others are directly dealing with cloud servers over ssh. Let us see some common commands to list or find a path for Linux commands.

ADVERTISEMENT
Internal or built-in shell command
  • External command/executable/file
  • A shell function
  • An alias
  • A keyword
  • How to find out Linux command type

    We need to use the type command to display the path of a Linux command. It will also tell if the command is a built-in shell, an alias, a function, or external command. The syntax is

    type command
    type -t command
    type -a command

    For example, let us find out pwd command type:

    type pwd
    type date
    type hello
    type ping
     
    # Display a single word which is one of 'alias', 'keyword', 'function', 'builtin', 
    # 'file or '' , if command is an alias, shell reserved word, shell function, shell builtin, 
    # disk file, or not found, respectively
    type -t ping
    type -t if
    type -t vi
    type -t nano
     
    # The '-a' option shows all locations containing an executable named ping
    type -a ping

    How to find a path of a Linux command like a proHow to find a path of a Linux command like a pro

    What is the $PATH variable?

    The PATH variable contains a set of directories where executable programs such as ping, date, vi, docker and others are stored on Linux or Unix-like systems. To view your current path use the echo command/printf command:

    echo "$PATH"
     
    # OR
    #
    # More human readable format 
    #
    echo "${PATH//:/$'n'}"

    Here is what I see:

    /home/vivek/google-cloud-sdk/bin
    /home/vivek/.local/bin
    /home/vivek/bin
    /usr/local/sbin
    /usr/local/bin
    /usr/sbin
    /usr/bin
    /sbin
    /bin
    /usr/games
    /usr/local/games
    /snap/bin

    To view all executable stored in /bin/ directory run the ls command as follows:

    ls /bin/
    ls -l /bin/

    How to display information about commands

    Use the command command as follows to list the path of a Linux command:

    command -v date
    command -v pwd
    command -v ping
    command -v docker

    Linux command utility to find the path of a Linux commandLinux command utility to find the path of a Linux command

    How to locate a Linux command

    We can also use the which command to get the path of a Linux command easily. For example:
    which gcc
    which nano

    To print all possible matching paths, pass the -a as follows:
    which -a ls
    Using which command to get the path of a Linux command

    Using which command to get the path of a Linux command

    Getting a path of a Linux command or man page

    Use the whereis command to find the binary, source code and man page for specified program or command on disk. The syntax is:

    whereis command
    whereis gcc
    whereis docker
    whereis lxc
    whereis vim

    Want to only display binaries? Try:
    whereis -b ls
    How about search only for manuals and info pages? Easy:
    whereis -m date
    whereis -m gcc

    Locating the path of the binary, source, and manual page on Linux

    Locating the path of the binary, source, and manual page on Linux

    Find and locate command

    We can also find files by name. For example, search for a file named ‘date’ type:

    locate -b 'date'
    ## OR ##
    find / -name "date" -ls
    # sudo for all files 
    sudo find / -name "date" -ls

    Outputs from the locate command:

    /snap/core/11316/bin/date
    /snap/core/11420/bin/date
    /snap/core18/2066/bin/date
    /snap/core18/2074/bin/date
    /snap/core20/1026/usr/bin/date
    /snap/core20/1081/usr/bin/date
    /usr/bin/date
    /usr/lib/byobu/date

    Displaying help about Linux commands

    We can use the whatis command, help command and man command or info command.

    whatis command

    Each Linux command comes with a manual page (help page describing usage and syntax). In addition, it has a short description available within it. For example, the whatis command searches the manual page names. It displays the manual page descriptions of any name matched in short form:
    whatis ls
    whatis clear
    whatis date
    whatis gcc

    Display one-line manual page descriptions

    Display one-line manual page descriptions
    For all external commands, we use the man command or info command as follows to get a detailed manual on screen:
    man date
    man ls
    man gcc
    man bash
    info ls
    info bash
    man which

    which command man page on Linuxwhich command man page on Linux

    Sample man page on Linux

    For all Bash keywords and builtins, we use the help command:
    help if
    help exit
    help logout
    help type
    help command

    Summing up

    The main hurdle for new Linux users is locating commands. But with the help of this simple page, you can now find command paths and even get help about them using man pages. Say you installed Docker on the Ubuntu server. Then you can use the following commands to find the path and get help:

    whatis docker
    whereis docker
    type -a docker
    which docker
    find / -iname "docker"
    locate -b 'docker'
    man docker

    docker info path linux

    docker info path linux
    For Debian or Ubuntu Linux server, try the following two commands. To list all files installed by the Docker package on the server using the dpkg command:
    dpkg -L docker-ce
    dpkg -L docker-ce | more

    /.
    /etc
    /etc/default
    /etc/default/docker
    /etc/init
    /etc/init/docker.conf
    /etc/init.d
    /etc/init.d/docker
    /lib
    /lib/systemd
    /lib/systemd/system
    /lib/systemd/system/docker.service
    /lib/systemd/system/docker.socket
    /usr
    /usr/bin
    /usr/bin/docker-init
    /usr/bin/docker-proxy
    /usr/bin/dockerd
    /usr/share
    /usr/share/doc
    /usr/share/doc/docker-ce
    /usr/share/doc/docker-ce/README.md
    /usr/share/doc/docker-ce/changelog.Debian.gz

    Display Docker package information using the apt command:
    apt show docker-ce

    Package: docker-ce
    Version: 5:19.03.13~3-0~ubuntu-bionic
    Priority: optional
    Section: admin
    Maintainer: Docker <support@docker.com>
    Installed-Size: 107 MB
    Depends: docker-ce-cli, containerd.io (>= 1.2.2-3), iptables, libseccomp2 (>= 2.3.0), libc6 (>= 2.8), libdevmapper1.02.1 (>= 2:1.02.97), libsystemd0
    Recommends: aufs-tools, ca-certificates, cgroupfs-mount | cgroup-lite, git, pigz, xz-utils, libltdl7, apparmor
    Conflicts: docker (<< 1.5~), docker-engine, docker-engine-cs, docker.io, lxc-docker, lxc-docker-virtual-package
    Replaces: docker-engine
    Homepage: https://www.docker.com
    Download-Size: 22.5 MB
    APT-Manual-Installed: yes
    APT-Sources: https://download.docker.com/linux/ubuntu bionic/edge amd64 Packages
    Description: Docker: the open-source application container engine Docker is a product for you to build, ship and run any application as a lightweight container . Docker containers are both hardware-agnostic and platform-agnostic. This means they can run anywhere, from your laptop to the largest cloud compute instance and everything in between - and they don't require you to use a particular language, framework or packaging system. That makes them great building blocks for deploying and scaling web apps, databases, and backend services without depending on a particular stack or provider.
     
    N: There are 29 additional records. Please use the '-a' switch to see them.

    I hope new Linux users and developers will find these commands useful. Happy coding.

    ADVERTISEMENT

    Posted by Contributor