Operations 11 min read

Master Linux File Search: Using which, whereis, and locate Commands

This guide explains how to locate files and commands on Linux using the which, whereis, and locate utilities, covering their purpose, syntax, options, and practical examples that demonstrate fast and accurate searches in the system.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux File Search: Using which, whereis, and locate Commands

which command

The which command searches the directories listed in the PATH environment variable for the first occurrence of a specified executable and returns its full path.

Syntax: which <executable_name> Common options (though rarely needed) include: -n – specify minimum filename length (rarely used). -p – include the full path in the output. -w – set output column width. -V – display version information.

Example 1 – Find the path of a command:

which lsmod
# /usr/bin/lsmod

which pwd
# /bin/pwd

which adduser
# /usr/sbin/adduser

Explanation: which looks only in the directories defined by PATH, so results vary with different PATH configurations.

Example 2 – Locate the which command itself:

which which
# alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
# /usr/bin/which

Note: The first line shows that which is aliased; the real binary resides at /usr/bin/which.

Example 3 – Search for a built‑in command ( cd ):

which cd
# (no output)

Reason: cd is a shell built‑in, not an external executable, so which cannot find it.

whereis command

The whereis utility locates the binary, source, and manual page files for a given command. It queries a pre‑built database, making it faster than find, but the database is updated only periodically (typically weekly).

Syntax: whereis [-bmsu] [-B path] [-M path] [-S path] filename Key options: -b – search for binaries only. -m – search for manual pages. -s – search for source files. -u – search for files not matching the above categories. -B – specify alternate binary search path. -M – specify alternate manual page search path. -S – specify alternate source search path.

Example – Find all related files for svn :

whereis svn
# svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

Example – Retrieve only binaries for svn :

whereis -b svn
# svn: /usr/bin/svn /usr/local/svn

Example – Retrieve only manual pages for svn :

whereis -m svn
# svn: /usr/share/man/man1/svn.1.gz

Because the database may be outdated, results can include files that have been removed or miss newly created ones until the next update.

locate command

The locate command searches a system‑wide index (maintained by updatedb ) for file names matching a pattern. It is extremely fast compared to find , but may miss very recent files if the index has not been refreshed. Syntax: locate [options] pattern Important options: -e – exclude matches. -1 – safe mode (hide files without read permission). -f – exclude a specific filesystem from the index. -q – quiet mode (suppress errors). -n N – limit output to N results. -r regex – use a regular expression. -o file – specify an alternate database file. -d path – specify the path to the database. -h – display help. -V – display version.

Example 1 – Find all paths containing pwd :

locate pwd
# /bin/pwd
# /etc/.pwd.lock
# /sbin/unix_chkpwd
# /usr/bin/pwdx
# ... (other matches)

Example 2 – Search for files in /etc that start with sh :

locate /etc/sh
# /etc/shadow
# /etc/shadow-
# /etc/shells

Example 3 – Search for files in /etc that start with m :

locate /etc/m
# /etc/magic
# /etc/magic.mime
# /etc/mailcap
# /etc/mailcap.order
# /etc/manpath.config
# /etc/mate-settings-daemon

Note: Because locate relies on the index, newly created or renamed files may not appear until updatedb runs (usually via a daily cron job).

Command Linefile searchlocatewhereiswhich
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.