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 kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Bash Command Operators Explained: ;, &&, ||, &, and |

Bash Command Operators Explained: ;, &&, ||, &, and |

Understand how Bash operators run commands sequentially, conditionally, in the background, or through pipelines, with practical examples and precedence guidance.

Linux alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

Linux jobs Command: List Jobs in the Current Shell

Linux jobs Command: List Jobs in the Current Shell

Learn how to list Jobs in the Current Shell with the Linux jobs command, including practical examples, key options, and important precautions.

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl command, including practical examples, key options, and important precautions.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.