Linux File Search Tools: Locate vs. Find – Which is Your Best Choice?

There are many search tools in Linux, and today we will mainly discuss two tools: locate and find.

01

Locate

1. Performance Overview

Queries the pre-built file index database on the system

/var/lib/mlocate/mlocate.db

Note: If this file is deleted, locate will not work. The database must be manually updated (updatedb), or the system must be restarted for locate to function again.

Relies on a pre-built index:

The index is built automatically during periods of low system activity (scheduled task /etc/cron.daily), and administrators can manually update the database (updatedb).

The index building process requires traversing the entire root file system, which is resource-intensive.

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

2. Working Characteristics

• Fast search speed

• Fuzzy search

• Non-real-time search

• Searches the full path of files, not just file names

• Only searches directories where the user has read and execute permissions; if permissions are lacking, results will not be displayed for security reasons, even if the database contains entries.

3. Usage of Locate

-i Case-insensitive search

-n # Only list the first # matching items

-r Supports regular expressions

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

02

Find

A real-time search tool that completes file searches by traversing specified paths (find differs from other commands in that options are prefixed with a single -).

Compared to locate, find is much more powerful; in addition to searching by file name, it can also search based on permissions, file types, sizes, and many other criteria, making it widely used.

1. Working Characteristics

• Search speed is slightly slower

• Precise search (can achieve fuzzy queries through regular expressions and wildcards)

• Real-time search

• Only searches directories where the user has read and execute permissions

2. Usage Format of Find

find [OPTION]… [ search path] [ search criteria] [ action]

Search path: Specify the exact target path; defaults to the current directory

Search criteria: The specified search standards can include file name, size, type, permissions, etc.; defaults to finding all files in the specified path.

Action: Operations to perform on files that meet the criteria, default output to the screen, with many other processing actions available.

3. Search Criteria

① Search by Depth Level

-maxdepth level Maximum search directory depth, -1 specifies the directory as level 1, the current directory

-mindepth level Minimum search directory depth

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

② Search by File Name and Inode:

-name: Exact search based on name, supports wildcard characters *, ?, [], [^], etc.

-iname: Case-insensitive exact search based on name

-inum: Search by inode

-samefile name: Search based on the same inode number (find hard links)

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

-links n: Files with n hard links

-regex “PATTERN”: Supports regular expressions, defaults to (emacs standard regex), queries the range expressed by the regex

Example: “.*\/ [a-z].*” searches for all files starting with a lowercase letter

-regextype egrep -regex supports egrep standard regex

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

③ Search by Owner and Group:

-user USERNAME: Search for files owned by the specified user (UID)

-group GRPNAME: Search for files belonging to the specified group (GID)

-uid UserID: Search for files owned by the specified UID number

-gid GroupID: Search for files belonging to the specified GID number

-nouser: Search for files without an owner

-nogroup: Search for files without a group

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

④ Search by File Type:

find -type

f: Regular filed: Directory filel: Symbolic link file (soft link)s: Socket file (/dev/log)b: Block device file (/dev/sda)c: Character device file (/dev/tty) p: Pipe file

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

⑤ Search by File Size:

find -size [+|-]#UNIT Search by file size Common units: k, M, G, c (byte)

#UNIT: (#-1, #] For example: 6k means (5k,6k]

-#UNIT: [0,#-1] For example: -6k means [0,5k]

+#UNIT: (#,∞) For example: +6k means (6k,∞)

⑥ Search by Timestamp:

In “days”:

-atime [+|-]#, (access time)

#: [#,#+1) For example: 3 means [3,4)

+#: [#+1,∞] For example: +3 means [4,∞)

-#: [0,#) For example: -3 means [0,3)

-mtime (modification time) same usage as above

-ctime (metadata change time) same usage as above

In “minutes”: (same usage as above)

-amin -mmin -cmin

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

⑦ -perm Search by Permissions:

mode: Exact permission match

+mode[/mode] Any one of the types (u,g,o) must match at least one bit, or relation, + has been deprecated since CentOS 7

-mode Every type must have the specified permission simultaneously, with relation

0 means not concerned

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

⑧ Combined Condition Search:

And: -a can be omitted Example: find -nouser [-a] -nogroup

Or: -o

Not: -not, !

De Morgan’s Laws:

(Not A) or (Not B) = Not(A and B) !A -o !B = !(A -a B)

(Not A) and (Not B) = Not(A or B) !A -a !B = !(A -o B)

4. Action Processing

-print Default

-delete Directly delete the found files without asking.

-ls Long list the found files, similar to ls -li

-fls file Long list the found files and import them into the specified file.

> file Import the query results into file >> file Append the query results to file

-ok command \; Execute the next command with the found files as parameters (interactive) (do not forget the last \; is a fixed format)

-exec command \; Execute the next command with the found files as parameters (non-interactive)

{ }: Used to reference the names of the found files themselves

Linux File Search Tools: Locate vs. Find - Which is Your Best Choice?

5. Processing Parameters with xargs (Universal Parameter Passing)

xargs is used to generate parameters for a command,

Purpose: Many commands do not support piping | to pass parameters, xargs can pass all parameters.

For example:find /etc/ -name “*.sh” | xargs ls -l

Some commands cannot accept too many parameters, and command execution may fail; xargs can solve this.

For example:touch, rm cannot execute more than a certain number of parameters at once (approximately 30000).

You can echo {1..30000} | xargs touch

Link: https://www.cnblogs.com/along21/p/7337302.html

(Copyright belongs to the original author, please delete if infringing)

Leave a Comment