How to Use the Find and Locate Commands in Linux

This post covers how to use the find and locate commands to search for files and directories on a Linux system. It explains the key differences between the two commands, provides beginner-friendly examples, and shows how to combine find with the -exec flag for powerful file operations.

How to Use the Find and Locate Commands in Linux

Searching for files on a Linux system is one of those tasks you will do constantly, whether you are tracking down a config file, a script you wrote last week, or a log that holds the answer to a tricky problem. Two commands make this job much easier: find and locate. They approach the same goal from different angles, and knowing when to use each one is a core Linux+ skill.

The find command searches the live filesystem in real time. It is powerful, flexible, and a little verbose at first, but the syntax follows a consistent pattern:

find [starting directory] [options] [expression]

Here are a few practical examples to get you started.

Find a File by Name

find /home -name "notes.txt"

This searches the /home directory and all subdirectories for a file named exactly notes.txt. If you want a case-insensitive search, swap -name for -iname:

find /home -iname "notes.txt"

Find Files by Type

You can narrow results by specifying whether you are looking for a file or a directory using the -type flag:

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

Here, -type f limits results to regular files. Use -type d to search for directories instead.

Find Files by Size

find /tmp -size +10M

This finds files in /tmp larger than 10 megabytes. You can use k for kilobytes, M for megabytes, and G for gigabytes. The + means "greater than" and - means "less than."

Find Files by Modification Time

find /etc -mtime -7

This returns files in /etc that were modified within the last 7 days. This is useful for auditing recent changes to configuration files.

The locate Command: Database-Driven Speed

While find searches the live filesystem, locate searches a pre-built database. This makes it significantly faster, especially across large directory trees. The trade-off is that the database is not always up to the minute.

Basic Usage

locate notes.txt

That is it. locate will return every path in the database that matches. For a case-insensitive search, use the -i flag:

locate -i notes.txt

Updating the Database

The database locate uses is typically updated automatically by a scheduled cron job, but you can refresh it manually with:

sudo updatedb

If you just created a file and locate cannot find it, run updatedb first. On some systems, the locate command is part of the mlocate package. If it is not installed, you can add it with:

sudo apt install mlocate    # Debian/Ubuntu
sudo dnf install mlocate    # RHEL/Fedora

When to Use Each Command

  • Use find when: you need real-time results, want to filter by permissions, size, date, or type, or plan to act on the results with additional commands like -exec.
  • Use locate when: you want a quick search by filename and do not need results from files created in the last few minutes.

A Quick Tip: Combining find with Actions

One of the most powerful features of find is the ability to execute a command on every result using -exec:

find /home -name "*.tmp" -exec rm {} \;

This finds all .tmp files under /home and deletes them. The {} is a placeholder for the matched filename, and \; terminates the -exec expression. Use this carefully, especially with destructive commands like rm.

What's Next

Now that you can search for files efficiently, the next skill to build is understanding how to manage file permissions and ownership. Knowing where a file is only gets you so far if you cannot read or modify it. In the next post, we will cover the chmod, chown, and chgrp commands and how Linux permission bits work in practice.

For deeper study, check out the CompTIA Linux+ Study Guide by Richard Blum, which covers file management commands in detail alongside exam objectives.


Tools and resources for this topic