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 namefor fast indexed searches when the database is current. - Use
grep -R 'text' /pathto 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
findby name. - “Which file mentions this hostname?” Use
grep -Rin the config directory. - “Where did a generated file go?” Use
findby extension and modification time. - “Is this file anywhere on the box?” Try
locate, then confirm withfind. - “Is this path a symlink?” Use
find -type lor 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.