Linux File Search Tools: Locate vs Find

1. 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. You need to manually update the database (updatedb) or restart the system for locate to function again.
Relies on a pre-built index:
The index is constructed automatically during idle times on the system (periodic task /etc/cron.daily), and the administrator can manually update the database(updatedb)
Building the index requires traversing the entire root file system, which is resource-intensive.

Linux File Search Tools: Locate vs Find

2. Working Characteristics
Fast search speed
Fuzzy search
Non-real-time search
The search is based on the full path of the file, not just the file name.
• Only searches directories where the user has read and execute permissions; if there are no permissions, even if the database has content, it will not be displayed for security reasons.
(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
2. Find
A real-time search tool that performs file searches by traversing specified paths (find differs from other commands in that options are prefixed with a single -).
Compared to locate, find has powerful functionalities; it can search not only based on file names but also by permissions, file types, sizes, and many other criteria, making it widely applicable.
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. Find Usage Format
find [OPTION]… [search path] [search criteria] [action]
Search path: specify the exact target path; defaults to the current directory
Search criteria: specified search standards, which can include file name, size, type, permissions, etc.; defaults to finding all files in the specified path
Action: operations to perform on the files that meet the criteria, defaults to outputting to the screen, with many other possible actions.
3. Search Criteria
1) Based on Search Depth
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

2) Based on File Name and Inode Search:
-name : Exact search based on name, supports using wildcard characters *, ?, [], [^], etc.
-iname : Case-insensitive exact search based on name
-inum : Search based on inode
-samefile name : Search based on the same inode number (find hard links)
Linux File Search Tools: Locate vs Find
-links n Files with n hard links
-regex “PATTERN” : Supports regular expressions, defaults to (emacs standard regex), querying the range expressed by the regex
Example: “.*\/ [a-z].*” searches for all files starting with a lowercase letter
-regextype egrep -regex Supports egrep standard regular expressions
Linux File Search Tools: Locate vs Find
3) Based on Owner and Group Search:
-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

4) Based on File Type Search:
find -type
f: Regular files
d: Directory files
l: Symbolic link files (soft links)
s: Socket files (/dev/log)
b: Block device files (/dev/sda)
c: Character device files (/dev/tty)
p: Pipe files

Linux File Search Tools: Locate vs Find

5) Based on File Size Search:
find -size [+|-]#UNIT Search based on 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,∞)
6) Based on Timestamp:
Indays“:
-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) works the same as above
-ctime(metadata change time) works the same as above
Inminutes“: (works the same as above)
-amin -mmin -cmin

Linux File Search Tools: Locate vs Find

7) -perm Search based onpermissions:
mode: Exact permission match
+mode[/mode] Any one of the object permissions (u,g,o) only needs to match one bit, OR relation, + Phased out since CentOS 7
-mode Every object must have the specified permission, AND relation
0 indicates no concern
Linux File Search Tools: Locate vs Find
8) 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 Imports the query results into file >> file Appends the query results to file
-ok command
Treats the found files as arguments for the next command (interactive) (do not forget the last
is a fixed format)
-exec command
Treats the found files as arguments for the next command (non-interactive)
{ }: Used to reference the names of the found files themselves
Linux File Search Tools: Locate vs Find
5. Parameter Replacement with xargs (“Universal” Parameter Passing)
xargs is used to generate parameters for a command,
Purpose: Many commands do not support pipe | to pass parameters, xargs can pass all parameters
For example: find /etc/ -name “*.sh” | xargs ls -l
Some commands cannot accept too many parameters; command execution may fail, and xargs can solve this
For example: touch, rm cannot execute more than a certain number of parameters (about 30000)
You can echo {1..30000} | xargs touch

The autumn recruitment has ended, if everyone does not prepare well, it will be hard to find a good job in the spring recruitment.

Here’s a job package for everyone, you can brush up on your spring recruitment and find a good job!

Linux File Search Tools: Locate vs Find

Leave a Comment