Skip to main content
99.99% Uptime SLA Network status
Find a File in Linux: find, locate, grep - Virtarix Blog

Find a File in Linux: find, locate, grep

June 6, 2026 · Blog / Technical Guide

If you need to find a file in Linux, use find when you need accurate live filesystem results, locate when you want a fast indexed filename search, and grep when you need to search inside files for text. The fastest answer depends on whether you know the filename, the directory, or only something inside the file.

On a VPS, scope matters. Searching the whole server can be slow, noisy, and permission-heavy. Start in the most specific directory you know, use safe patterns, and expand only when the first search does not find the file.

Key takeaways

  • Use find /path -name 'file' to search for a filename under a specific directory.
  • Use find /path -iname 'file' for a case-insensitive filename search.
  • Use find /path -type f -name '*.log' to find regular files by extension.
  • Use locate name for fast indexed searches when the database is current.
  • Use grep -R 'text' /path to search inside file contents.
  • For related Linux basics, read the Virtarix guides to Linux command line tips, Linux command syntax, umask basics, and VPS security.

Which command should you use?

Goal Best command Why
Find a current file by name find Searches the live filesystem
Find a name very quickly locate Searches an indexed database
Search inside files grep Matches text inside file contents
Find by file type find Can filter regular files, directories, and links
Find symbolic links find Can match link type and link targets

If you are not sure, start with find in the most likely directory. It is slower than locate, but it reflects the current filesystem instead of a database snapshot.

Step 1: Find a file by exact name

Search a known directory with find:

find /var/www -name 'wp-config.php'

This searches under /var/www for entries named exactly wp-config.php. Quoting the pattern prevents the shell from expanding wildcard characters before find receives them.

Avoid starting with / unless you really need a full-system search. A search rooted at / can produce permission errors and scan directories unrelated to your problem. Start with the application directory, user home, log directory, or backup path you expect.

Step 2: Search without matching case

If you do not know the capitalization, use -iname:

find /var/www -iname 'readme.md'

This can find names such as README.md, Readme.md, or readme.md. Case-insensitive search is useful when files were uploaded from different operating systems or generated by scripts with inconsistent naming.

Step 3: Find files by extension

Use -type f for regular files and -name for a pattern:

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

The -type f filter avoids directories, sockets, and other entry types. The quoted *.log pattern keeps your shell from expanding it in the current directory before the search runs.

For a web app, the same pattern can locate templates, uploads, cache files, or configuration fragments:

find /var/www/example.com -type f -name '*.php'

Run broad extension searches carefully on production servers. They can return thousands of paths.

Step 4: Find directories

Use -type d to search for directories:

find /var/www -type d -name 'cache'

This is useful when you need to locate application cache folders, release directories, upload folders, or plugin directories without matching files that happen to share the same name.

Step 5: Find symbolic links

Use -type l to list symbolic links:

find /var/www -type l

Symbolic links matter during deployments because release directories, shared uploads, and configuration paths often rely on links. If a path points somewhere unexpected, an application may read old code or write data to the wrong location.

If you are cleaning up links, inspect before deleting. A symbolic link can be an intentional part of a deployment process.

Step 6: Use locate for fast filename searches

locate searches a prepared filename database. That makes it fast, but it can miss files created after the database was last updated.

locate nginx.conf

If the command returns nothing, that does not prove the file is absent. It may mean the database is stale or the path is excluded. On systems where you manage the index, update it with the appropriate updatedb workflow, then search again.

Use locate when speed matters and a slightly stale result is acceptable. Use find when you need current truth.

Step 7: Search inside files with grep

Finding a filename is different from finding text inside files. Use recursive grep when you know part of the content:

grep -R 'server_name' ./config

This searches under the local ./config directory for lines containing server_name. It is useful for finding configuration references, API keys that need rotation, old hostnames, include paths, feature flags, and deployment settings.

Use a narrow directory first. Recursive text searches across an entire server can be slow and may scan secrets, binaries, caches, or large uploads.

Step 8: Combine searches with VPS context

A practical VPS search often starts with a question:

  • “Where is the config file?” Use find by name.
  • “Which file mentions this hostname?” Use grep -R in the config directory.
  • “Where did a generated file go?” Use find by extension and modification time.
  • “Is this file anywhere on the box?” Try locate, then confirm with find.
  • “Is this path a symlink?” Use find -type l or inspect the path directly.

Document the path you find. The next admin should not need to repeat the same filesystem search during an incident.

Common mistakes

Searching from the root directory first

A full-system search is sometimes necessary, but it should not be the default. It is slower and produces more permission noise.

Forgetting quotes around wildcards

Use quotes around patterns such as '*.log' so the shell does not expand them before find runs.

Using locate as final proof

locate is fast because it uses an index. Confirm important results with a live filesystem check before changing production files.

Confusing grep with find

find searches filenames and attributes. grep searches text inside files. Use the tool that matches the question.

VPS file-finding checklist

Before you act on a result, confirm:

  1. You searched the right directory.
  2. The command matched filenames, contents, or file types as intended.
  3. The path belongs to the service you are investigating.
  4. The result is current, especially if it came from locate.
  5. You understand whether a result is a file, directory, or symbolic link.
  6. You have a backup or rollback path before editing important files.

If you want a disposable Linux environment for practicing file searches, Virtarix Cloud VPS plans give you an isolated place to test find, locate, grep, symlinks, and safe cleanup habits before changing production servers.

FAQ

What is the best command to find a file in Linux?

Use find when you need a current filesystem search. Start from the most specific directory you know.

What is the difference between find and locate?

find searches the live filesystem. locate searches a prepared database, so it is faster but can be stale.

How do I search inside files in Linux?

Use grep -R 'text' /path to recursively search file contents under a directory.

How do I find symbolic links in Linux?

Use find /path -type l to list symbolic links under a directory.

Summary

To find a file in Linux, choose the tool based on the question. Use find for live filename and file-type searches, locate for fast indexed filename lookups, and grep for text inside files. Start with a narrow path, quote patterns, and verify results before changing production files.

For VPS work, the goal is not only to find the path. The goal is to find the right path safely, understand what it belongs to, and avoid turning a search into an accidental production change. Keep notes on useful paths so repeat incidents are faster, safer, and easier to hand over.

Byline: Peter French — Updated 2026-05-18.

Peter French
About the Author Peter Frenchis the Managing Director at Virtarix, with over 17 years in the tech industry. He has co-founded a cloud storage business, led strategy at a global cloud computing leader, and driven market growth in cybersecurity and data protection.