which, type & snap

Locate the program behind a command, and meet the universal snap package format.

Syntaxwhich command type command command -v command

When you type a command, how does the shell find the program? These tools tell you.

which & type

  • which python3 — prints the full path of the program that will run.
  • type ls — says whether a name is a program, a shell builtin, or an alias.
  • command -v git — a portable way to check if a command exists (handy in scripts).

snap

snap is a newer, distro-independent package format from Ubuntu. Snaps bundle all their dependencies, so the same package works everywhere, at the cost of larger size.

  • sudo snap install code --classic — install a snap.
  • snap list — show installed snaps.

Example

Example · bash
# Where does this command live?
which python3
# /usr/bin/python3

# What kind of thing is 'll'?
type ll
# ll is aliased to `ls -alF'

# Install a snap package
sudo snap install htop

When to use it

  • A developer runs 'which python3' to confirm the active Python binary path when multiple versions are installed.
  • A sysadmin uses 'type ls' to discover that 'ls' is aliased to 'ls --color=auto' before scripting with it.
  • A DevOps engineer runs 'which kubectl' in a CI container to verify the tool is on PATH before running deploy commands.

More examples

Locate a command binary

Uses which to find the full path of executables on PATH, useful when multiple versions may be installed.

Example · bash
which python3
# /usr/bin/python3

which nginx
# /usr/sbin/nginx

# Check if a command exists at all
which nonexistent || echo 'not found'

Inspect command type

Shows how 'type' distinguishes between aliases, shell builtins, and external binaries, unlike 'which'.

Example · bash
type ls
# ls is aliased to 'ls --color=auto'

type cd
# cd is a shell builtin

type python3
# python3 is /usr/bin/python3

Find all instances on PATH

Uses -a to reveal all instances of a command across PATH, helping resolve version conflicts.

Example · bash
# -a shows ALL matches, not just the first
which -a python
# /usr/bin/python
# /usr/local/bin/python

# type -a does the same
type -a python

Discussion

  • Be the first to comment on this lesson.