Operations 8 min read

Master Linux File Search: locate vs find – Fast Tips & Advanced Options

This guide explains how the Linux locate and find commands work, covering their performance characteristics, key features, common options, search criteria, combining conditions, actions, and how to use xargs for parameter passing, helping users choose the right tool for efficient file searching.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux File Search: locate vs find – Fast Tips & Advanced Options

1. locate

Performance Overview

locate searches a pre‑built file index stored in /var/lib/mlocate/mlocate.db. If the database is missing, locate won't work; you must rebuild it with updatedb or reboot.

The index is built automatically during idle periods via a daily cron job ( /etc/cron.daily). Building the index traverses the entire filesystem and can be resource‑intensive.

Key Characteristics

Fast search speed

Fuzzy matching

Non‑real‑time – results reflect the last index update.

Full path search , not just filenames.

Searches only directories where the user has read and execute permissions, enhancing security.

Common Options

-i : case‑insensitive search

-n # : show only the first # matches

-r : enable regular‑expression matching

locate diagram
locate diagram

2. find

find is a real‑time search tool that walks the specified path hierarchy. Unlike locate, find offers powerful criteria such as permissions, file type, size, etc.

Key Characteristics

Search speed is slower than locate.

Precise search with support for regular expressions and wildcards.

Real‑time traversal.

Respects read and execute permissions of the searching user.

Syntax

find [OPTION]... [path] [expression] [action]

Path defaults to the current directory. Expression defaults to all files under the path. Action defaults to printing to the screen.

Search Criteria Examples

-name pattern

: exact name match, supports *, ?, [], [^]. -iname pattern: case‑insensitive name match. -inum n: match inode number. -samefile name: find hard links sharing the same inode. -maxdepth n / -mindepth n: limit search depth. -user USER, -group GROUP, -uid UID, -gid GID: filter by owner or group. -size [+|-]#UNIT: size‑based search (k, M, G, c). -atime, -mtime, -ctime with [+|-]# or # : time‑based search in days or minutes. -perm mode: exact permission match; -perm -mode: any of the bits set; -perm /mode: any of the bits.

find criteria diagram
find criteria diagram

Combining Conditions

Use -a (and, can be omitted), -o (or), -not or ! (not). Apply De Morgan’s laws for complex expressions.

Actions

-print

: default output. -delete: delete matched files without prompting. -ls: long listing similar to ls -li. -fls file: write long listing to a file.

Redirection > file or >> file to save results. -ok command \;: interactive execution on each match. -exec command \;: non‑interactive execution. {}: placeholder for the current file name.

Parameter Passing with xargs

xargs

builds command‑line arguments from standard input, useful when a command does not accept piped input or when the argument list would exceed system limits. Example: find /etc -name "*.sh" | xargs ls -l.

xargs example diagram
xargs example diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

OperationsFile Searchcommand-linefindlocate
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.