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.

Distinguish a search for paths from a search within file content. Use the text filtering and counting lesson for matching lines.

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.

On Ubuntu 26.04, install a missing locate command with sudo apt install plocate, then build the index with sudo updatedb. On Rocky Linux 9, first query enabled repositories with dnf provides '*/locate' and install the package actually returned. Indexing scans the system: use the practice VM and account for exclusions, permissions, and index age.

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.