Progress: 0/62 (0%)

🔍 Searching for Files

Searching for Files

Finding specific files in Linux is easy with the right commands. Whether you need a precise real-time search or a quick database lookup, Linux has you covered. Think of it like having both a detective (find) and a library index (locate) at your disposal.

find — Search Files by Name, Type, Size, Date

The find command is like having a detective in your computer. It searches directories and subdirectories for files or folders that match certain conditions. Think of it like looking for a specific book in a huge library using clues like title, size, or last borrowed date.

find [path] [options] [expression]
Search by filename
find /home/user/Documents -name "notes.txt"

Searches in /home/user/Documents for a file called notes.txt

Note: Case-sensitive by default.

Case-insensitive search:

find /home/user/Documents -iname "Notes.txt"

The -iname option makes the search case-insensitive (Notes.txt, NOTES.txt, notes.TXT all match).

Search by file type
find /home/user/Documents -type d
  • -type d → directories
  • -type f → regular files
📌 Find all directories:
find /home/user/Documents -type d
Search by size
find /home/user/Documents -size +1M
  • +1M → bigger than 1 Megabyte
  • -1M → smaller than 1 Megabyte
  • 1M → exactly 1 Megabyte

You can use k for kilobytes, M for megabytes, G for gigabytes.

Search by modification date
find /home/user/Documents -mtime -7
  • -mtime -7 → modified in the last 7 days
  • -mtime +7 → modified more than 7 days ago

This is useful for finding recent files or cleaning up old ones.

Execute commands on found files

You can combine find with -exec to run commands on results:

find . -name "*.log" -exec rm {} \;

Deletes all .log files in the current directory and subdirectories

  • {} → placeholder for each file found
  • \; → ends the command

⚠️ Be careful — this is powerful but can delete many files at once!

Real-life analogy

Imagine a detective in a library:

  • Looking for books by title → -name
  • Looking for certain types (books vs magazines) → -type
  • Looking for books added recently → -mtime
  • Destroying old outdated books → -exec rm

find is powerful but careful — like a detective following clues.

locate — Quick Search Using a Prebuilt Database

The locate command is a faster way to find files than find because it searches a prebuilt database instead of scanning the disk in real-time. Think of it like having a library index — you look up the title in the index instead of checking every shelf.

Basic usage
locate filename
📌 Example:
locate notes.txt

Lists all files named notes.txt that exist on the system based on the last database update.

Update the database

The database is not always real-time, so update it with:

sudo updatedb

This refreshes the file index so locate can find newly created files.

Search with patterns
locate "*.log"

Finds all files ending with .log anywhere on the system.

You can combine it with grep to filter results:

locate "*.txt" | grep Documents

Finds .txt files specifically in Documents.

find vs locate comparison
Feature find locate
Real-time ✅ Yes ❌ No (depends on database)
Speed Slower Very fast
Options Very flexible Limited
Use case Precise search with filters Quick filename lookup
Real-life analogy

Imagine you're in a huge library:

  • find → walking through every aisle and checking each shelf (thorough but slow)
  • locate → looking in the library index cards → much faster if index is updated

Topic Summary: Searching for Files

find → powerful, flexible, searches in real-time with many filter options (name, type, size, date, execute commands)

locate → super fast, searches a prebuilt database (remember to run sudo updatedb first)

Use find for precise, complex searches. Use locate for quick filename lookups.