Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Use find to search the current directory tree by conditions, and locate to search a prebuilt filename database quickly. which and whereis help locate command-related files, but Bash's type or command -v is better when you need to know how your current shell resolves a command name, including aliases or functions. This lesson separates these different questions.

Search the current file system with find

find walks a directory tree from a starting point and selects entries by name, type, size, or other tests. The following practice assumes Bash and GNU find. If ~/linux-course-11-practice already exists, choose a different name. Stop if the first mkdir fails rather than continuing in an existing directory.

mkdir ~/linux-course-11-practice
cd ~/linux-course-11-practice
mkdir drafts archive
touch drafts/notes.txt drafts/todo.md archive/old.txt

Check your location with pwd, then look for regular files whose names end in .txt. Quotes prevent the shell from expanding *.txt before find receives it. GNU -maxdepth 2 limits the search to two levels from the starting point.

pwd
find . -maxdepth 2 -type f -name '*.txt'

The order may differ by file system, but both paths should appear:

./drafts/notes.txt
./archive/old.txt

-type f selects regular files, and -name matches the name pattern. GNU find -iname performs a case-insensitive name match. On a real system, an unreadable directory may produce a permission error; a matching path does not by itself prove that you can read the file's contents.

Search a filename database with locate

locate searches a prebuilt filename database instead of walking the tree now. If a database is available, this can be fast across a broad area, but files created since its last update may be missing and deleted names may remain temporarily. How inaccessible paths are handled also depends on the implementation and configuration.

locate hosts

The results may include /etc/hosts along with other paths. If locate or its database is missing, consult your distribution's installed tools and index-update procedure. GNU Findutils provides updatedb, but its schedule and permissions are system-specific. Verify that a result exists now with find or ls before acting on it.

Find an executable's path

which generally searches the executable paths listed in the PATH environment variable. The following command may show a path such as /usr/bin/ls or /bin/ls; the result varies by system.

which ls

Implementations of which differ. Do not assume it accounts for every alias or function that might intercept the name in your shell. In Bash, these commands more directly describe how the current shell resolves it:

type -a ls
command -v ls

type -a can show aliases, functions, built-ins, and executable candidates of the same name. command -v is useful for checking whether a name can be resolved. The exact output depends on your shell configuration.

Inspect binary and manual locations with whereis

whereis reports locations associated with a command name, including binaries, source, and manual pages. whereis ls might show both a binary and a manual path. Not every kind of item must be installed, and search locations depend on the implementation and setup.

whereis ls
Question Start with Caution
Which current files match conditions? find Scope and permissions affect time and errors
Which filenames are in the database? locate Database may be stale
Where is a PATH executable? which Not the same as full shell resolution
How will this shell resolve a name? type -a, command -v Depends on current shell settings
Where are a command's binary or manual files? whereis Results depend on environment

Searching for a file and resolving a command name are different tasks. Finding a file named ls with find does not prove that it is the file executed when you type ls. Check type -a ls if that distinction matters.

Official documentation

Read the GNU Findutils manual for find and locate, and the GNU Bash built-ins documentation for type and command. Check man whereis on your system for the installed implementation's options.

More in This Category
Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.

Linux Tutorial / Read Files with cat, less, more, head, and tail

Linux Tutorial / Read Files with cat, less, more, head, and tail

Learn when to use cat, less, more, head, and tail to read Linux files, with a small reproducible example and guidance for long files and logs.

Linux Directory Structure Explained: Purpose of Major Directories

Linux Directory Structure Explained: Purpose of Major Directories

Understand the purpose of major Linux directories such as /etc, /var, /usr, /home, /proc, and /sys, and learn where common system files belong.

Linux Tutorial / Inspect, Pause, and Stop Processes

Linux Tutorial / Inspect, Pause, and Stop Processes

Inspect Linux processes with ps, top, and pgrep, then safely pause, resume, and terminate a practice process while distinguishing shell jobs from services.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux w Command: Show Logged-In Users and Their Processes

Linux w Command: Show Logged-In Users and Their Processes

Learn how to show Logged-In Users and Their Processes with the Linux w command, including practical examples, key options, and important precautions.

Linux readlink Command: Inspect Symbolic Link Targets

Linux readlink Command: Inspect Symbolic Link Targets

Learn how to use the Linux readlink command to display symbolic link targets and optionally resolve canonical paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Linux Tutorial / What Is Linux? Understanding the Kernel, Distributions, and Open Source

Understand how the Linux kernel differs from a distribution, what user space and open source mean, and how to check your system's kernel and distribution versions.

Linux rpm Command: Inspect RPM Packages and the Installed Database

Linux rpm Command: Inspect RPM Packages and the Installed Database

Learn how to inspect RPM Packages and the Installed Database with the Linux rpm command, including practical examples, key options, and important precautions.