2018-1-9linux basic Knowledge (20) file Lookup command

Source: Internet
Author: User

in this chapter we begin to tell the file lookup commands, in Linux, mainly around these two commands, one is locate. The other is the find command.

First, File search

We look for a file in Windows, for example (c,d,e,f) disk one of the partitions or the entire partition to find, then after the search, matching the search criteria for the file will be automatically displayed, this is a file lookup process, The general explanation for file lookups is to find and display files that match the criteria on the file system. This differs from the previously learned grep command, where grep is used to match a string, but the file lookup is to find the file itself, not the contents of the file, and the implementation tool for file lookups in Linux is: Locate and find. But the two commands implement a different mechanism.

Second, locate command

  locate command is a fuzzy matching lookup command that will be displayed as long as the path contains the searched characters, which is fast and has excellent performance. Just not traversing the entire file system to achieve, but query the pre-built database, the database is not real-time, waiting for the system to be idle, is automatically built at some point. So let's summarize the following main features of the command:

  locate:      relies on pre-built index libraries, and if not, through a recurring task;          System Auto-implementation (recurring task);         manually update the database (updatedb);              Working Characteristics:          Find Fast;         fuzzy find;          Non-real-time lookup; 

The format of the Locate command is:

Locate [OPTION] ... PATTERN ...-B: matches only the base name in the path;-C: Counts the total number of eligible files;-r:bre Note: The indexing process requires traversing the entire root filesystem, consuming resources extremely;

third, find command

Find: Real-time Find tool, through the traversal of the specified path file system hierarchy to complete the file search; job characteristics: Search speed: slightly slow; find real-time search;

The format of the Find command is:

Find [OPTIONS] [Find starting path] [find condition] [processing action]

All of the above are optional, the default is the Find command, only under the current path is found, because no search criteria are specified, so will meet the criteria, and the action is the default standard output.

Find the actual path: Specify the specific search target starting path, default to the current directory, search criteria: The specified criteria for finding, can be based on the file name, size, type, affiliation, permissions and so on standard, and to find all the files under the specified path; such as delete operations, the default is output to standard output (screen), the default is not specified when the condition, will conform to the display;

3.1 Find a condition

   find criteria can also be referred to as lookup criteria, by a - and long options or can be understood as complete word composition used as test , the default or specified processing occurs when the condition is met, so a find condition or lookup criterion is also called its expression, consisting of options and tests. What we're talking about is the implementation of the test in the find condition, and the test is mainly composed of the following types:
   Let's start with the first one, the test condition for finding based on the file name.

    Find:    -name  "pattern"     -iname  "according to the file name Pattern "         supports GLOB-style wildcard characters; not regular expression style;             *, ?, [], [^]             -regex pattern: Finds the file based on the regular expression pattern, matches the entire path, not the base name; 

The second is to find it based on its file affiliation, which means to look at the owner of the file that belongs to that user.

Search based on file affiliation:-user USERNAME: Finds all files in the user specified by the master,-group GRPNAME: Finds all the files in the specified group of the genus Group;

If the file owner or owner group is not or is deleted, only the UID and GID are displayed, then we can also find it by the ID of the genus Group.

-uid UID: Finds the UID file specified by the owner;-gid GID: Finds all files belonging to the specified group of groups;

Conversely, as long as the ID number does not belong to the main group of files, we call it a file that is not a group of the primary genus.

-nouser: Find files that are not owned by the master;-nogroup: Find files that are not owned by the master;

   then the third is a test condition for finding based on the type of file.

    find:    -type type:       based on the type of file   f: Normal file;         d: catalog file;         l: Symbolic link file;         B: Block device file;         c: Character device file;         p: Pipe File;         s: Socket file; 

We can edit the complex condition test, as long as the result of the test is usually a Boolean, or True or false, so according to its logical operation mode combination, and the logical operation is nothing more than what we said before and, or, non-, different or combination, and here is to talk about the conditions to join together.

Combination test: With:-A, default combination logic; or:-O non:-not,!

   We continue to say fourth, fourth is to search according to the size of the file, the default is in bytes, then with the minus sign, not exactly find, The direct default write unit, it is a half open half-closed interval, equivalent to this number minus 1 and less than the number itself, meaning can be less than this value, but to be less than the unit number itself, cannot exceed the number.
  -#UNIT is less than the specified file unit and cannot exceed the file itself.
   + #UNIT意味着要大于该指定的文件单位, cannot be less than the file itself, meaning it does not contain the file.

   -size [+|-] #UNIT (units)      common units:k, m, g          #UNIT: (#-1, #]    -#UNIT:[0, #-1]     + #UNIT: (#, oo)               Note:oo  positive Infinity 

   then we're going to say based on its time stamp lookup, this is a very troublesome find operation, it is based on two categories to find, one is "day" units, the other is to "Minutes" units. We take days as an example, divided into Atime, Mtime and ctime,atime for access time, mtime for content modification time, CTime for property modification time. And they also support the basic unit of addition or subtraction, and we take atime as an example.
   If #, means that the condition is greater than or equal to the current time in the past, but not greater than the number. For example, finding a file three days ago means that from now on, it will be greater than or equal to three days, but not more than four days.
   If it is-#, this means that this condition is satisfied at the current time to the past, and cannot exceed the condition, for example, to find files that are less than three days old.
   If it is +#, it means that the condition is satisfied at the current time to the past, and exceeds the condition itself, for example, to find a file that is three days away.

    with "Day" as the unit:    -atime [+|-]#         #:[#,#-1)         -#:[#,#-1)          +#: (#,oo)     -mtime    -ctime    "Minutes" Units:     -amin    -mmin    -cmin 

Then the next step is to find it based on permissions.

-perm [/|-] mode mode: exact permission match;/mode: any one (r,w,x) of the permissions of any class of users (U,g,o) satisfies the condition, and (one can match) the existence of a "or" relationship between 9-bit permissions; -mode: Each of the Users (U,g,o) in the permissions of each bit (r,w,x) at the same time meet the conditions, must have a 9-bit permission exists between the "and" relationship; on the contrary, at least one does not;

3.2 Handling Actions

After the search has been completed, we can do a processing of the results, for example, in addition to the default display, we can also produce or save information. Then the usual processing actions are as follows:

    processing action:     -print: Output to standard output; default execution action;     -ls: Similar to the file to find "ls -l" command, output file details;     -delete: Delete the found file;     -fls /path/to/ Somefile: Saves long-form information on the found file to the specified file;    -ok command {} \;  : Executes commands for each file that is found, and each action is confirmed by the user;    -exec command {} \;  : Executes command-represented commands on each found file;         {}: Reference to the filename found; 

For example:

# Find/-nouser-a-nogroup-ok chown root:root {} \;    # Find/-nouser-a-nogroup-exec chown root:root {} \; # Find./-user-perm/002-exec mv {} {}.tmp \;

It is important to note that the amount of files found by find is very large, and find to pass the file path to find to the following command, is to find out all eligible file paths, and pass to the following command, but some commands can not accept too long parameters, when the command execution will fail ; Another way to circumvent this problem.

# Find | Xargs COMMAND


2018-1-9linux basic Knowledge (20) file Lookup command

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.