Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To search in a Linux terminal, choose the command by what you need: use find to inspect a live directory tree, grep to search text inside files, and locate for a fast lookup in a filename database. Use xargs when you need to pass discovered paths to another command.

Which Linux search command should you use?

What you need Start with What it searches Key limitation
Find a path or filename in a directory tree find Entries beneath the starting directories you provide It traverses the tree, which can take time across a large scope.
Find matching text inside files grep Input files, selecting lines that match a pattern Pattern modes and options vary by implementation.
Quickly look up a path by name locate A maintained filename database The database may be stale or omit excluded directories.
Pass found paths to another command xargs Converts input items into command arguments Use null-delimited input for unusual filenames when supported.

GNU Findutils documents find, locate, and xargs as tools for listing, searching, and acting on files; updatedb creates the database used by locate (GNU Findutils manual).

How do I find a file in the Linux terminal?

Use find when you want to check the filesystem as it exists now. It searches from each starting path and evaluates tests against the entries it encounters. Start at the most relevant directory to avoid an unnecessarily broad traversal.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find by filename

find . -type f -name '*.log'

This searches beneath the current directory (.) for regular files whose names end in .log. Keep the wildcard in quotes: otherwise, the shell may expand it before find receives the pattern. The pattern is interpreted by find, not by the shell (GNU Findutils manual).

Find a name without case sensitivity

find . -type f -iname '*report*'

On GNU find, -iname matches the name without case sensitivity. This is a GNU-specific example; other implementations may have different options. Check man find on the target system before relying on an option across Unix-like systems.

Narrow the search when results are excessive

Change the starting point from . to the directory most likely to contain the file, and combine it with a suitable test such as -type f or -name. For example, search ~/Documents rather than your entire home directory if that is where the file should be.

How do I search inside files in Linux?

Use grep to select lines in input files that match a pattern. With GNU grep, this command recursively searches /etc for the text timeout in files ending in .conf:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
grep -Rni --include='*.conf' 'timeout' /etc

Here, -R searches recursively, -n shows line numbers, and -i ignores case. --include limits the search to matching filenames; it is an option in GNU grep, not a promise that every implementation supports it. The POSIX grep interface and Linux implementations can differ in details, so consult man grep on your machine when an option is unavailable or behaves differently (POSIX grep utility specification).

When should I use locate instead of find?

Use locate when a quick name lookup is more important than checking the live filesystem directly:

locate 'sshd_config'

locate searches a database rather than walking directories at query time. Its results depend on when the database was updated and which directories its configuration includes. It can therefore miss a newly created file or anything in an excluded tree. If current existence matters, verify a result with find or another direct check. Updating the database is typically an administrator-controlled operation; do not assume an ordinary user can refresh the system-wide index (GNU Findutils manual).

How do I search found files safely?

To search content only in files discovered by find, you can pass paths to grep through xargs. Newline-separated path lists are unsafe for arbitrary filenames because a filename can itself contain a newline. On GNU/Linux systems where both tools support null-delimited paths, use:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
find . -type f -print0 | xargs -0 grep -n 'needle'

-print0 separates paths with a null character, and xargs -0 reads that format, so spaces, quotes, and newlines in filenames are not mistaken for separators. Confirm support in the local manuals for find and xargs; option availability can differ outside the GNU tools. GNU Findutils documents combining its tools to act on discovered paths (GNU Findutils manual).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why did my Linux search miss a file or return unexpected results?

  • find returns too many entries: Use a narrower starting directory and add relevant tests such as -type f or -name.
  • A wildcard matches unexpectedly: Quote the pattern so the utility, rather than the shell, interprets it.
  • locate misses a recent file: Its database may not include the latest filesystem changes, or the directory may be excluded from the database.
  • grep rejects an option: Check man grep; the option may be GNU-specific rather than part of the POSIX interface.
  • A pipeline mishandles unusual filenames: Use null-delimited output and input if both commands support it.

Before acting on a broad set of results, inspect the paths and restrict the search to the intended directory. Search output is not a safe substitute for reviewing what a later command will change or delete.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.