4 Ways to Search for Files in Linux (find, grep, locate, whereis)

In the Linux world, there are a lot of ways to search for files, and each has its own strengths and trade-offs.

In this post we’ll cover four of them—findgreplocate, and whereis—with practical examples, their output, and tips on when to use each.

Quick comparison

CommandSearchesSpeedBest for
findThe live filesystem (directory tree)SlowerFlexible searches by name, size, type, permissions, time
grepInside file contentsSlower on big filesFinding text/patterns within files
locateA prebuilt name databaseVery fastInstant name lookups (may be slightly stale)
whereisA few fixed locationsFastLocating binaries, man pages, and source

1. Search files with the find command

The most flexible way to search files in Linux is the find command. It walks a directory tree and returns files matching the criteria you specify. For example, to find every .txt file starting at the root:

find / -name "*.txt"

Output (example):

/usr/share/doc/readme.txt
/home/user/notes.txt
/var/tmp/output.txt

The find command can search by file name, extension, file size, and file permissions. You can also restrict the search to specific directories or exclude files and directories.

Keep in mind that find can be slow on large directory trees, and its syntax takes some getting used to—the man page is worth a read. When scanning big trees, the -xdev option keeps the search on a single filesystem, which avoids errors and speeds things up.

Examples of the find command

Find files by name (more examples):

find / -name "*.txt"

Find files with a specific extension, staying on one filesystem:

find / -type f -name "*.txt" -xdev
  • -xdev — don’t cross filesystem boundaries
  • -type f — match regular files only

Find files of a certain size:

find / ! -type d -size +1000k
  • ! -type d — exclude directories
  • +1000k — larger than 1000 KB (use -1000k for smaller than 1000 KB)

Output (example):

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

/var/log/syslog.1
/home/user/backup.tar

Find empty files and directories:

find / -empty

Search recursively and grep inside the matches:

find / -name "*.txt" -type f -print0 | xargs -0 grep "foo"
  • -name — filename pattern to match
  • -type f — the file type is a regular file
  • -print0 — separate results with a NUL character (safe for names with spaces)
  • xargs -0 — read NUL-separated input from standard input

Output (example):

/home/user/notes.txt:foo bar baz
/var/tmp/output.txt:another foo line

2. Search file contents with the grep command

grep searches inside text files for patterns or strings. For example, to find every line in a file that contains the word “Linux”:

grep "Linux" filename

Output (example):

Linux is a free and open-source operating system.
Many servers run Linux.

grep shines at locating specific text in large files, and it supports powerful regular expressions for complex patterns. Two caveats: it can be slow on very large files, and it doesn’t work well on binary files (executables, images).

Use -r (or -R) to recurse through subdirectories, and -i to ignore case.

Examples of the grep command

Search for a word in a file:

grep 'howtouselinux' filename

Case-insensitive search:

grep -i 'howtouselinux' filename

Output (example):

Welcome to HowToUseLinux
visit howtouselinux.com today

Recursive search through a whole directory:

grep -r "howtouselinux" /etc/

Output (example):

/etc/motd:Managed by howtouselinux
/etc/hosts:127.0.0.1  howtouselinux.local

3. Search files with the locate command

locate searches a prebuilt database of file names rather than the live filesystem, which makes it much faster than find. For example:

locate "*.txt"

Output (example):

/home/user/notes.txt
/usr/share/doc/readme.txt
/var/tmp/output.txt

The trade-off is freshness: the database is updated periodically (not in real time), so recently created files may not appear yet. Refresh it manually with:

sudo updatedb

Tip: limit how many results are returned with -n:

locate -n 10 "*.conf"

(If locate isn’t installed, add the mlocate or plocate package.)

4. Search files with the whereis command

whereis locates the binarysource, and manual page for a command by checking a handful of standard locations—so it’s fast. For example, to find the ls binary and its man page:

whereis ls

Output:

ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

You can see which locations whereis will search with:

whereis -l

Useful options:

  • -b — search only for binaries (faster, more targeted)
  • -m — search only for manual pages
whereis -b ls

Output:

ls: /usr/bin/ls

Summary

  • Use find when you need flexible, real-time searches by name, size, type, permissions, or time.
  • Use grep when you need to search inside files for text or patterns.
  • Use locate for near-instant name lookups (run updatedb first if the database might be stale).
  • Use whereis to quickly locate a command’s binary, source, and man page.

Keep these tips in mind and you’ll find what you’re looking for in Linux in no time.

Avatar photo
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 715

Leave a Reply

Your email address will not be published. Required fields are marked *