Linux commands: two find tool Locate,find

Source: Internet
Author: User
Tags egrep

There are a lot of search tools in Linux, and today we mainly explain locate,find two tools.

1. Locate

(1) Querying system for pre-built file index database

/var/lib/mlocate/mlocate.db

Note : If this file is deleted, locate will not be able to use it. You need to manually update the database (updatedb), or restart the system, locate to restore the role

Dependent on pre-built indexes:

The index is built automatically when the system is idle (recurring task/etc/cron.daily), and the administrator manually updates the database (UpdateDB)

The index build process needs to traverse the entire root file system, consuming resources very

(2) Working characteristics:

Fast Search Speed

Fuzzy Lookup

non-real-time lookup

• Search is the full path of the file, not just the filename

• Only search the directory where the user has read and Execute Permissions , no permissions, even if the database has content, does not display, for security

(3) Locate usage

-I case-insensitive search

-N # list only the first # matching items

-R Support Regular

2. Find

Real-time Find tool to complete file lookups by traversing a specified path (find is different from other commands, option only adds one-)

Compared to the Locate,find function is very powerful, in addition to based on file name lookup, but also based on permissions, file type, size and many other ways to find, so we are widely used.

(1) Working characteristics:

• Search speed slightly slower

exact lookup (you can use regular, wildcard characters to implement fuzzy queries)

Real-time search

• Search only directories where the user has read and Execute permissions

(2) Find usage format

Find [OPTION] ... [Find Path] [Search Criteria] [Handling Action]

Find path: Specify a specific target path; defaults to the current directory

Search criteria: Specifies the criteria for finding criteria, such as file name, size, type, permissions, etc., by default to find all files under the specified path

Handling actions: Perform actions on qualifying files, output to screen by default , and more processing actions.

(3) Search criteria

① based on search hierarchy

-Maxdepth level the largest search directory depth, 1 specifies the directory as 1th, current directory

-mindepth level minimal Search Directory Depth

② Search by file name and Inode:

-name: Name-based exact lookup, support for using Word wildcard *,?, [], [^] etc.

-iname: Exact lookup lookup based on name ignoring case

-inum: inode -based lookup

-samefile Name: Lookup based on the same inode number (Find hard links)

-links n Hard-link n files

-regex "PATTERN": Supports regular, defaults to (Emacs standard Regular), queries the range of regular expressions

Example: ". *\/[a-z].*" searches for all files that begin with lowercase letters

-regextype Egrep-regex supports egrep with standard regular

According to the genus, the genus Group looks for:

-user USERNAME: Find files that belong to the specified user (UID)

-group GRPNAME: Finding files that belong to a specified group (GID)

-uid UserID: Find the file that belongs to the specified UID number

-gid GroupID: Finding files with a specified GID number for the genus Group

-nouser: Finding files that are not owned by the master

-nogroup: Finding files that are not owned by a group

④ Search by File type:

Find-type

F: Normal file

D: Catalog file

L: Symbolic Link file (soft connection)

S: Socket file (/dev/log)

B: Block device files (/DEV/SDA)

C: Character device file (/dev/tty)

P: Pipeline File

⑤ is based on the file size to find:

Find -size [+|-] #UNIT finds common units based on file size: K, M, G,c (byte)

          # UNIT: (#-1, #] such as: 6k (5k,6k]

           -# UNIT:[0,#-1] such as: -6k = [0,5k]

+# UNIT:(#,∞) such as: +6k (6k,∞)

⑥ based on time stamp:

In "Days" as the unit:

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

#: [#,#+1] such as: 3 = [3,4]

+#: [#+1,∞] such as: +3 = [4,∞]

-#: [0,#) such as:-3 = [0,3]

-mtime (modification time) usage Ibid.

-ctime (meta data Change time) usage Ibid.

In minutes: (use the same as above)

-amin-mmin-cmin

⑦-perm Search by permissions :

Mode: Exact permission match

+mode[/mode] any class (U,g,o) object's permissions as long as a match can, or relationship ,+ from the Centos7 start to eliminate

-mode each class of objects must have both the specified permission and the relationship

0 indicates no concern

⑧ Combination Condition Lookup:

With:-A can be omitted: find-nouser [-A]-nogroup

Or:-O

Non:-not,!

De Morgan's Law:

(not a) or (not B) = Non (A and B)! A-o! B =! (A-a B)

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

(4) Handling actions

-print Default

-delete directly delete The files found, do not ask.

-ls Long lists the files found, similar to Ls-li

-fls file will find a long list of files to import into the specified file.

> File Imports query results into file >> file to add query results to file

-ok command \; Perform ( interactive ) The found file as a parameter to the next command (do not forget the last \; is a fixed format)

-exec command \; performs ( non-interactive ) on the found file as a parameter to the next command

{}: Used to reference the found file name itself

(5) parameter substitution Xargs("universal" parameter pass)

Xargs is used to generate parameters for a command,

Purpose: Many commands do not support pipelines | To pass parameters, the Xargs command can pass all parameters such as: find/etc/-name "*.sh" |                           Xargs ls-l Some commands cannot accept too many parameters, command execution may fail, Xargs can be resolved for example: Touch, RM cannot execute more than a certain parameter at a time (about 30000) Can echo {1..30000} | Xargs Touch

Linux commands: two find tool Locate,find

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.