Navigation basics

Linux commands: which

Learn how the Linux which command shows the full path where an executable is stored on disk, and why it cannot locate shell aliases or built-in functions.

8 minute lesson

~~~

When you type a command name, the shell may resolve an alias, function, built-in, or executable found through $PATH. which commonly searches for an executable path:

You can do so using which. The command will return the path to the command specified:

Terminal showing which ls returning /bin/ls and which docker returning /usr/local/bin/docker

That is useful, but it does not always describe what your current shell will actually run. Prefer the shell-aware command:

type ls
type -a node
command -v git

type can identify aliases, functions, and built-ins as well as files. type -a shows every matching definition, which helps when two Node.js installations or package managers compete in $PATH. command -v is a portable choice in shell scripts.

Inspect the search path itself:

printf '%s\n' "$PATH" | tr ':' '\n'

Directories are searched from left to right. Adding a directory at the front can shadow a system command; adding it at the end gives existing commands priority. After installing a tool, a shell may cache an older resolution. Starting a new shell or using its cache-refresh command can clear that confusion.

Do not execute an unfamiliar path merely because which found it. Inspect ownership and permissions first with ls -l, especially on shared systems.

Try it: run type -a for cd, ls, and a language runtime. Explain why their results differ.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →