Master Linux File Search: locate vs find – Speed, Options, and Tips
This guide compares Linux’s locate and find utilities, explaining their performance characteristics, usage options, search criteria, and actions, while providing practical examples and tips for efficient file searching and manipulation on Unix-like systems.
Linux provides several file‑search utilities; this article focuses on locate and find .
1. locate
Performance Overview
locate queries a pre‑built file index database (usually /var/lib/mlocate/mlocate.db). If the database is missing, locate cannot work until updatedb is run or the system is rebooted.
The index is built automatically during idle periods via a daily cron job ( /etc/cron.daily). Administrators can also update it manually with updatedb. Building the index traverses the entire filesystem and can be resource‑intensive.
Features
Fast search speed
Fuzzy matching
Non‑real‑time (relies on the existing index)
Searches full file paths, not just names
Only returns results in directories where the user has read/execute permissions
Common Options
-i– case‑insensitive search -n # – show only the first # matches -r – enable regular‑expression search
2. find
find performs real‑time searches by traversing specified paths; unlike locate, it does not rely on a pre‑built index. It offers far more powerful criteria, including permissions, file type, size, timestamps, and more.
Features
Search speed is slower than locate because it scans the filesystem
Precise search with support for regex and wildcards
Real‑time results
Respects user read/execute permissions
Syntax
find [OPTION]... [path...] [expression] [action]Path defaults to the current directory. If no expression is given, all files under the path are matched. The default action is to print the pathname.
Common Expressions
Depth control: -maxdepth N, -mindepth N Name matching: -name pattern, -iname pattern (case‑insensitive)
Inode: -inum N, -samefile name Link count: -links N Regex: -regex "PATTERN" (default emacs regex; use -regextype egrep for egrep syntax)
Owner/group: -user USER, -group GROUP, -uid UID, -gid GID, -nouser, -nogroup File type: -type f (regular), d (directory), l (symlink), s (socket), b (block), c (character), p (pipe)
Size: -size [+|-]#UNIT where UNIT can be k, M, G, or c (bytes)
Time: -atime, -mtime, -ctime with [+|-]# days, or -amin, -mmin, -cmin for minutes
Permissions: -perm mode (exact), -perm -mode (all bits set), -perm /mode (any bit set)
Combining Tests
Use -a (and, default) or -o (or). Negate with -not or !. Example: find -nouser -a -nogroup.
Actions
-print– default output -delete – remove matched files without prompting -ls – list in ls -l style -fls file – write long listing to file Redirection: > file,
>> file -ok command \;– interactive execution -exec command \; – non‑interactive execution
Use {} as a placeholder for the current file name
Using xargs with find
xargsbuilds command‑line arguments from standard input, useful when a command does not accept piped input. Example: find /etc -name "*.sh" | xargs ls -l. It also avoids argument‑list limits, e.g., echo {1..30000} | xargs touch.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
