Skip to main content
Find a File in Linux: find, locate, grep - Virtarix Blog

Find a File in Linux: find, locate, grep

June 5, 2026 · Blog / Technical Guides

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:

“`bash title="Find wp-config.php under the web root" 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`:

```bash title="Find README files without matching case"
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:

“`bash title="Find log files under /var/log" 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:

```bash title="Find PHP files under a website"
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:

“`bash title="Find cache directories under the web root" 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:

```bash title="List symbolic links under the web root"
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.

“`bash title="Search the locate index for nginx.conf" 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:

```bash title="Search config files for server_name"
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.

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.

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.