File SEARCH Command: locate, find, locatefind

Source: Internet
Author: User

File SEARCH Command: locate, find, locatefind

File Search
Search for qualified files on the file system
File Search command:
Locate: non-real-time search (database search)
Find: Real-time search

Locate:
Query System pre-built file index database
/Var/lib/mlocate. db

Dependent on pre-built Indexes
Index construction is performed automatically when the system is idle (periodic tasks), and the administrator updates the database manually (updatedb)

Indexing requires traversing the entire root file system, which consumes a lot of resources.
Features:
• Fast search speed
• Fuzzy search
• Non-real-time search
• Search for the full path of a file, not just the file name
• Only search for directories with read and execution Permissions
Locate command
Locate KEYWORD

-I: perform a case-insensitive search.

-N N: only list the first N matching items.

-B: Only match the base name in the path

-C: only count the number of qualified paths

-R: You can write commands based on basic regular expressions.

Locate foo
• Search for files with a name or path containing "foo"
Locate-r '. foo $'
• Use Regex to search for objects ending with ". foo"

Find
The real-time search tool completes file search by traversing the specified path;
Features:
• Slow query speed
• Precise search
• Real-time search
• Only search for directories with read and execution Permissions
Usage:
Find [OPTION]... [search path] [Search Condition] [processing action]

Search Path:
Specify the target path. The default path is the current directory.
Search criteria:
The specified search criteria include file name, size, type, and permission. By default, all files in the specified path are found.
Action:
Perform operations on files that meet the conditions and output them to the screen by default.

SEARCH CRITERIA
Expression: option and Test
Search by file name and inode:
-Name "file name": glob *,?, [], [^]

-Iname "file name": case-insensitive

-Inum n: search by inode number

-Samefile name: file with the same inode number

-Links n: a file with n links

-Regex "PATTERN": uses PATTERN (Regular Expression) to match the entire file path string, not just the file name ID.

Search by owner and group:
-User USERNAME: searches for files whose owner is the specified user (UID ).

-Group gr name: searches for files whose group is the specified group (GID ).

-Uid UserID: searches for files whose owner is the specified UID.

-Gid GroupID: Find the file whose group is the specified GID.

-Nouser: Search for files with no owner

-Nogroup: Search for files without groups

Search by file type:
-Type TYPE
F: Common File

D: directory file

L: Symbolic Link file

S: Socket File

B: block Device Files

C: character Device File

P: MPS queue File

Combination conditions:
And:-

Or:-o

Non:-not!

De Morgan's Law:
(Not A) or (not B) = Not (A and B)
(Not A) and (not B) = Not (A or B)
Example:
! A-! B =! (A-o B)
! A-o! B =! (A-a B)
Find example
Find-name snow.png
Search for a file named snow.png

Find-iname A.png
Search for files named a.png, A. PNG, etc.
Find/-name". Txt"
Find the file with the suffix txt in the root directory.
Find/var-name"
Log *"
Search for files containing logs in the var directory
Find-user test-group test
Search for Files Owned by user test and group test

Other examples:
Find-user test-not-group test
Find-user test-o-user test
Find-not (-user test-o-user test)
Find/-user test-o-uid 500

Find the file whose owner is not root and whose file name does not start with f Under the/tmp directory.
Find/tmp (-not-user root-a-not-name 'f')-Ls
Find/tmp-not (-user root-o-name 'f
')-Ls

Exclude directory
Example: Find all files with the. conf Suffix in the/etc/directory except the/etc/sane. d directory.
Find/etc-path '/etc/sane. d'-a-prune-o-name "*. conf"-print

Search by file size:
-Size [+,-] # unit
Common Unit: k, M, G

For example, 6 k indicates (5 k, 6 k]
-# Unit: [0 to #-1]
For example,-6 k indicates [0 to 5 k]
+ # Unit :( # and greater #)
For example, + 6 k indicates (6 k or greater than 6 k)

Based on the timestamp
In days (time ):
-Atime [+ |-] # access time

+ #: Indicates (# + 1) days out of access

-#: # Accessed in days

#: Indicates that the accessed time period is smaller than (# + 1) or greater than # Day

-Ctime: Change Time
-Mtime: modification time
In minutes)
-Amin
-Mmin
-Cmin

Search by permission:
-Perm [+ |-] MODE

MODE: exact match

+ MODE: match the permissions of any class of users. It is often used to find out whether a specific permission of a class of users exists.

-MODE: the specified permission bits for each type of users must be checked.

Example:
Find-perm 755, will match the file with the permission pattern exactly 755
As long as anyone has the write permission, find-perm + 222 will match
Find-perm-222 matches only when everyone has the write permission.
Find-perm-002 will match only when others (other) have the write permission

Action:

-Print: default value, which is printed on the standard output.

-Ls: outputs the information of each file in a long format, similar to ls-l

-Delete: delete the searched file.

-Fls file: The long format information of all the searched files is saved to the specified file.

-Exec COMMAND {};: Specifies the COMMAND for the searched File

-OK COMMAND {};: Interactive. Execute the COMMAND specified by the COMMAND for each file found.

Find | xargs COMMAND: find transfers all the searched files to-exec for execution at a time.

{}: Used to reference the searched file name itself

Parameter substitution: xargs
The xargs parameter used to generate a command can read stdin data, and the stdin data is separated into arguments by a space character or carriage return.

Note: When the file name or other nouns contain space characters

Some commands cannot accept too many parameters, and the command execution may fail. xargs can solve this problem.
Example:
Find | xargs COMMAND
Find/sbin-perm + 7000 | xargs ls-l

Example:
Find-name "*. conf"-exec cp {}{}. orig;
Back up the configuration file and add the. orig extension.

Find/tmp-ctime + 3-user joe-OK rm {};
The system prompts you to delete a temporary file of joe that has been in existence for more than 3 days.

Find ~ -Perm-002-exec chmod o-w {};
Search for files in your home directory that can be written by other users

Find/data-type f-perm 644-name "*. sh"-exec chmod 755 {};

Find/home-type d-ls

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.