> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Locating and Finding Files

> Guide to using find and locate on Linux, showing when to use each, updating locate database, demo steps, and practical examples for real-time and indexed file searches

As a system administrator you'll frequently need to locate files—configuration files, logs, scripts, or misplaced data. This guide shows how to search a Linux file system quickly and reliably using the `find` and `locate` commands. It explains when to use each tool, demonstrates a short root-level demo, and provides practical `find` examples you can adapt on RHEL or other Linux distributions.

Why use these tools?

* `find` searches the filesystem in real time and is precise and flexible.
* `locate` is much faster because it queries a pre-built database rather than scanning the whole disk.

Use `find` for accuracy and complex filtering. Use `locate` for speed when a periodically updated index is sufficient.

<Callout icon="lightbulb" color="#1CB2FE">
  The `updatedb` command refreshes the database used by `locate`. On many systems this runs automatically (cron or systemd timers), but you can run `updatedb` manually to get the latest results. It typically requires root privileges to index the full filesystem.
</Callout>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/pGYwDVd7uFSlinOw/images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/File-Management/Locating-and-Finding-Files/demo-locating-finding-files-slide.jpg?fit=max&auto=format&n=pGYwDVd7uFSlinOw&q=85&s=34c1af78b7620d7b6a3bb6bb521928ea" alt="A dark presentation slide titled &#x22;Demo: Locating and Finding Files&#x22; on the left. On the right is a simple blue outline icon of a folder with a magnifying glass." width="1920" height="1080" data-path="images/Prep-Course-Red-Hat-Certified-System-Administrator-RHCSA/File-Management/Locating-and-Finding-Files/demo-locating-finding-files-slide.jpg" />
</Frame>

Demo (run as root)

Steps:

1. Create a directory and a file.
2. Try `locate` (it won't find the file until the database is updated).
3. Run `updatedb`, then use `locate` again.
4. Demonstrate `find` examples for real-time search.

Example session:

```bash theme={null}
# become root (enter password when prompted)
su -

# create a directory and a file
mkdir -p /home/student/locateme
touch /home/student/locateme/locatefile

# try locate (no result yet because the database is stale)
locate locatefile

# update the locate database (requires root)
updatedb

# now locate finds the file
locate locatefile
# expected output:
# /home/student/locateme/locatefile
```

Using `locate`

* Very fast because it uses an indexed database.
* By default `locate` is case-sensitive. Use `-i` for case-insensitive searches:

```bash theme={null}
locate -i LOCATEFILE
```

* Keep in mind `locate` results reflect the state of the last database update; newly created files may not appear until after `updatedb` runs.

Using `find` (real-time, powerful)

Basic syntax:

* Start directory then expression:
  * `find <start-dir> <expression>`
* Examples below assume you are searching from the current directory `.` or root `/`.

Common `find` options and examples

| Option / Predicate | Purpose | Example |
| - | - | - |
| `-name` | Match filename (case-sensitive) | `find . -name 'locateme'` |
| `-iname` | Match filename (case-insensitive) | `find / -iname 'passwd*'` |
| `-type` | Filter by file type: `f` file, `d` directory, `l` symlink | `find / -type l` |
| `-user` | Files owned by a specific user | `find / -user student` |
| `-group` | Files owned by a specific group | `find / -group student` |
| `-size` | Match by size. Prefix `+` for greater than, `-` for less than. Units: `c` (bytes), `k`, `M`, `G` | `find / -size +100M -size -200M` |
| Combined predicates | Build complex queries | `find / -type f -user student -size +10M` |

Practical `find` examples

```bash theme={null}
# Find 'locateme' in the current directory
find . -name 'locateme'
# Search the entire filesystem for 'locateme'
find / -name 'locateme'

# Use shell wildcards (quote them to avoid shell expansion)
find / -name 'passwd*'

# Case-insensitive name search
find / -iname 'passwd*'

# Find symbolic links
find / -type l

# Files larger than 100 MiB but smaller than 200 MiB
find / -size +100M -size -200M

# Files owned by user 'student'
find / -user student

# Regular files owned by 'student' larger than 10 MiB
find / -type f -user student -size +10M
```

Advanced capabilities

* `find` supports many more predicates: permissions (`-perm`), modification time (`-mtime`, `-mmin`), pruning directories you want to skip (`-prune`), and executing commands on matches (`-exec`, `-ok`).
* For complex tasks, combine `find` with `xargs`, `-print0`, or `-exec ... +` for efficient processing.

<Callout icon="lightbulb" color="#1CB2FE">
  `find` is extremely flexible — use `man find` to explore predicates like `-mtime`, `-perm`, `-prune`, and `-exec`. For large searches consider `-xdev` to avoid crossing filesystem boundaries, and `-mount` on some systems.
</Callout>

Quick comparison

| Tool | Speed | Accuracy | Typical use |
| - | - | - | - |
| `locate` | Fast (database) | Not real-time; may be stale | Quick lookups when you don't need immediate updates |
| `find` | Slower (scans) | Real-time and precise | Complex queries, real-time discovery, advanced filters |

Links and references

* `man find` — local manual page: run `man find`
* GNU findutils manual: [https://www.gnu.org/software/findutils/manual/](https://www.gnu.org/software/findutils/manual/)
* `man updatedb` / `man locate` — local manual pages for locate and updatedb
* RHEL documentation: [https://access.redhat.com/documentation/](https://access.redhat.com/documentation/)

Summary

* Use `locate` when you want very fast filename lookups and can tolerate slightly out-of-date results; refresh its database with `updatedb` as needed.
* Use `find` for accurate, on-the-fly searches and advanced filtering. It’s slower but far more powerful.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bec5b574-0ba0-45f8-9ddf-03f7cc4f0dd6/lesson/411d0c2f-2830-49bf-bb40-e9e1678cf4a6" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bec5b574-0ba0-45f8-9ddf-03f7cc4f0dd6/lesson/271a4335-092e-4a83-b906-f6e1b1bff37c" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.