Linux Search Tools

Source: Internet
Author: User
Tags egrep

Hello everyone! This article introduces the search tools for Linux: Locate, find, and more precise finder. Before we learned LS |grep can also achieve the function of searching files, but LS is based on file name lookup, although collocation grep can follow certain conditions to find files, and because each file format is not unique, so this method does not always apply. Here is a brief introduction to the simple and accurate search tool1, locate: Database-based query, non-real-time, the database needs to be updated for newly created files. Local is based on the file full path of the fuzzy query, the query will judge the permission format: Locate keyword-i ignore case-n X only list the first n matching items updatedb update locate database /var/lib/mlocate/mlocate.db-r supports regular eg:locate conf search names or files with "conf" in the path locate-r ' \.conf$ ' using regex To search for ". conf 2. Find: Real-time Query, the speed certainly did not locate fast. But it can be based on file size, owner, group, access time, filename, file type, hard links and other more accurate way to find. You also need to have the appropriate permissions (RX)   (1) syntax: Find [OPTION] ... [Find Path] [Search Criteria]   [Processing action] Find path: Specify the target path, default to the current directory lookup criteria: The specified criteria for finding, can be file name, size, type, permissions and other standards; The defaults are to find all the file processing actions under the specified path: Perform actions on eligible files, default output to screen   (2) Find-maxdepth level maximum search depth-minxdepth level minimum search depth-name name-based exact lookup, support wildcard *,?, [], [^] Eg:find-name "M y* "searches the current directory (including subdirectories), all files with the file name beginning with my-iname find-empty based on name ignoring case: Empty file-gid n or-group name:gid N or group name is NA Me  -ipath p,-path p: matches a pathname, Ipath ignores case-inum based on Inode lookup-samefile "file name" lookup based on the same inode number (Find hard link)-links n Files with hard links n-regex "PATTERN" supports the regular default (Emacs standard), which matches the full path, not just the filename Eg:find-regex ". *f[1-9]" because it matches the full path so ". *" to be added to the front-reg Extype Egrep-regex support Egrep with standard regular-user user name based on file owner lookup-group group name based on file group lookup-uid userid lookup based on file uid-gid gr Oupid file GID-based lookup-nouser find files without owner-nogroup find files with no group-type based on file type lookup? F: Normal file? D: catalog file? L: Symbolic link file? S: Socket file? B: Block device files? C: Character device files? P: Pipe files   (3) combination conditions:-A with,-O or,-not=! Non-Demogan law:! A-a! B =! (A-o B)! A-o! B =! (a-a B) eg:find/tmp \ (-not-user root-a-not-name ' f* ' \)-lsfind/tmp-not \ (-user root-o-name ' f* ' \) – LS need to be aware of F * indicates that the file name begins with F *f* means that the file name contains F can \ (represents the start of a complex expression \) Represents the end of a complex expression   (4) Exclude directory Find [-path ...] [expression] After the path list is the expression-path "/usr/sam"-prune-o-print is-path "/usr/sam"-a-prune-o-print the shorthand expressions are evaluated sequentially,-A and-O are short-circuit-evaluated, with Shell's && and | | Similarly, if-path "/usr/sam" is true, then the evaluation-prune,-prune returns True, and the logical expression is true; otherwise, the-prune is not evaluated, and the logical expression is false. If the-path "/usr/sam"-a-prune is false, the-print is evaluated, the-print returns True, or the logical expression is true, otherwise no value-print, or the logical expression is true.   Eg:find/etc-path '/ETC/SANE.D '-a-prune-o-name "*.conf"   Find files with the file name ending in. conf in the/etc directory (except/ETC/SANE.D Record not query) find/etc \ (-path '/etc/sane.d '-o-path '/etc/fonts ' \)-a-prune-o-name "*.conf"   Find files in/etc directory Files ending with. conf (except/ETC/SANE.D directory,/etc/fonts not queried)   (5) search based on file size-size [+|-] #UNIT based on file sizeFind common units: K, M, G,c (Byte) #UNIT: (#-1, #] such as: 6k (5k,6k]-#UNIT: [0,#-1] such as: -6k means [0,5k]+ #UNIT: (#,∞) such as: +6k (6k,∞) find In order to use regular regular expressions, you need the option-regex "pattern", note that the common option of Find-name is not support regular expression, at best, can only say that the-name option to support the wildcard character *? []    (6) based on timestamp lookup in "Day" units;-atime [+|-]#,#: [#,#+1] +#: [#+1,∞]-#: [0,#]-mtime-ctime with "minutes" :-amin-mmin-cmin  (7) Based on permissions lookup-perm [mode|+mode|-mode]mode: Exact permissions match +mode[/mode] any class (U,g,o) object's permissions as long as one match is available, or the relationship , + starting from Centos7-mode each class of objects must have the specified permission at the same time, and the relationship 0 means that the   (8) Processing action is not followed:-print default-delete directly deletes the files found and does not ask. -ls long lists the files found-fls file will find a long list of files to import into the specified file. > File-ok command {} \; Perform (interactive)-exec command {} \ For each file found as the parameter of the next command; Perform (non-interactive) {}: For each file you find as a parameter of the next command to use to refer to the file name itself     the detours that those days have gone by: Problem Description: Find-name "F2"-o-name "F3"-a-exec echo {} \;        Only the results of the last search criteria match will be passed to the EXEC command to make the parameters find \ (-name "F2"-o-name "F3" \)-a-exec echo {} \; All the query results in parentheses are passed as arguments to the EXEC command to make arguments: Should the query order of find not be executed sequentially?     When the F2 does not match to the return false value, then matches the second condition F3.       So should we just match to a condition and return a true, will it pass through the-exec to the command?       After repeated debugging, found Find-name "F2"-o-name "F3"-a-exec echo {} \; This type of writing only passes the results of the pre--exec conditions to the subsequent commands, and the query order of find does not have a relationship with the parsing principle: Find-name "F2"-O-name "F3"-exec echo {} \;(1) No brackets will-name "F3"-a-exec echo {} \; as a whole (2)-exec will pass the result to the command, no longer display in standard output (equivalent to output redirection)  Summary: Find sees the condition as a two-part:-name "F2" or-name "F3"-exec Echo {} (1) First F3, matching to later will pass the query results to the EXEC command, and then execute (2)-N Ame "F2" query results, originally should be displayed in the standard output, but because the results passed to the-exec, and exec only pass F3 matching results, so F2 not show Practice:1. Find the main root of the/var directory, and belong to all files of the group Mail find/var-user root-a-group mail-ls2, find all files that do not belong to root, LP, GDM in/var directory find/var-no T \ (-user root-o-user lp-o-user GDM \)-LS3, find the contents of the last week in the/var directory has been modified, and the owner is not root, is not postfix file find/var/-mtime-7-no T-user root-not-user Postfix-ls4, find files that are not owned or owned by the current system, and have been visited in the last week Find/-nouser-o-nogroup-a-atime-7-ls5, find/etc All files that are larger than 1M in the directory and type as normal files find/etc/-size +1m-a-type f-ls6, find files with no write permission for all users in/etc directory find/etc! -PERM/222-LS7, find at least one class of users in the/etc directory that do not have permission to execute files find/etc! -PERM-111-LS8, find/etc/init.d directory, all users have execute permissions, and other users have write permission to file find/etc/init.d/-perm-113-ls9, find the name matches the regular expression file, note the previous '. * ' ( Found file with directory) find./-regex. *so.*\.gz 10, find the directory and list the files in the directory (the LS command is executed separately for each directory found, there is no option-print the previous line of the file list does not display the directory name) find./-type D -print-exec ls {} \; 11. Find the directory and list the files under the directory (the LS command is executed separately for each directory found) find./-type d-ok ls {} \;

Linux Search Tools

Related Article

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.