File search under Linux system

Source: Internet
Author: User

In our actual application, often need to find a specific file, or based on a particular attribute of the file to find, today, the side of the cake to share, Linux system file search for two big weapon:
1,locate: Non-real-time lookup (based on pre-generated database lookups), fuzzy matching, fast speed
2,find: Real-time lookup (traversal of all files in the directory to find), exact match, support for many search criteria, slow

First, locate
Querying the pre-generated file index database on the system:/var/lib/mlocate/mlocate.db
Dependent on pre-built indexes; The index is built automatically when the system is idle (recurring tasks)
Administrator manually update database commands: UpdateDB
The index build process needs to traverse the entire root filesystem, consumes resources, and recommends updating the server during idle time to avoid impacting business speed

Working characteristics:
1, non-real-time lookup
2, Fuzzy matching
3, Fast search speed
Search for the full path of the file, not just the filename
may only search directories where the user has read and Execute permissions

Usage:

Locate [OPTION] ... KEYWORD
-I: Perform case-sensitive search
-N #: List only the first # matching items
-R ' PATTERN ': Search using regular table
For example: Locate foo Search name or file with "foo" in the Path
Locate-r ' \.foo$ ' uses Regex to search for files ending with ". Foo"

Second, find
Working characteristics:
1, real-time search
2, Exact match
3, slow to find
may only search directories where the user has read and Execute permissions
Real-time Find tool to complete file lookups by traversing a specified path

Usage:

Find [OPTION] ... [Find Path] [Search Criteria] [Handling Action]
Find path: Specify a specific target path, default to the current directory
Find criteria: The specified lookup criteria, can be file name, size, type, permissions and other criteria, by default to find all files under the specified path
Handling actions: Perform actions on eligible files, output to screen by default

Find criteria
Search by file name and Inode:
-name "file name": Support using Glob *,?, [], [^]
-iname "file name": Letter case insensitive
-inum #: Search by Inode number
-samefile Name: file with the same inode number
-links #: Number of links to # files
-regex "pattern": matches the entire file path string with pattern, not just the file name

According to the file belongs to the main, belong to group search:
-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

Find by File type:
-type Type:
F: Normal file
D: Catalog file
L: Symbolic Link file
S: Socket file
B: Block device files
C: Character device file
P: Pipeline File

Combination conditions:
With:-A
Or:-O
Non:-not,!

De Morgan Law:
Non (p and q) = (not P) or (not Q)
Non (p or q) = (not P) and (non-Q)
! A-a! B =! (A-o B)
! A-o! B =! (A-a B)

Depending on the file size, look for:
-size [+|-]#: Common units: K, M, G
#: (#-1, #), such as: 6k approximately equal to 6K files
-#: (#,#-1), such as: -6k less than 6K files
+#: (#,∞), such as: +6k files greater than 6K

Depending on the file timestamp:
Take "Day" as the unit;
-atime [+|-]# to access time
-mtime to modify content time
-ctime to modify attribute time
#: [#,#+1] #天
+#: [#+1,∞] At least # days
-#: (0,#) up to # days

In "Minutes" units:
-amin
-mmin
-cmin

Search by file permissions:
-perm [/|-]mode
MODE: Exact permission match
/mode: The permission of any class (U,g,o) object can be a match, or a relationship, + from the CENTOS7 start elimination
-mode: Each class of objects must have the specified permission, and the relationship
0 indicates no concern
Find-perm 755 matches a file with a permission pattern of exactly 755
As long as any person has write permission, Find-perm +222 will match
find-perm-222 matches only when everyone has permission to write
FIND-PERM-002 matches only if other people (other) have write permissions

Handling actions:
-print: Default processing action, display to screen
-ls: Similar to performing a "ls-l" command on a found file
-delete: Delete the found file
-fls file: Long format information for all files found is saved to the specified files
-ok Command {} \: Executes commands specified for each file found, and interactively asks the user to confirm each file before executing the command
{}: Used to reference the found file name itself
-exec Command {} \: Executes commands specified for each file found

Find passes the found file to the command specified later, finds all eligible files at one time to pass to the following command, some commands can not accept too many parameters, the command execution may fail, the following way to circumvent this problem
Find | Xargs COMMAND

Three, example demo:
 1, Find/ The Var directory is subordinate to root, and all files belonging to the group Mail
  find/var-user root-a-group mail-ls
 2, find the/var directory is not root, LP, All Files of GDM
  find/usr-not-user root-o-not-user lp-o-not-user GDM
  find/var-not \ (-use R root-o-user lp-o-user GDM \)-ls
 3, find the content of the last week in the/var directory has been modified, and the owner is not root, nor postfix files
  find/var -mtime-7-a-not \ (-user root-o-user postfix \)-ls
 4, find files that are not in the primary or group on the current system, and have been visited in the last week
  find/-N Ouser-o-nogroup-atime-7-S
 5, find all files that are larger than 1M in the/etc directory and are of the normal file type
  find/etc-size +1m-a-type F-ls&nbs P;&NBSP
 6, find files with no write permission for all users in/etc directory
  find/etc-not-perm/222-ls
 7, Find/ In the ETC directory, there is at least one class of files that the user does not have permission to execute
  find/etc-not-perm-222-ls
 8, lookup/etc/ INIT.D directory, all users have execute permissions, and other users have write permission to the file
  find/etc/init.d/-perm-113-ls

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1838492

File search under Linux system

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.